From d83c31f5ec0048b1e8c6c5de3b1a93e0c93bb6ea Mon Sep 17 00:00:00 2001 From: hfgd Date: Tue, 3 May 2022 21:51:29 +0200 Subject: [PATCH 1/7] Created module status-roles --- default-locales.json | 4 +++ modules/status-roles/configs/config.json | 33 +++++++++++++++++++ modules/status-roles/events/presenceUpdate.js | 22 +++++++++++++ modules/status-roles/module.json | 18 ++++++++++ 4 files changed, 77 insertions(+) create mode 100644 modules/status-roles/configs/config.json create mode 100644 modules/status-roles/events/presenceUpdate.js create mode 100644 modules/status-roles/module.json diff --git a/default-locales.json b/default-locales.json index fe69c104..bb37977e 100644 --- a/default-locales.json +++ b/default-locales.json @@ -361,6 +361,10 @@ "all-users": "All Users", "bots": "Bots", "humans": "Humans" + }, + "status-role": { + "fulfilled": "Status-role condition is fulfilled", + "not-fulfilled": "Status-role condition is no longer fulfilled" } } } \ No newline at end of file diff --git a/modules/status-roles/configs/config.json b/modules/status-roles/configs/config.json new file mode 100644 index 00000000..1aa0b9d9 --- /dev/null +++ b/modules/status-roles/configs/config.json @@ -0,0 +1,33 @@ +{ + "filename": "config.json", + "humanname-de": "Konfiguration", + "humanname-en": "Configuration", + "description-en": "Configure the function of the module here", + "description-de": "Stelle hier die Funktionen des Modules ein", + "content": [ + { + "field_name": "words", + "humanname-en": "Words", + "humanname-de": "Statusinhalt", + "default": [], + "type": "array", + "content": "string", + "description-en": "Words users should have in their status.", + "description-de": "Wörter, die Nutzer in ihrem Status haben sollen.", + "params-de": {}, + "default-de": [] + }, + { + "field_name": "roles", + "humanname-en": "Roles", + "humanname-de": "Rollen", + "default": [], + "type": "array", + "content": "roleID", + "description-en": "Roles to give to users with one of the words in their status", + "description-de": "Rollen, die an Nutzer mit einem der Wörter im Status vergeben werden sollen", + "params-de": {}, + "default-de": [] + } + ] +} diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js new file mode 100644 index 00000000..930c7695 --- /dev/null +++ b/modules/status-roles/events/presenceUpdate.js @@ -0,0 +1,22 @@ +const {localize} = require("../../../src/functions/localize"); + +module.exports.run = async function (client, newPresence) { + + if (!client.botReadyAt) return; + const moduleConfig = client.configurations['status-roles']['config']; + const roles = moduleConfig.roles; + const status = moduleConfig.words; + const member = newPresence.member; + + if (newPresence.activities.length > 0) { + if (status.some(word => newPresence.activities[0].state.includes(word))) { + return member.roles.add(roles, localize('mass-role', 'fulfilled')); + } else { + for (let i = 0; i < roles.length; i++) { + if (member.roles.cache.has(roles[i])) { + member.roles.remove(roles[i], localize('mass-role', 'not-fulfilled')); + } + } + } + } +} \ No newline at end of file diff --git a/modules/status-roles/module.json b/modules/status-roles/module.json new file mode 100644 index 00000000..afad7759 --- /dev/null +++ b/modules/status-roles/module.json @@ -0,0 +1,18 @@ +{ + "name": "status-roles", + "humanReadableName-de": "Status-Rollen", + "humanReadableName-en": "Status-roles", + "author": { + "name": "hfgd", + "link": "https://github.com/hfgd123", + "scnxOrgID": "2" + }, + "openSourceURL": "https://github.com/hfgd123/CustomDCBot/tree/main/modules/status-roles", + "description-en": "Simple module to reward users who have an invite to your server in their status!", + "description-de": "Einfaches Modul, um Nutzer zu belohnen, die einen Link zu deinem Server in ihrem Status haben!", + "events-dir": "/events", + "config-example-files": [ + "configs/config.json" + ], + "tags": ["administration"] +} From 312d8dd98b29b86d095f3ab920cd4094d5f556f5 Mon Sep 17 00:00:00 2001 From: hfgd Date: Tue, 3 May 2022 21:55:06 +0200 Subject: [PATCH 2/7] This moment when ESLint just hates you --- modules/status-roles/events/presenceUpdate.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js index 930c7695..a927906f 100644 --- a/modules/status-roles/events/presenceUpdate.js +++ b/modules/status-roles/events/presenceUpdate.js @@ -1,4 +1,4 @@ -const {localize} = require("../../../src/functions/localize"); +const {localize} = require('../../../src/functions/localize'); module.exports.run = async function (client, newPresence) { @@ -19,4 +19,4 @@ module.exports.run = async function (client, newPresence) { } } } -} \ No newline at end of file +}; \ No newline at end of file From 9ff55ef00488c22a4cc044498cab6d77f4dd0ba3 Mon Sep 17 00:00:00 2001 From: hfgd Date: Tue, 3 May 2022 22:34:30 +0200 Subject: [PATCH 3/7] Fixed changes suggested by SCDerox (now checking if state, removed case sensitivity problem, checking if user on guild, oldPresence error, typos) for the for I will commit again --- modules/status-roles/events/presenceUpdate.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js index a927906f..85096c6c 100644 --- a/modules/status-roles/events/presenceUpdate.js +++ b/modules/status-roles/events/presenceUpdate.js @@ -1,20 +1,29 @@ const {localize} = require('../../../src/functions/localize'); -module.exports.run = async function (client, newPresence) { +module.exports.run = async function (client, oldPresence, newPresence) { if (!client.botReadyAt) return; + if (newPresence.member.guild.id !== client.guildID) return; const moduleConfig = client.configurations['status-roles']['config']; const roles = moduleConfig.roles; const status = moduleConfig.words; const member = newPresence.member; if (newPresence.activities.length > 0) { - if (status.some(word => newPresence.activities[0].state.includes(word))) { - return member.roles.add(roles, localize('mass-role', 'fulfilled')); - } else { + if (newPresence.activities[0].state) { + if (status.some(word => newPresence.activities[0].state.toLowerCase().includes(word.toLowerCase()))) { + return member.roles.add(roles, localize('status-role', 'fulfilled')); + } else { + for (let i = 0; i < roles.length; i++) { + if (member.roles.cache.has(roles[i])) { + member.roles.remove(roles[i], localize('status-role', 'not-fulfilled')); + } + } + } + } else { for (let i = 0; i < roles.length; i++) { if (member.roles.cache.has(roles[i])) { - member.roles.remove(roles[i], localize('mass-role', 'not-fulfilled')); + member.roles.remove(roles[i], localize('status-role', 'not-fulfilled')); } } } From c97de54d4db5f32e452ecf63957e9ee71e0fd51c Mon Sep 17 00:00:00 2001 From: hfgd Date: Tue, 3 May 2022 22:41:11 +0200 Subject: [PATCH 4/7] Made for loop better readable --- modules/status-roles/events/presenceUpdate.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js index 85096c6c..6b9b0d88 100644 --- a/modules/status-roles/events/presenceUpdate.js +++ b/modules/status-roles/events/presenceUpdate.js @@ -14,9 +14,9 @@ module.exports.run = async function (client, oldPresence, newPresence) { if (status.some(word => newPresence.activities[0].state.toLowerCase().includes(word.toLowerCase()))) { return member.roles.add(roles, localize('status-role', 'fulfilled')); } else { - for (let i = 0; i < roles.length; i++) { - if (member.roles.cache.has(roles[i])) { - member.roles.remove(roles[i], localize('status-role', 'not-fulfilled')); + for (const key in roles) { + if (member.roles.cache.has(roles[key])) { + member.roles.remove(roles[key], localize('status-role', 'not-fulfilled')); } } } From 079586d2e5e080e293214b61eef3496a8f27d976 Mon Sep 17 00:00:00 2001 From: hfgd Date: Tue, 3 May 2022 22:44:49 +0200 Subject: [PATCH 5/7] removed exactly one space... --- modules/status-roles/events/presenceUpdate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js index 6b9b0d88..f2a15df9 100644 --- a/modules/status-roles/events/presenceUpdate.js +++ b/modules/status-roles/events/presenceUpdate.js @@ -20,7 +20,7 @@ module.exports.run = async function (client, oldPresence, newPresence) { } } } - } else { + } else { for (let i = 0; i < roles.length; i++) { if (member.roles.cache.has(roles[i])) { member.roles.remove(roles[i], localize('status-role', 'not-fulfilled')); From 8ff6bd385b4ee7755c4cc283f1ebf37f9ff6fa9d Mon Sep 17 00:00:00 2001 From: hfgd Date: Wed, 4 May 2022 20:26:37 +0200 Subject: [PATCH 6/7] Now using separate removeRoles() function and added lines 22-23 --- modules/status-roles/events/presenceUpdate.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js index f2a15df9..f572c488 100644 --- a/modules/status-roles/events/presenceUpdate.js +++ b/modules/status-roles/events/presenceUpdate.js @@ -14,18 +14,19 @@ module.exports.run = async function (client, oldPresence, newPresence) { if (status.some(word => newPresence.activities[0].state.toLowerCase().includes(word.toLowerCase()))) { return member.roles.add(roles, localize('status-role', 'fulfilled')); } else { - for (const key in roles) { - if (member.roles.cache.has(roles[key])) { - member.roles.remove(roles[key], localize('status-role', 'not-fulfilled')); - } - } + removeRoles(); } } else { - for (let i = 0; i < roles.length; i++) { - if (member.roles.cache.has(roles[i])) { - member.roles.remove(roles[i], localize('status-role', 'not-fulfilled')); - } + removeRoles(); + } + } else { + removeRoles(); + } + function removeRoles() { + for (let i = 0; i < roles.length; i++) { + if (member.roles.cache.has(roles[i])) { + member.roles.remove(roles[i], localize('status-role', 'not-fulfilled')); } } } -}; \ No newline at end of file +}; From 626249c78c151e4f1d54c3fb6a3a8e6919895454 Mon Sep 17 00:00:00 2001 From: hfgd Date: Wed, 4 May 2022 21:06:44 +0200 Subject: [PATCH 7/7] Added comment for removeRoles() --- modules/status-roles/events/presenceUpdate.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/status-roles/events/presenceUpdate.js b/modules/status-roles/events/presenceUpdate.js index f572c488..36b3101d 100644 --- a/modules/status-roles/events/presenceUpdate.js +++ b/modules/status-roles/events/presenceUpdate.js @@ -22,6 +22,10 @@ module.exports.run = async function (client, oldPresence, newPresence) { } else { removeRoles(); } + + /** + * Removes the roles of an user who no longer fulfills the criteria + */ function removeRoles() { for (let i = 0; i < roles.length; i++) { if (member.roles.cache.has(roles[i])) {