Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sticky messages #125

Merged
merged 5 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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