Skip to content

Commit

Permalink
feat: added massrole module
Browse files Browse the repository at this point in the history
* removed borken, unnecessary dependency

* Revert "removed borken, unnecessary dependency"

This reverts commit 15bd079.

* removed broken, unnecessary dependency

* created necessary structure

* Base system now works for target all

* Update massrole.js

Fixed remove-all

* Delete config.json

* Update module.json

Removed config.json

* Update massrole.js

Fixed SCDerox request and added anoter TODO

* Added targets bots and humans

* Implemented autocomplete for "target"-option

* Now using locales for all strings thus making strings.json irrelevant

* Fixed autocomplete by removing locales from options

* Undid last change, since it somehow works now

* Now using old package-lock file

I think this happened when we tried breaking it to get it to work on windows. ~~I hope it doesn't destroy my module~~

* Update massrole.js

Fixed ESLint syntax errors

* Update massrole.js

Removed semicolon

* did npm run test -- --fix to fix errors or smth like that

* Update modules/massrole/commands/massrole.js

Co-authored-by: Simon <sc.de@gmx.de>

* Replaced forEach with for loop

* Now using choices instead of autocomplete

* yes

* keyword-spacing

* Update package.json

* Implemented error-handling if bot hasn't enough permissions

* Implemented error-handling if bot hasn't enough permissions

* ...

* Update default-locales.json

Co-authored-by: Simon <sc.de@gmx.de>

Co-authored-by: Simon <sc.de@gmx.de>
  • Loading branch information
hfgd123 and SCDerox authored May 1, 2022
1 parent bc06a0d commit 24c0bd2
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
/node_modules/
/config/
src/functions/scnx-integration.js
/.vscode/
/.vscode/
/.idea/CustomDCBot.iml
/.idea/modules.xml
/.idea/inspectionProfiles/Project_Default.xml
/.idea/vcs.xml
/.idea/workspace.xml
14 changes: 14 additions & 0 deletions default-locales.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,20 @@
"shop-command-description-list": "List all items in the shop",
"shop-command-description-delete": "Remove an item from the shop",
"channel-not-found": "Can't find the leaderboard channel with the ID %c"
},
"massrole": {
"command-description": "Manage roles for all members",
"add-subcommand-description": "Add a role to all members",
"remove-subcommand-description": "Remove a role from all members",
"remove-all-subcommand-description": "Remove all roles from all members",
"role-option-add-description": "The role, that will be given to all members",
"role-option-remove-description": "The role, that will be removed from all members",
"target-option-description": "Determines whether bots should be included or not",
"all-users": "All Users",
"bots": "Bots",
"humans": "Humans",
"done": "Action executed",
"not-done": "Action couldn't be executed because the bot has not enough permissions."
}
}
}
295 changes: 295 additions & 0 deletions modules/massrole/commands/massrole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
const {localize} = require('../../../src/functions/localize');
let target;
let failed;

module.exports.subcommands = {
'add': async function (interaction) {
checkTarget(interaction);
if (target === 'all') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
try {
await member.roles.add(interaction.options.getRole('role'));
} catch (e) {
failed++;
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}
} else if (target === 'bots') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
if (member.user.bot) {
try {
await member.roles.add(interaction.options.getRole('role'));
} catch (e) {
failed++;
}
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}
} else if (target === 'humans') {
await interaction.deferReply({ephemeral: true});
for (const member of interaction.guild.members.cache.values()) {
if (member.manageable) {
if (!member.user.bot) {
try {

await member.roles.add(interaction.options.getRole('role'));
} catch (e) {
failed++;
}
}
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}
}
},
'remove': async function (interaction) {
checkTarget(interaction);
if (target === 'all') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
try {
await member.roles.remove(interaction.options.getRole('role'));
} catch (e) {
failed++;
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}

}
if (target === 'bots') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
if (member.user.bot) {
try {
await member.roles.remove(interaction.options.getRole('role'));
} catch (e) {
failed++;
}
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}

}
if (target === 'humans') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
if (member.manageable) {
if (!member.user.bot) {
try {
await member.roles.remove(interaction.options.getRole('role'));
} catch (e) {
failed++;
}
}
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}

}
},
'remove-all': async function (interaction) {
checkTarget(interaction);
if (target === 'all') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
try {
await member.roles.remove(member.roles.cache.filter(role => !role.managed));
} catch (e) {
failed++;
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}
} else if (target === 'bots') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
if (member.manageable) {
if (member.user.bot) {
try {
await member.roles.remove(member.roles.cache.filter(role => !role.managed));
} catch (e) {
failed++;
}
}
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}
} else if (target === 'humans') {
await interaction.deferReply({ ephemeral: true });
for (const member of interaction.guild.members.cache.values()) {
if (member.manageable) {
if (!member.user.bot) {
try {
await member.roles.remove(member.roles.cache.filter(role => !role.managed));
} catch (e) {
failed++;
}
}
}
}
if (failed === 0) {
await interaction.editReply(localize('massrole', 'done'));
} else {
await interaction.editReply(localize('massrole', 'not-done'));
failed = 0;
}
}
}
};

