From c2c055c9ae4169b1b7d153ea0c19865f49409e4a Mon Sep 17 00:00:00 2001 From: hfgd Date: Thu, 26 May 2022 23:38:01 +0200 Subject: [PATCH 1/4] Added auto reply for mentions as requested by shini --- modules/auto-react/config.json | 17 ++++++++++-- modules/auto-react/events/messageCreate.js | 32 ++++++++++++---------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/modules/auto-react/config.json b/modules/auto-react/config.json index da0e6fbc..16759c86 100644 --- a/modules/auto-react/config.json +++ b/modules/auto-react/config.json @@ -22,8 +22,21 @@ "humanname-de": "Erwähnungen", "humanname-en": "Mentions", "type": "keyed", - "description-en": "Here you can add members and the reactions on their mentions of them (you can add multiple emojis with | between each one)", - "description-de": "Du kannst hier NutzerIDs und die dazugehörigen Emojis auf Erwähnungen dieser eintragen (mehrere Emojis müssen mit einem | getrennt werden", + "description-en": "Here you can add members and the reactions on mentions of them (you can add multiple emojis with | between each one)", + "description-de": "Du kannst hier NutzerIDs und die dazugehörigen Emojis auf Erwähnungen dieser eintragen (mehrere Emojis müssen mit einem | getrennt werden)", + "content": { + "key": "integer", + "value": "string" + } + }, + { + "field_name": "membersReply", + "default": {}, + "humanname-de": "Erwähnungsantworten", + "humanname-en": "Mention-replys", + "type": "keyed", + "description-en": "Here you can add members and the reply on mentions of them", + "description-de": "Du kannst hier NutzerIDs und die Antwort auf Erwähnungen dieser eintragen", "content": { "key": "integer", "value": "string" diff --git a/modules/auto-react/events/messageCreate.js b/modules/auto-react/events/messageCreate.js index 7a0a5058..6e7841d4 100644 --- a/modules/auto-react/events/messageCreate.js +++ b/modules/auto-react/events/messageCreate.js @@ -1,14 +1,14 @@ module.exports.run = async (client, msg) => { if (!client.botReadyAt) return; - if (msg.interaction || msg.system || !msg.guild || msg.guild.id !== client.config.guildID) return; + if (msg.interaction || msg.system) return; await checkChannel(msg); await checkMembers(msg); - await checkCategory(msg); await checkAuthor(msg); + await checkMembersReply(msg); }; /** - * Checks for member pings on a message and reacts with the configured emotes + * Checks for member pings in a message and reacts with the configured emotes * @private * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} @@ -27,37 +27,39 @@ async function checkMembers(msg) { } /** - * Checks if a message need reactions (and reacts if needed) because it was send in a configured channel + * Checks for member pings in a message and replys with the configured message * @private - * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) + * @param msg * @returns {Promise} */ -async function checkChannel(msg) { +async function checkMembersReply(msg) { const moduleConfig = msg.client.configurations['auto-react']['config']; - if (!moduleConfig.channels[msg.channel.id]) return; - moduleConfig.channels[msg.channel.id].split('|').forEach(emoji => { - msg.react(emoji).catch(() => { - }); + if (!msg.mentions.members) return; + msg.mentions.members.forEach(m => { + if (moduleConfig.membersReply[m.id]) { + msg.reply(moduleConfig.membersReply[m.id]).catch(() => { + }); + } }); } /** - * Checks if a message need reactions (and reacts if needed) because it was send in a configured category + * Checks if a message need reactions (and reacts if needed) because it was send in a configured channel * @private * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} */ -async function checkCategory(msg) { +async function checkChannel(msg) { const moduleConfig = msg.client.configurations['auto-react']['config']; - if (!moduleConfig.categories[msg.channel.parentId]) return; - moduleConfig.categories[msg.channel.parentId].split('|').forEach(emoji => { + if (!moduleConfig.channels[msg.channel.id]) return; + moduleConfig.channels[msg.channel.id].split('|').forEach(emoji => { msg.react(emoji).catch(() => { }); }); } /** - * Checks if a message need reactions (and reacts if needed) because it was send in a configured channel + * Checks if a message need reactions (and reacts if needed) because it was send by a configured user * @private * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} From 671272b3dbf5935de4799db5d06d7d9e8b88f447 Mon Sep 17 00:00:00 2001 From: hfgd Date: Sun, 29 May 2022 17:03:38 +0200 Subject: [PATCH 2/4] Now deleting welcome messages of users who joined and left within 7 days --- modules/welcomer/configs/config.json | 10 +++++ modules/welcomer/events/guildMemberAdd.js | 21 ++++++++- modules/welcomer/events/guildMemberRemove.js | 45 +++++++++++++++++++- modules/welcomer/models/User.js | 25 +++++++++++ modules/welcomer/module.json | 1 + 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 modules/welcomer/models/User.js diff --git a/modules/welcomer/configs/config.json b/modules/welcomer/configs/config.json index 5aa41264..49a30290 100644 --- a/modules/welcomer/configs/config.json +++ b/modules/welcomer/configs/config.json @@ -39,6 +39,16 @@ "description-de": "Rollen, die Booster haben sollen", "params-de": {}, "default-de": [] + }, + { + "field_name": "delete-welcome-message", + "humanname-en": "Delete welcome message", + "humanname-de": "Willkommensnachricht löschen", + "default": true, + "type": "boolean", + "description-en": "Should their welcome message be deleted, if a user leaves the server within 7 days", + "description-de": "Soll die Willkommensnachricht eines Nutzers, der den Server innerhalb von 7 Tagen wieder verlässt gelöscht werden", + "params-de": {} } ] } diff --git a/modules/welcomer/events/guildMemberAdd.js b/modules/welcomer/events/guildMemberAdd.js index 2534ffd7..16b9606d 100644 --- a/modules/welcomer/events/guildMemberAdd.js +++ b/modules/welcomer/events/guildMemberAdd.js @@ -5,6 +5,7 @@ module.exports.run = async function (client, guildMember) { if (!client.botReadyAt) return; if (guildMember.guild.id !== client.guild.id) return; const moduleConfig = client.configurations['welcomer']['config']; + const moduleModel = client.models['welcomer']['User']; if (guildMember.user.bot && moduleConfig['not-send-messages-if-member-is-bot']) return; const moduleChannels = client.configurations['welcomer']['channels']; @@ -26,7 +27,7 @@ module.exports.run = async function (client, guildMember) { } if (!message) message = channelConfig.message; - await channel.send(embedType(message || 'Message not found', + const sentMessage = await channel.send(embedType(message || 'Message not found', { '%mention%': guildMember.toString(), '%servername%': guildMember.guild.name, @@ -37,5 +38,23 @@ module.exports.run = async function (client, guildMember) { '%createdAt%': formatDate(guildMember.user.createdAt) } )); + + const memberModel = await moduleModel.findOne({ + where: { + userId: guildMember.id + } + }); + if (memberModel) { + await memberModel.update({ + messageID: sentMessage.id, + timestamp: new Date() + }); + } else { + await moduleModel.create({ + userID: guildMember.id, + messageID: sentMessage.id, + timestamp: new Date() + }); + } } }; \ No newline at end of file diff --git a/modules/welcomer/events/guildMemberRemove.js b/modules/welcomer/events/guildMemberRemove.js index be14d8e1..d774d0ee 100644 --- a/modules/welcomer/events/guildMemberRemove.js +++ b/modules/welcomer/events/guildMemberRemove.js @@ -5,6 +5,7 @@ module.exports.run = async function (client, guildMember) { if (!client.botReadyAt) return; if (guildMember.guild.id !== client.guild.id) return; const moduleConfig = client.configurations['welcomer']['config']; + const moduleModel = client.models['welcomer']['User']; if (guildMember.user.bot && moduleConfig['not-send-messages-if-member-is-bot']) return; const moduleChannels = client.configurations['welcomer']['channels']; @@ -33,4 +34,46 @@ module.exports.run = async function (client, guildMember) { } )); } -}; \ No newline at end of file + + const memberModel = await moduleModel.findOne({ + where: { + userId: guildMember.id + } + }); + if (memberModel && moduleConfig['delete-welcome-message']) { + for (const channelConfig of moduleChannels.filter(c => c.type === 'join')) { + const channel = await guildMember.guild.channels.fetch(channelConfig.channelID).catch(() => { + }); + if (await timer(client, guildMember.id)) { + try { + await (await channel.messages.fetch(memberModel.messageID)).delete(); + } catch (e) {} + } + } + await moduleModel.destroy({ + where: { + userId: guildMember.id + } + }); + } +}; + +/** + ** Function to handle the time stuff + * @private + * @param client Client of the bot + * @param {userId} userId Id of the User + * @returns {Promise} + */ +async function timer(client, userId) { + const model = client.models['welcomer']['User']; + const timeModel = await model.findOne({ + where: { + userId: userId + } + }); + if (timeModel) { + // check timer duration + return timeModel.timestamp.getTime() + 604800000 >= Date.now(); + } +} \ No newline at end of file diff --git a/modules/welcomer/models/User.js b/modules/welcomer/models/User.js new file mode 100644 index 00000000..5017ad2a --- /dev/null +++ b/modules/welcomer/models/User.js @@ -0,0 +1,25 @@ +const {DataTypes, Model} = require('sequelize'); + +module.exports = class User extends Model { + static init(sequelize) { + return super.init({ + id: { + autoIncrement: true, + type: DataTypes.INTEGER, + primaryKey: true + }, + userID: DataTypes.STRING, + messageID: DataTypes.STRING, + timestamp: DataTypes.DATE + }, { + tableName: 'welcomer_User', + timestamps: true, + sequelize + }); + } +}; + +module.exports.config = { + 'name': 'User', + 'module': 'welcomer' +}; \ No newline at end of file diff --git a/modules/welcomer/module.json b/modules/welcomer/module.json index 16317262..663f8bb7 100644 --- a/modules/welcomer/module.json +++ b/modules/welcomer/module.json @@ -12,6 +12,7 @@ "description-en": "Simple module to say \"Hi\" to new members, give them roles automatically and say \"thanks\" to users who boosted", "description-de": "Einfaches Modul zum Begrüßen von neuen Usern, zum automatischen Vergeben von Rollen beim Joinen und zum Bedanken bei Boosts.", "events-dir": "/events", + "models-dir": "/models", "config-example-files": [ "configs/channels.json", "configs/random-messages.json", From e2e63fc8581e5d2c9c1480ab05bda0e7f2ba6e9f Mon Sep 17 00:00:00 2001 From: hfgd Date: Sun, 29 May 2022 17:12:29 +0200 Subject: [PATCH 3/4] undid changes that somehow were in the commit but shouldn't have been --- modules/auto-react/config.json | 17 ++---------- modules/auto-react/events/messageCreate.js | 32 ++++++++++------------ 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/modules/auto-react/config.json b/modules/auto-react/config.json index 16759c86..da0e6fbc 100644 --- a/modules/auto-react/config.json +++ b/modules/auto-react/config.json @@ -22,21 +22,8 @@ "humanname-de": "Erwähnungen", "humanname-en": "Mentions", "type": "keyed", - "description-en": "Here you can add members and the reactions on mentions of them (you can add multiple emojis with | between each one)", - "description-de": "Du kannst hier NutzerIDs und die dazugehörigen Emojis auf Erwähnungen dieser eintragen (mehrere Emojis müssen mit einem | getrennt werden)", - "content": { - "key": "integer", - "value": "string" - } - }, - { - "field_name": "membersReply", - "default": {}, - "humanname-de": "Erwähnungsantworten", - "humanname-en": "Mention-replys", - "type": "keyed", - "description-en": "Here you can add members and the reply on mentions of them", - "description-de": "Du kannst hier NutzerIDs und die Antwort auf Erwähnungen dieser eintragen", + "description-en": "Here you can add members and the reactions on their mentions of them (you can add multiple emojis with | between each one)", + "description-de": "Du kannst hier NutzerIDs und die dazugehörigen Emojis auf Erwähnungen dieser eintragen (mehrere Emojis müssen mit einem | getrennt werden", "content": { "key": "integer", "value": "string" diff --git a/modules/auto-react/events/messageCreate.js b/modules/auto-react/events/messageCreate.js index 6e7841d4..7a0a5058 100644 --- a/modules/auto-react/events/messageCreate.js +++ b/modules/auto-react/events/messageCreate.js @@ -1,14 +1,14 @@ module.exports.run = async (client, msg) => { if (!client.botReadyAt) return; - if (msg.interaction || msg.system) return; + if (msg.interaction || msg.system || !msg.guild || msg.guild.id !== client.config.guildID) return; await checkChannel(msg); await checkMembers(msg); + await checkCategory(msg); await checkAuthor(msg); - await checkMembersReply(msg); }; /** - * Checks for member pings in a message and reacts with the configured emotes + * Checks for member pings on a message and reacts with the configured emotes * @private * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} @@ -27,39 +27,37 @@ async function checkMembers(msg) { } /** - * Checks for member pings in a message and replys with the configured message + * Checks if a message need reactions (and reacts if needed) because it was send in a configured channel * @private - * @param msg + * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} */ -async function checkMembersReply(msg) { +async function checkChannel(msg) { const moduleConfig = msg.client.configurations['auto-react']['config']; - if (!msg.mentions.members) return; - msg.mentions.members.forEach(m => { - if (moduleConfig.membersReply[m.id]) { - msg.reply(moduleConfig.membersReply[m.id]).catch(() => { - }); - } + if (!moduleConfig.channels[msg.channel.id]) return; + moduleConfig.channels[msg.channel.id].split('|').forEach(emoji => { + msg.react(emoji).catch(() => { + }); }); } /** - * Checks if a message need reactions (and reacts if needed) because it was send in a configured channel + * Checks if a message need reactions (and reacts if needed) because it was send in a configured category * @private * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} */ -async function checkChannel(msg) { +async function checkCategory(msg) { const moduleConfig = msg.client.configurations['auto-react']['config']; - if (!moduleConfig.channels[msg.channel.id]) return; - moduleConfig.channels[msg.channel.id].split('|').forEach(emoji => { + if (!moduleConfig.categories[msg.channel.parentId]) return; + moduleConfig.categories[msg.channel.parentId].split('|').forEach(emoji => { msg.react(emoji).catch(() => { }); }); } /** - * Checks if a message need reactions (and reacts if needed) because it was send by a configured user + * Checks if a message need reactions (and reacts if needed) because it was send in a configured channel * @private * @param msg [Message](https://discord.js.org/#/docs/main/stable/class/Message) * @returns {Promise} From 793b9ffab5215a5214dc8c4450d6c6eb0d3cf432 Mon Sep 17 00:00:00 2001 From: hfgd Date: Sun, 29 May 2022 18:52:45 +0200 Subject: [PATCH 4/4] Now saving ChannelID instead of fetching from config --- modules/welcomer/events/guildMemberAdd.js | 2 ++ modules/welcomer/events/guildMemberRemove.js | 16 +++++++--------- modules/welcomer/models/User.js | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/welcomer/events/guildMemberAdd.js b/modules/welcomer/events/guildMemberAdd.js index 16b9606d..a3028151 100644 --- a/modules/welcomer/events/guildMemberAdd.js +++ b/modules/welcomer/events/guildMemberAdd.js @@ -46,12 +46,14 @@ module.exports.run = async function (client, guildMember) { }); if (memberModel) { await memberModel.update({ + channelID: sentMessage.channelId, messageID: sentMessage.id, timestamp: new Date() }); } else { await moduleModel.create({ userID: guildMember.id, + channelID: sentMessage.channelId, messageID: sentMessage.id, timestamp: new Date() }); diff --git a/modules/welcomer/events/guildMemberRemove.js b/modules/welcomer/events/guildMemberRemove.js index d774d0ee..34aa049d 100644 --- a/modules/welcomer/events/guildMemberRemove.js +++ b/modules/welcomer/events/guildMemberRemove.js @@ -34,27 +34,25 @@ module.exports.run = async function (client, guildMember) { } )); } - const memberModel = await moduleModel.findOne({ where: { userId: guildMember.id } }); if (memberModel && moduleConfig['delete-welcome-message']) { - for (const channelConfig of moduleChannels.filter(c => c.type === 'join')) { - const channel = await guildMember.guild.channels.fetch(channelConfig.channelID).catch(() => { - }); - if (await timer(client, guildMember.id)) { - try { - await (await channel.messages.fetch(memberModel.messageID)).delete(); - } catch (e) {} - } + const channel = await guildMember.guild.channels.fetch(memberModel.channelID).catch(() => {}); + if (await timer(client, guildMember.id)) { + try { + await (await channel.messages.fetch(memberModel.messageID)).delete(); + } catch (e) {} } await moduleModel.destroy({ where: { userId: guildMember.id } }); + + } }; diff --git a/modules/welcomer/models/User.js b/modules/welcomer/models/User.js index 5017ad2a..fda47dbd 100644 --- a/modules/welcomer/models/User.js +++ b/modules/welcomer/models/User.js @@ -9,6 +9,7 @@ module.exports = class User extends Model { primaryKey: true }, userID: DataTypes.STRING, + channelID: DataTypes.STRING, messageID: DataTypes.STRING, timestamp: DataTypes.DATE }, {