From 701a18a717d558980291ec4858c1c12b38ef81f3 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Mon, 2 Jan 2023 20:45:05 +1100 Subject: [PATCH] Sync event handler from TAEMBO/IRTGaming-bot --- src/commands/purge.ts | 2 +- src/events/guildBanAdd.ts | 3 +-- src/events/guildBanRemove.ts | 3 +-- src/events/guildMemberAdd.ts | 3 +-- src/events/guildMemberRemove.ts | 3 +-- src/events/guildMemberUpdate.ts | 3 +-- src/events/interactionCreate.ts | 3 +-- src/events/inviteCreate.ts | 3 +-- src/events/inviteDelete.ts | 3 +-- src/events/messageCreate.ts | 5 ++--- src/events/messageDelete.ts | 3 +-- src/events/messageDeleteBulk.ts | 12 ++++-------- src/events/messageUpdate.ts | 3 +-- src/events/roleUpdate.ts | 3 +-- src/index.ts | 11 ++++++++--- 15 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/commands/purge.ts b/src/commands/purge.ts index f3b99aa..a392b20 100644 --- a/src/commands/purge.ts +++ b/src/commands/purge.ts @@ -10,7 +10,7 @@ export default { let messagesArray: Array = []; if (user){ - (interaction.channel as Discord.TextChannel).messages.fetch({limit: 100}).then((msgs)=>{ + (interaction.channel as Discord.TextChannel).messages.fetch({limit: amount}).then((msgs)=>{ const msgList = msgs.filter(x=>x.author.id == user.id); (interaction.channel as Discord.TextChannel).bulkDelete(msgList); }) diff --git a/src/events/guildBanAdd.ts b/src/events/guildBanAdd.ts index feb08b7..a5be463 100644 --- a/src/events/guildBanAdd.ts +++ b/src/events/guildBanAdd.ts @@ -1,8 +1,7 @@ import Discord, { AuditLogEvent } from 'discord.js'; import { TClient } from '../client'; export default { - name: 'guildBanAdd', - execute: async(client:TClient, member:Discord.GuildMember)=>{ + async run(client:TClient, member:Discord.GuildMember){ if (member.guild?.id != client.config.mainServer.id) return; const fetchBanlog = await member.guild.fetchAuditLogs({ limit: 1, diff --git a/src/events/guildBanRemove.ts b/src/events/guildBanRemove.ts index 6591db5..b05216d 100644 --- a/src/events/guildBanRemove.ts +++ b/src/events/guildBanRemove.ts @@ -1,8 +1,7 @@ import Discord, { AuditLogEvent } from 'discord.js'; import { TClient } from '../client'; export default { - name: 'guildBanRemove', - execute: async(client:TClient, member:Discord.GuildMember)=>{ + async run(client:TClient, member:Discord.GuildMember){ if (member.guild?.id != client.config.mainServer.id) return; const fetchUnbanlog = await member.guild.fetchAuditLogs({ limit: 1, diff --git a/src/events/guildMemberAdd.ts b/src/events/guildMemberAdd.ts index 3bba02d..322fc51 100644 --- a/src/events/guildMemberAdd.ts +++ b/src/events/guildMemberAdd.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'guildMemberAdd', - execute: async(client:TClient, member:Discord.GuildMember)=>{ + async run(client:TClient, member:Discord.GuildMember){ //if (member.partial) return; if ( member.partial diff --git a/src/events/guildMemberRemove.ts b/src/events/guildMemberRemove.ts index 5fe90b9..5d99b36 100644 --- a/src/events/guildMemberRemove.ts +++ b/src/events/guildMemberRemove.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'guildMemberRemove', - execute: async(client:TClient, member:Discord.GuildMember)=>{ + async run(client:TClient, member:Discord.GuildMember){ if (!client.config.botSwitches.logs) return; if ( !member.joinedTimestamp diff --git a/src/events/guildMemberUpdate.ts b/src/events/guildMemberUpdate.ts index d031522..dd83977 100644 --- a/src/events/guildMemberUpdate.ts +++ b/src/events/guildMemberUpdate.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'guildMemberUpdate', - execute: async(client:TClient, oldMember:Discord.GuildMember, newMember:Discord.GuildMember)=>{ + async run(client:TClient, oldMember:Discord.GuildMember, newMember:Discord.GuildMember){ if (oldMember.guild.id != client.config.mainServer.id) return; if (!client.config.botSwitches.logs) return; const channel = (client.channels.resolve(client.config.mainServer.channels.logs) as Discord.TextChannel) diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index 24f3129..2cabff5 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'interactionCreate', - execute: async(client:TClient, interaction:Discord.ChatInputCommandInteraction)=>{ + async run(client:TClient, interaction:Discord.ChatInputCommandInteraction){ if (!interaction.inGuild() || !interaction.inCachedGuild() || !interaction.command) return; if (interaction.isCommand()){ const commandFile = client.commands.get(interaction.commandName); diff --git a/src/events/inviteCreate.ts b/src/events/inviteCreate.ts index 492cb19..16691d5 100644 --- a/src/events/inviteCreate.ts +++ b/src/events/inviteCreate.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'inviteCreate', - execute: async(client:TClient, invite: Discord.Invite)=>{ + async run(client:TClient, invite: Discord.Invite){ if (!invite.guild) return; const newInvites = await (invite.guild as Discord.Guild).invites.fetch(); newInvites.forEach(inv=>client.invites.set(inv.code,{uses: inv.code, creator: inv.inviterId})) diff --git a/src/events/inviteDelete.ts b/src/events/inviteDelete.ts index 8965dd6..8fe2b04 100644 --- a/src/events/inviteDelete.ts +++ b/src/events/inviteDelete.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'inviteDelete', - execute: async(client:TClient, invite: Discord.Invite)=>{ + async run(client:TClient, invite: Discord.Invite){ client.invites.delete(invite.code) } } \ No newline at end of file diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 776b105..5d97e7c 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,8 +1,7 @@ import Discord, { ChannelType } from 'discord.js'; import { TClient } from '../client'; export default { - name: 'messageCreate', - execute: async(client:TClient, message:Discord.Message)=>{ + async run(client:TClient, message:Discord.Message){ if ( message.author.bot || message.channel.type === ChannelType.DM @@ -153,7 +152,7 @@ export default { message.reply(`Night **${message.member.displayName}**`) } // Failsafe thingy (Toastproof maybe) - if (message.content.startsWith('!!!_wepanikfrfr') && client.config.eval.whitelist.includes(message.author.id)){ + if (message.content.startsWith('!!_wepanikfrfr') && client.config.eval.whitelist.includes(message.author.id)){ (client.guilds.cache.get(message.guildId) as Discord.Guild).commands.set(client.registry).then(()=>message.reply('How did you manage to lose the commands??? Anyways, it\'s re-registered now.')).catch((e:Error)=>message.reply(`Failed to deploy slash commands:\n\`\`\`${e.message}\`\`\``)); } } diff --git a/src/events/messageDelete.ts b/src/events/messageDelete.ts index 7e4c265..485c965 100644 --- a/src/events/messageDelete.ts +++ b/src/events/messageDelete.ts @@ -1,8 +1,7 @@ import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'messageDelete', - execute: async(client:TClient, msg:Discord.Message)=>{ + async run(client:TClient, msg:Discord.Message){ if (!client.config.botSwitches.logs) return; const channel = client.channels.resolve(client.config.mainServer.channels.logs) as Discord.TextChannel; const disabledChannels = [ diff --git a/src/events/messageDeleteBulk.ts b/src/events/messageDeleteBulk.ts index 7e6c006..28dec23 100644 --- a/src/events/messageDeleteBulk.ts +++ b/src/events/messageDeleteBulk.ts @@ -1,15 +1,11 @@ -import Discord, { Message, Snowflake } from 'discord.js'; +import Discord from 'discord.js'; import { TClient } from '../client'; export default { - name: 'messageDeleteBulk', - execute: async(client:TClient, messages:Discord.Collection)=>{ + async run(client:TClient, messages:Discord.Collection>){ const channel = client.channels.resolve(client.config.mainServer.channels.logs) as Discord.TextChannel; if (!client.config.botSwitches.logs) return; - let msgArray: Array = []; - messages.forEach((m)=>{ - msgArray.push(`${m.author.username}: ${m.content}`); - }); - const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setTitle(`${messages.size} messages were purged`).setDescription(`\`\`\`${msgArray.reverse().join('\n')}\`\`\``.slice(0,3900)).addFields({name: 'Channel', value: `<#${(messages.first() as Discord.Message).channel.id}>`}); + if ((client.guilds.cache.get('929807948748832798') as Discord.Guild)?.id != client.config.mainServer.id) return; + const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setTitle(`${messages.size} messages were purged`).setDescription(`\`\`\`${messages.map((msgs)=>`${msgs.member.displayName}: ${msgs.content}`).reverse().join('\n').slice(0,3900)}\`\`\``).addFields({name: 'Channel', value: `<#${(messages.first() as Discord.Message).channel.id}>`}); channel.send({embeds: [embed]}) } } \ No newline at end of file diff --git a/src/events/messageUpdate.ts b/src/events/messageUpdate.ts index 96371d8..19a58b7 100644 --- a/src/events/messageUpdate.ts +++ b/src/events/messageUpdate.ts @@ -1,8 +1,7 @@ import Discord, { ActionRowBuilder, ButtonBuilder } from 'discord.js'; import { TClient } from '../client'; export default { - name: 'messageUpdate', - execute: async(client:TClient, oldMsg:Discord.Message, newMsg:Discord.Message)=>{ + async run(client:TClient, oldMsg:Discord.Message, newMsg:Discord.Message){ if (!client.config.botSwitches.logs) return; const disabledChannels = [ '548032776830582794', '541677709487505408' diff --git a/src/events/roleUpdate.ts b/src/events/roleUpdate.ts index fdf4e0d..0d23be1 100644 --- a/src/events/roleUpdate.ts +++ b/src/events/roleUpdate.ts @@ -1,8 +1,7 @@ import Discord, { AuditLogEvent } from 'discord.js'; import { TClient } from '../client'; export default { - name: 'roleUpdate', - execute: async(client:TClient, oldRole:Discord.Role, newRole:Discord.Role)=>{ + async run(client:TClient, oldRole:Discord.Role, newRole:Discord.Role){ const fetchRoleUpdoot = await client.guilds.cache.get(oldRole.guild.id).fetchAuditLogs({ limit: 1, type: AuditLogEvent.RoleUpdate diff --git a/src/index.ts b/src/index.ts index dd99395..173078d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ client.on('ready', async()=>{ }, 60000); // ['929807948748832798', '468835415093411861', '1058183358267543552', '549114074273677314'] - 0=Dev Server, 1=Main Server, 2=Throne, 3=Toast's test server if (client.config.botSwitches.registerCommands){ - ['929807948748832798', '468835415093411861', '1058183358267543552'].forEach((guildId)=>(client.guilds.cache.get(guildId) as Discord.Guild).commands.set(client.registry).catch((e:Error)=>{ + ['929807948748832798'].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.mainServer.channels.errors) as Discord.TextChannel).send(`Cannot register slash commands for **${client.guilds.cache.get(guildId).name}** (\`${guildId}\`):\n\`\`\`${e.message}\`\`\``) })); @@ -33,11 +33,16 @@ client.on('ready', async()=>{ (client.channels.resolve(client.config.mainServer.channels.bot_status) as Discord.TextChannel).send(`${client.user.username} is active\n\`\`\`json\n${Object.entries(client.config.botSwitches).map((hi)=>`${hi[0]}: ${hi[1]}`).join('\n')}\`\`\``); // Event handler - const eventFiles = fs.readdirSync('src/events').filter(file=>file.endsWith('.ts')); + fs.readdirSync('src/events').forEach((file)=>{ + const eventFile = require(`./events/${file}`); + client.on(file.replace('.ts', ''), async(...args)=>eventFile.default.run(client,...args)); + }); + + /*const eventFiles = fs.readdirSync('src/events').filter(file=>file.endsWith('.ts')); eventFiles.forEach((file)=>{ const event = require(`./events/${file}`); client.on(event.default.name, async(...args)=>event.default.execute(client, ...args)); - }); + });*/ }) // Handle errors