From 9ed53658114cfdfa938a3450ac70201705b24179 Mon Sep 17 00:00:00 2001 From: GramingFoxTeam Date: Mon, 31 Jul 2023 17:59:50 +0300 Subject: [PATCH] feat(bot-discord): stickied messages (#13) --- apps/bot-discord/src/commands/ban.js | 0 apps/bot-discord/src/config.example.json | 18 ++++++++++++++++-- apps/bot-discord/src/config.json | 20 +++++++++++++++++--- apps/bot-discord/src/events/messageCreate.js | 16 +++++++++++++++- apps/bot-discord/src/index.js | 2 ++ 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 apps/bot-discord/src/commands/ban.js diff --git a/apps/bot-discord/src/commands/ban.js b/apps/bot-discord/src/commands/ban.js new file mode 100644 index 0000000..e69de29 diff --git a/apps/bot-discord/src/config.example.json b/apps/bot-discord/src/config.example.json index bf44ed3..fa6c9db 100644 --- a/apps/bot-discord/src/config.example.json +++ b/apps/bot-discord/src/config.example.json @@ -1,9 +1,23 @@ { "discord": { - "trainRoles": ["1019903194941362198", "955220417969262612"], + "trainRoles": [ + "1019903194941362198", + "955220417969262612" + ], "botId": "1038762591805247518", "ignoreRole": "1027874293192863765", - "ignoreChannels": ["953965039105232906", "953964264400515092", "952987428786941952"] + "ignoreChannels": [ + "953965039105232906", + "953964264400515092", + "952987428786941952" + ] + }, + "sticky": { + "channelId": "1135563848586379264", + "stickyMessage": { + "title": "Read me!" + }, + "timeout": 30000 }, "server": { "port": 3000, diff --git a/apps/bot-discord/src/config.json b/apps/bot-discord/src/config.json index 38a1d15..f439602 100644 --- a/apps/bot-discord/src/config.json +++ b/apps/bot-discord/src/config.json @@ -1,9 +1,23 @@ { "discord": { - "trainRoles": ["1019903194941362198", "955220417969262612"], + "trainRoles": [ + "1019903194941362198", + "955220417969262612" + ], "botId": "1038762591805247518", "ignoreRole": "1027874293192863765", - "ignoreChannels": ["953965039105232906", "953964264400515092", "952987428786941952"] + "ignoreChannels": [ + "953965039105232906", + "953964264400515092", + "952987428786941952" + ] + }, + "sticky": { + "channelId": "1135563848586379264", + "stickyMessage": { + "title": "Read me!" + }, + "timeout": 30000 }, "server": { "port": 3000, @@ -146,4 +160,4 @@ } } ] -} +} \ No newline at end of file diff --git a/apps/bot-discord/src/events/messageCreate.js b/apps/bot-discord/src/events/messageCreate.js index 7ef779c..b766426 100644 --- a/apps/bot-discord/src/events/messageCreate.js +++ b/apps/bot-discord/src/events/messageCreate.js @@ -10,11 +10,25 @@ export default { if (msg.attachments.first() && msg.attachments.first().contentType.startsWith('image')) { helper.scanImage(msg.attachments.first().url, `${msg.channelId}/${msg.id}`); } - + if (!msg.content || msg.author.bot) return; helper.scanText( msg.content.toLowerCase().replace(/<.*?>/g, ''), `${msg.channelId}/${msg.id}` ); + + // Sticky message + if (msg.channel.id !== config.sticky.channelId) return; + if (msg.client.stickiedMessageTimeout) clearInterval(msg.client.stickiedMessageTimeout); + + msg.client.stickiedMessageTimeout = setTimeout(() => { + const channel = await msg.client.channels.fetch(config.sticky.channelId); + + const message = await channel.send({ embeds: [config.sticky.stickyMessage] }); + + if (msg.client.stickiedMessage) channel.delete(msg.client.stickiedMessage); + + msg.client.stickiedMessage = message.id; + }, config.sticky.timeout); } }; diff --git a/apps/bot-discord/src/index.js b/apps/bot-discord/src/index.js index 945d600..27d9cac 100644 --- a/apps/bot-discord/src/index.js +++ b/apps/bot-discord/src/index.js @@ -21,6 +21,8 @@ const client = new Client({ client.commands = new Collection(); client.trainingVotes = new Collection(); +client.stickiedMessage = null; +client.stickiedMessageTimeout = null; const commandsPath = join(__dirname, 'commands'); const commandFiles = readdirSync(commandsPath).filter((file) =>