/**
* Read content of "target"-option
*
*/
function checkTarget(interaction) {
if (!interaction.options.getString('target') || interaction.options.getString('target') === 'all') {
target = 'all';
} else if (interaction.options.getString('target') === 'bots') {
target = 'bots';
} else if (interaction.options.getString('target') === 'humans') {
target = 'humans';
}
}


module.exports.config = {
name: 'massrole',
description: localize('massrole', 'command-description'),
defaultPermission: false,
options: [
{
type: 'SUB_COMMAND',
name: 'add',
description: localize('massrole', 'add-subcommand-description'),
options: [
{
type: 'ROLE',
required: true,
name: 'role',
description: localize('massrole', 'role-option-add-description')
},
{
type: 'STRING',
required: false,
name: 'target',
choices: [
{
name: localize('massrole', 'all-users'),
value: 'all'
},
{
name: localize('massrole', 'bots'),
value: 'bots'
},
{
name: localize('massrole', 'humans'),
value: 'humans'
}
],
description: localize('massrole', 'target-option-description')
}
]
},
{
type: 'SUB_COMMAND',
name: 'remove',
description: localize('massrole', 'remove-subcommand-description'),
options: [
{
type: 'ROLE',
required: true,
name: 'role',
description: localize('massrole', 'role-option-remove-description')
},
{
type: 'STRING',
required: false,
name: 'target',
choices: [
{
name: localize('massrole', 'all-users'),
value: 'all'
},
{
name: localize('massrole', 'bots'),
value: 'bots'
},
{
name: localize('massrole', 'humans'),
value: 'humans'
}
],
description: localize('massrole', 'target-option-description')
}
]
},
{
type: 'SUB_COMMAND',
name: 'remove-all',
description: localize('massrole', 'remove-all-subcommand-description'),
options: [
{
type: 'STRING',
required: false,
name: 'target',
choices: [
{
name: localize('massrole', 'all-users'),
value: 'all'
},
{
name: localize('massrole', 'bots'),
value: 'bots'
},
{
name: localize('massrole', 'humans'),
value: 'humans'
}
],
description: localize('massrole', 'target-option-description')
}
]
}
]
};
14 changes: 14 additions & 0 deletions modules/massrole/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "massrole",
"humanReadableName": "Massrole",
"author": {
"name": "hfgd",
"link": "https://github.com/hfgd123",
"scnxOrgID": "2"
},
"openSourceURL": "https://github.com/hfgd123/CustomDCBot/tree/main/modules/massrole",
"description-en": "Simple module to manage the roles of many members at once!",
"description-de": "Einfaches Modul, um die Rollen vieler Nutzer gleichzeitig zu verwalten!",
"commands-dir": "/commands",
"tags": ["administration"]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"node-schedule": "^2.1.0",
"parse-duration": "^1.0.2",
"sequelize": "^6.14.0",
"sqlite3": "^5.0.2",
"sqlite3": "^5.0.6",
"twitch": "^4.6.7",
"twitch-auth": "^4.6.7",
"utf-8-validate": "^5.0.8",
Expand All @@ -46,4 +46,4 @@
"devDependencies": {
"eslint": "^7.32.0"
}
}
}

0 comments on commit 24c0bd2

Please sign in to comment.