From b9ecfe23e11e067c7bdb1ed0463d8282f5b8875b Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Thu, 1 Feb 2024 07:54:38 +1100 Subject: [PATCH] Some improvements --- src/components/Automod.ts | 18 ------------------ src/events/guildBanAdd.ts | 1 - src/events/guildMemberAdd.ts | 5 +++-- src/events/guildMemberRemove.ts | 7 +++---- src/events/messageCreate.ts | 20 +++++++++++--------- src/events/messageDelete.ts | 6 +++--- src/events/messageUpdate.ts | 2 +- src/events/ready.ts | 9 ++++----- src/index.ts | 3 +-- 9 files changed, 26 insertions(+), 45 deletions(-) diff --git a/src/components/Automod.ts b/src/components/Automod.ts index 285b270..de6dc29 100644 --- a/src/components/Automod.ts +++ b/src/components/Automod.ts @@ -35,24 +35,6 @@ export default class Automoderator { clearTimeout(data.timeout); data.timeout = setTimeout(()=>delete client.repeatedMessages[message.author.id], thresholdTime); } - }// Explanation: If spammer sends same message 5 or more times in every channel, they will be served a rotten fish and chips for dinner. - static async crosspostSpam(client:TClient, message:Discord.Message, reason:string) { - if (!client.crosspostSpam[message.author.id]) client.crosspostSpam[message.author.id] = {message: message.content, channelId: [message.channelId]}; - else if (!client.crosspostSpam[message.author.id].channelId.includes(message.channelId)) client.crosspostSpam[message.author.id].channelId.push(message.channelId); - - if (client.crosspostSpam[message.author.id].channelId.length >= 5) { - if (!this.lockQuery.has(message.author.id)) { - this.lockQuery.add(message.author.id); - Logger.console('log', 'AUTOMOD', `Lock acquired for ${message.author.tag} with reason: ${reason}`); - await client.punishments.punishmentAdd('ban', {}, client.user.id, `AUTOMOD:${reason}`, message.author, message.member as Discord.GuildMember); - setTimeout(()=>{ - this.lockQuery.delete(message.author.id); - Logger.console('log', 'AUTOMOD', `Lock released for ${message.author.tag}`); - }, 5000); // Wait 5 seconds before releasing the lock. - } - delete client.crosspostSpam[message.author.id]; - } - setTimeout(()=>delete client.crosspostSpam[message.author.id], 180000); // Delete the data after 3 minutes. } static async imageOnly(message:Discord.Message) { const io_channels = ['468896467688620032']; diff --git a/src/events/guildBanAdd.ts b/src/events/guildBanAdd.ts index 3517b8d..161061e 100644 --- a/src/events/guildBanAdd.ts +++ b/src/events/guildBanAdd.ts @@ -9,7 +9,6 @@ export default class GuildBanAdd { if (target.id === member.user.id) { const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setThumbnail(member.user.displayAvatarURL({size: 2048})) .setTitle(`Member Banned: ${target.username}`).addFields( - {name: '🔹 User', value: `<@${target.id}>\n\`${target.id}\``}, {name: '🔹 Moderator', value: `<@${executor.id}>\n\`${executor.id}\``}, {name: '🔹 Reason', value: reason === null ? 'Reason unspecified': reason} ); diff --git a/src/events/guildMemberAdd.ts b/src/events/guildMemberAdd.ts index 6f8a3e3..3aae464 100644 --- a/src/events/guildMemberAdd.ts +++ b/src/events/guildMemberAdd.ts @@ -17,12 +17,13 @@ export default class GuildMemberAdd { let isBot = 'Bot'; if (!member.user.bot) isBot = 'Member'; if (!client.config.botSwitches.logs) return; - (client.channels.resolve(client.config.dcServer.channels.welcome) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setThumbnail(member.user.displayAvatarURL({size: 2048}) || member.user.defaultAvatarURL).setTitle(`Welcome to ${member.guild.name}, ${member.user.username}!`).setFooter({text: `${index}${suffix} member`})]}) + (client.channels.resolve(client.config.dcServer.channels.welcome) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setThumbnail(member.user.displayAvatarURL({size: 2048}) || member.user.defaultAvatarURL).setTitle(`Welcome to ${member.guild.name}, ${member.user.username}!`).setFooter({text: `${index}${suffix} member`})]}); + const newInvites = await member.guild.invites.fetch(); const usedInvite = newInvites.find((inv:Discord.Invite)=>client.invites.get(inv.code)?.uses < inv.uses); newInvites.forEach((inv:Discord.Invite)=>client.invites.set(inv.code,{uses: inv.uses, creator: inv.inviterId, channel: inv.channel.name})); (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({embeds: [ - new client.embed().setColor(client.config.embedColorGreen).setTimestamp().setThumbnail(member.user.displayAvatarURL({size: 2048})).setTitle(`${isBot} Joined: ${member.user.username}`).setDescription(`<@${member.user.id}>\n\`${member.user.id}\``).setFooter({text: `Total members: ${index}${suffix}`}).addFields( + new client.embed().setColor(client.config.embedColorGreen).setTimestamp().setThumbnail(member.user.displayAvatarURL({size: 2048})).setTitle(`${isBot} Joined: ${member.user.username}`).setFooter({text: `Total members: ${index}${suffix}`}).addFields( {name: '🔹 Account Creation Date', value: `\n`}, {name: '🔹 Invite Data:', value: usedInvite ? `Invite: \`${usedInvite.code}\`\nCreated by: **${usedInvite.inviter?.username}**\nChannel: **#${usedInvite.channel.name}**` : 'No invite data could be fetched.'} )]}); diff --git a/src/events/guildMemberRemove.ts b/src/events/guildMemberRemove.ts index 29f36f3..f354d24 100644 --- a/src/events/guildMemberRemove.ts +++ b/src/events/guildMemberRemove.ts @@ -1,7 +1,7 @@ import Discord from 'discord.js'; import TClient from '../client.js'; export default class GuildMemberRemove { - static async run(client:TClient, member:Discord.GuildMember){ + static async run(client:TClient, member:Discord.GuildMember) { if (!client.config.botSwitches.logs) return; if (!member.joinedTimestamp || member.guild?.id != client.config.dcServer.id) return; if (client.guilds.cache.get(client.config.dcServer.id).bans.cache.has(member.id)) { @@ -11,9 +11,8 @@ export default class GuildMemberRemove { let isBot = 'Bot'; if (!member.user.bot) isBot = 'Member'; const levelData = await client.userLevels.fetchUser(member.id); - const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setThumbnail(member.user.displayAvatarURL({size: 2048}) as string) - .setTitle(`${isBot} Left: ${member.user.username}`).addFields( - {name: '🔹 User', value: `<@${member.user.id}>\n\`${member.user.id}\``}, + const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setThumbnail(member.user.displayAvatarURL({size: 2048})) + .setTitle(`${isBot} Left: ${member.user.username}`).setFooter({text: `ID: ${member.user.id}`}).addFields( {name: '🔹 Account Creation Date', value: `\n`}, {name: '🔹 Server Join Date', value: `\n`}, {name: `🔹 Roles: ${member.roles.cache.size - 1}`, value: `${member.roles.cache.size > 1 ? member.roles.cache.filter((x)=>x.id !== member.guild.roles.everyone.id).sort((a,b)=>b.position - a.position).map(x=>x).join(member.roles.cache.size > 4 ? ' ' : '\n').slice(0,1024) : 'No roles'}`, inline: true} diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 5796b71..09a7d15 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -7,9 +7,9 @@ import ConfigHelper from '../helpers/ConfigHelper.js'; import Automoderator from '../components/Automod.js'; import MessageTool from '../helpers/MessageTool.js'; export default class MessageCreate { - static async run(client:TClient, message:Discord.Message){ + static async run(client:TClient, message:Discord.Message) { if (message.author.bot) return; - if (!message.inGuild()) return (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({content: `<:fish_unamused:1083675172407623711> ${MessageTool.formatMention(client.config.whitelist[0], 'user')}\n**${message.author.username}** (\`${message.author.id}\`) tried to send me a DM, their message is:\`\`\`${message.content}\`\`\``, allowedMentions: {parse: ['users']}}); + if (!message.inGuild()) return (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({content: `${this.randomEmotes[Math.floor(Math.random()*this.randomEmotes.length)]} ${MessageTool.formatMention(client.config.whitelist[0], 'user')}\n**${message.author.username}** (\`${message.author.id}\`) sent me a DM, their message is:\`\`\`${message.content}\`\`\``, allowedMentions: {parse: ['users']}}); let automodded: boolean; if (client.config.botSwitches.automod && !message.member.roles.cache.has(client.config.dcServer.roles.dcmod) && !message.member.roles.cache.has(client.config.dcServer.roles.admin) && message.guildId === client.config.dcServer.id) { @@ -39,13 +39,6 @@ export default class MessageCreate { imageOnly: { check: ()=>!MessageTool.isStaff(message.member as Discord.GuildMember), action: async()=>await Automoderator.imageOnly(message) - }, - crosspost: { - check: ()=>!MessageTool.isStaff(message.member as Discord.GuildMember) && message.content.toLowerCase(), - action: async()=>{ - automodded = true; - await Automoderator.crosspostSpam(client, message, 'Crosspost spam'); - } } }; @@ -124,4 +117,13 @@ export default class MessageCreate { } } } + private static randomEmotes = [ + '<:_:1083675172407623711>', '<:_:1201440990473506857>', + '<:_:1083675175163277383>', '<:_:1083675149347340391>', + '<:_:1139410139653353533>', '<:_:1083675155504574487>', + '<:_:1201441007271690340>', '<:_:1060388693166264380>', + '<:_:1084392085047758930>', '<:_:1159495459082092655>', + '', '', + '' + ]; } diff --git a/src/events/messageDelete.ts b/src/events/messageDelete.ts index 11f457f..14ed391 100644 --- a/src/events/messageDelete.ts +++ b/src/events/messageDelete.ts @@ -7,11 +7,11 @@ export default class MessageDelete { if (!client.config.botSwitches.logs) return; if (msg.guild?.id != client.config.dcServer.id || msg.partial || msg.author.bot || disabledChannels.includes(msg.channelId)) return; if (Discord.DiscordAPIError.name === '10008') return Logger.console('log', 'MsgDelete', 'Caught an unexpected error returned by Discord API. (Unknown Message)'); - const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setAuthor({name: `Author: ${msg.author.username} (${msg.author.id})`, iconURL: `${msg.author.displayAvatarURL()}`}).setTitle('Message deleted').setDescription(`<@${msg.author.id}>\n\`${msg.author.id}\``); + const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setAuthor({name: `Author: ${msg.author.username} (${msg.author.id})`, iconURL: `${msg.author.displayAvatarURL()}`}).setTitle('Message deleted'); if (msg.content.length != 0) embed.addFields({name: 'Content', value: `\`\`\`\n${Discord.escapeCodeBlock(msg.content.slice(0,1000))}\n\`\`\``}); embed.addFields( - { name: 'Channel', value: `<#${msg.channelId}>` }, - { name: 'Sent at', value: `\n` } + {name: 'Channel', value: `<#${msg.channelId}>`}, + {name: 'Sent at', value: `\n`} ) const attachments:string[] = []; msg.attachments.forEach(x=>attachments.push(x.url)); diff --git a/src/events/messageUpdate.ts b/src/events/messageUpdate.ts index 5b38800..f50bd48 100644 --- a/src/events/messageUpdate.ts +++ b/src/events/messageUpdate.ts @@ -9,7 +9,7 @@ export default class MessageUpdate { if (await client.prohibitedWords.findWord(newMsg.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]|[]|ing\b/g, '').split(' ').join('')) && (!MessageTool.isStaff(newMsg.member))) newMsg.delete(); if (!rawSwitches.MESSAGE_UPDATE || (rawSwitches.MESSAGE_UPDATE && newMsg.content !== oldMsg.content)) { rawSwitches.MESSAGE_UPDATE = true; - (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setTimestamp().setAuthor({name: `Author: ${oldMsg.author.username} (${oldMsg.author.id})`, iconURL: oldMsg.author.displayAvatarURL()}).setTitle('Message edited').setDescription(`<@${oldMsg.author.id}>\n\`${oldMsg.author.id}\``).addFields({name: 'Old content', value: `\`\`\`${oldMsg.content.length < 1 ? '(Attachment)' : Discord.escapeCodeBlock(oldMsg.content.slice(0,2048))}\`\`\``}, {name: 'New content', value: `\`\`\`${Discord.escapeCodeBlock(newMsg.content.slice(0,2048))}\`\`\``}, {name: 'Channel', value: `<#${oldMsg.channelId}>`})], components: [new Discord.ActionRowBuilder().addComponents(new Discord.ButtonBuilder().setStyle(5).setURL(oldMsg.url).setLabel('Jump to message'))]}); + (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setTimestamp().setAuthor({name: `Author: ${oldMsg.author.username} (${oldMsg.author.id})`, iconURL: oldMsg.author.displayAvatarURL()}).setTitle('Message edited').addFields({name: 'Old content', value: `\`\`\`${oldMsg.content.length < 1 ? '(Attachment)' : Discord.escapeCodeBlock(oldMsg.content.slice(0,2048))}\`\`\``}, {name: 'New content', value: `\`\`\`${Discord.escapeCodeBlock(newMsg.content.slice(0,2048))}\`\`\``}, {name: 'Channel', value: `<#${oldMsg.channelId}>`})], components: [new Discord.ActionRowBuilder().addComponents(new Discord.ButtonBuilder().setStyle(5).setURL(oldMsg.url).setLabel('Jump to message'))]}); } } } diff --git a/src/events/ready.ts b/src/events/ready.ts index cab9360..131822f 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -16,18 +16,17 @@ export default class Ready { setInterval(()=>{ client.user.setPresence(client.config.botPresence); guild.invites.fetch().then(invites=>invites.forEach(inv=>client.invites.set(inv.code, {uses: inv.uses, creator: inv.inviterId, channel: inv.channel.name}))) - },300000) + }, 300000) }) - if (client.config.botSwitches.registerCommands){ - console.log('Total commands: '+client.registry.length) //Debugging reasons. + if (client.config.botSwitches.registerCommands) { client.config.whitelistedServers.forEach(guildId=>(client.guilds.cache.get(guildId) as Discord.Guild).commands.set(client.registry).catch((e:Error)=>{ console.log(`Couldn't register slash commands for ${guildId} because`, e.stack); (client.channels.resolve(client.config.dcServer.channels.errors) as Discord.TextChannel).send(`Cannot register slash commands for **${client.guilds.cache.get(guildId).name}** (\`${guildId}\`):\n\`\`\`${e.message}\`\`\``) })) } - console.log(`${client.user.username} has logged into Discord API`); + console.log(`Ready as ${client.user.tag}`); console.log(client.config.botSwitches, client.config.whitelistedServers); - (client.channels.resolve(client.config.dcServer.channels.bot_status) as Discord.TextChannel).send({content: `**${client.user.username}** is active`, embeds:[new client.embed().setColor(client.config.embedColor).setDescription(`**\`\`\`ansi\n${botSwitches}\n\`\`\`**`)]}); + (client.channels.resolve(client.config.dcServer.channels.bot_status) as Discord.TextChannel).send({embeds:[new client.embed().setColor(client.config.embedColor).setDescription(`**\`\`\`ansi\n${botSwitches}\n\`\`\`**`)]}); console.timeEnd('Startup') } } diff --git a/src/index.ts b/src/index.ts index 1667a56..249af9e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,9 +115,8 @@ client.on('raw', async (packet:RawGatewayPacket)=>{ embeds: [new client.embed() .setColor(client.config.embedColorRed) .setTitle('Message deleted') - .setDescription('Unknown author') .addFields( - {name: 'Received over raw API gateway', value: '\u200b'}, + {name: 'This was received over raw API event', value: '\u200b'}, {name: 'Channel', value: `<#${packet.d.channel_id}>`}, ).setTimestamp() ]