Skip to content

Commit

Permalink
Add sticky messages (#125)
Browse files Browse the repository at this point in the history
* Add untested sticky messages module

* Some sticky fixes

* My mistakes xD

* Remove unused client
  • Loading branch information
DEVTomatoCake authored Sep 17, 2023
1 parent 0662a69 commit e7fd5d1
Show file tree
Hide file tree
Showing 4 changed files with 2,096 additions and 1,220 deletions.
45 changes: 45 additions & 0 deletions modules/sticky-messages/configs/sticky-messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"description": {
"en": "Manage the sticky messages here",
"de": "Passe hier die Sticky-Nachrichten an"
},
"humanName": {
"en": "Sticky messages",
"de": "Sticky-Nachrichten"
},
"filename": "sticky-messages.json",
"configElements": true,
"content": [
{
"name": "channelId",
"humanName": {
"en": "Channel",
"de": "Kanal"
},
"default": {
"en": ""
},
"description": {
"en": "Channel-ID in which the message should get send",
"de": "Kanal-ID, in welchem die Nachricht gesendet werden soll"
},
"type": "channelID"
},
{
"name": "message",
"humanName": {
"en": "Message",
"de": "Nachricht"
},
"default": {
"en": ""
},
"description": {
"en": "Message that should get send",
"de": "Nachricht, die gesendet werden soll"
},
"type": "string",
"allowEmbed": true
}
]
}
63 changes: 63 additions & 0 deletions modules/sticky-messages/events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { embedTypeV2 } = require('../../../src/functions/helpers');
const channelData = {};

/**
* Deletes the sticky message sent by the bot
* @param {Discord.Client} client
* @param {Discord.Message} msg
*/
async function deleteMessage(client, msg) {
let message;
message = await msg.channel.messages.fetch(channelData[msg.channel.id].msg).catch(async () => {
const msgs = await msg.channel.messages.fetch({limit: 20});
message = msgs.find(m => m.author.id === client.user.id);
});

if (message) message.delete().catch(() => {});
}

/**
* Sends the message to the channel
* @param {Discord.Message} msg
* @param {Object} configMsg The configured message
*/
async function sendMessage(msg, configMsg) {
channelData[msg.channel.id] = {
msg: null,
timeout: null,
time: Date.now()
};
const sentMessage = await msg.channel.send(await embedTypeV2(configMsg));
channelData[msg.channel.id] = {
msg: sentMessage.id,
timeout: null,
time: Date.now()
};
}

module.exports.run = async (client, msg) => {
if (!client.botReadyAt) return;
if (!msg.guild) return;
if (msg.guild.id !== client.guildID) return;
if (!msg.member) return;
if (msg.author.bot) return;

const stickyChannels = client.configurations['sticky-messages']['sticky-messages'];
if (!stickyChannels) return;

const currentConfig = stickyChannels.find(c => c.channelId === msg.channel.id);
if (!currentConfig || !currentConfig.message) return;

if (channelData[msg.channel.id]) {
if (channelData[msg.channel.id].time + 5000 > Date.now()) {
if (!channelData[msg.channel.id].timeout) channelData[msg.channel.id].timeout = setTimeout(() => {
deleteMessage(client, msg);
sendMessage(msg, currentConfig.message);
}, 5000);
return;
}

deleteMessage(client, msg);
sendMessage(msg, currentConfig.message);
} else sendMessage(msg, currentConfig.message);
};
24 changes: 24 additions & 0 deletions modules/sticky-messages/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "sticky-messages",
"humanReadableName": {
"en": "Sticky messages",
"de": "Sticky-Nachrichten"
},
"author": {
"scnxOrgID": "60",
"name": "TomatoCake",
"link": "https://github.com/DEVTomatoCake"
},
"description": {
"en": "Let a set message always appear at the end of a channel.",
"de": "Lasse eine festgelegte Nachricht immer am Ende eines Kanals erscheinen."
},
"events-dir": "/events",
"config-example-files": [
"configs/sticky-messages.json"
],
"tags": [
"community"
],
"openSourceURL": "https://github.com/DEVTomatoCake/ScootKit-CustomBot/tree/main/modules/sticky-messages"
}
Loading

0 comments on commit e7fd5d1

Please sign in to comment.