From 2dba989e0284997506c4579b12a2bed7fc41ce4c Mon Sep 17 00:00:00 2001 From: Nayv <43297329+nayvcake@users.noreply.github.com> Date: Sat, 23 Dec 2023 01:42:30 -0300 Subject: [PATCH] add more error and promise handling --- src/discord/ChinoClient.platform.ts | 8 +++++++- src/discord/events/ErrorListener.ts | 8 +------- src/discord/events/GuildMemberAddListener.ts | 18 ++++++++++++------ .../events/GuildMemberRemoveListener.ts | 2 +- .../events/GuildMemberUpdateListener.ts | 16 ++++++++++++---- src/discord/events/MessageDeleteListener.ts | 6 +++++- src/discord/events/MessageUpdateListener.ts | 6 +++++- src/discord/events/ReadyListener.ts | 2 +- .../structures/commands/CommandRunner.ts | 7 ++++++- src/discord/structures/utils/BoosterUtils.ts | 6 +++++- src/index.ts | 9 ++++++++- 11 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/discord/ChinoClient.platform.ts b/src/discord/ChinoClient.platform.ts index ccfb46f..b98cb24 100644 --- a/src/discord/ChinoClient.platform.ts +++ b/src/discord/ChinoClient.platform.ts @@ -36,7 +36,13 @@ export class ChinoClient extends Client { path = join(path, 'events') readdirSync(path).forEach(async (event: string) => { const events = await import('file://' + join(path, event.split('.').at(0))) - this.on(events.default.name, (...args) => events.default.run(this, ...args)) + this.on(events.default.name, (...args) => { + try { + events.default.run(this, ...args) + } catch (error) { + console.error(error) + } + }) }) } diff --git a/src/discord/events/ErrorListener.ts b/src/discord/events/ErrorListener.ts index 37b830f..6333f6d 100644 --- a/src/discord/events/ErrorListener.ts +++ b/src/discord/events/ErrorListener.ts @@ -5,12 +5,6 @@ const logger = new Logger('DiscordPlatform.events.ErrorListener') export default { name: 'error', run: (client: ChinoClient, error: Error, shard: Shard) => { - const uselessErrors = [ - 'WebSocket was closed before the connection was established', - 'Connection reset by peer' - ] - if (!uselessErrors.includes(error.message)) { - console.error(`A error on shard ${shard}: `, error) - } + console.error(`A error on shard ${shard}: `, error) } } \ No newline at end of file diff --git a/src/discord/events/GuildMemberAddListener.ts b/src/discord/events/GuildMemberAddListener.ts index 4b66268..dc837d2 100644 --- a/src/discord/events/GuildMemberAddListener.ts +++ b/src/discord/events/GuildMemberAddListener.ts @@ -1,14 +1,18 @@ import { Guild, Member, TextChannel } from 'eris' import { ChinoClient } from '../ChinoClient.platform' -import { EmbedBuilder } from '../structures' import { Config } from '../config' +import { EmbedBuilder } from '../structures' export default { name: 'guildMemberAdd', run: async (client: ChinoClient, guild: Guild, member: Member) => { if (member.user.bot) { if (member.roles.includes(Config.guild_support.botRole)) return - member.addRole(Config.guild_support.botRole) + try { + member.addRole(Config.guild_support.botRole) + } catch (error) { + console.error(error) + } } const invites = await guild.getInvites() @@ -22,10 +26,12 @@ export default { embed.addField('🐦 Twitter', 'Check out my twitter [@ChinoKafuuBot](https://twitter.com/ChinoKafuuBot)', true) embed.addField('<:chino_chibi:574337895838777374> Add me', `Add me in your server [clicking here](https://discord.com/oauth2/authorize?client_id=${Config.guild_support.botMain}&scope=bot%20applications.commands&permissions=8560045566)`, true) embed.addField(' Invite your friends', `You can invite some of your friends copying [this invite](${guild.vanityURL !== null ? `https://discord.gg/${guild.vanityURL}` : `https://discord.gg/${invites.filter((invite) => !invite.inviter.bot && !invite.temporary)[0].code}`})`, true) + Promise.all([ + channel.edit({ + topic: `[Click to expand] ${guild.memberCount} members | Read the <#${Config.guild_support.infoChannel}> to know what is allowed or not. \n\n**INVITE TO SERVER:** If you want to know about the server invite, here it is: ${guild.vanityURL !== null ? `https://discord.gg/${guild.vanityURL}` : `https://discord.gg/${invites.filter((invite) => !invite.inviter.bot && !invite.temporary)[0].code}`}\n\n**CHINO'S INVITE:** If you want to add it to your server, here is my invite: https://discord.com/oauth2/authorize?client_id=${Config.guild_support.botMain}&scope=bot%20applications.commands&permissions=8560045566` + }), + channel.createMessage(embed.build(member.user.mention)) + ]).catch((error) => console.error(error)) - channel.edit({ - topic: `[Click to expand] ${guild.memberCount} members | Read the <#${Config.guild_support.infoChannel}> to know what is allowed or not. \n\n**INVITE TO SERVER:** If you want to know about the server invite, here it is: ${guild.vanityURL !== null ? `https://discord.gg/${guild.vanityURL}` : `https://discord.gg/${invites.filter((invite) => !invite.inviter.bot && !invite.temporary)[0].code}`}\n\n**CHINO'S INVITE:** If you want to add it to your server, here is my invite: https://discord.com/oauth2/authorize?client_id=${Config.guild_support.botMain}&scope=bot%20applications.commands&permissions=8560045566` - }) - channel.createMessage(embed.build(member.user.mention)) } } \ No newline at end of file diff --git a/src/discord/events/GuildMemberRemoveListener.ts b/src/discord/events/GuildMemberRemoveListener.ts index 5f90145..2db14a8 100644 --- a/src/discord/events/GuildMemberRemoveListener.ts +++ b/src/discord/events/GuildMemberRemoveListener.ts @@ -18,6 +18,6 @@ export default { topic: `[Click to expand] ${guild.memberCount} members | Read the <#${Config.guild_support.infoChannel}> to know what is allowed or not. \n\n**INVITE TO SERVER:** If you want to know about the server invite, here it is: ${guild.vanityURL !== null ? `https://discord.gg/${guild.vanityURL}` : guild.getInvites().then((invites) => `https://discord.gg/${invites[0].code}`)}\n\n**CHINO'S INVITE:** If you want to add it to your server, here is my invite: https://discord.com/oauth2/authorize?client_id=${Config.guild_support.botMain}&scope=bot%20applications.commands&permissions=8560045566` }), channel.createMessage(embed.build()) - ]) + ]).catch((error) => console.error(error)) } } \ No newline at end of file diff --git a/src/discord/events/GuildMemberUpdateListener.ts b/src/discord/events/GuildMemberUpdateListener.ts index 197c5a7..7a40088 100644 --- a/src/discord/events/GuildMemberUpdateListener.ts +++ b/src/discord/events/GuildMemberUpdateListener.ts @@ -1,7 +1,7 @@ -import { BoosterUtils, EmbedBuilder } from '../structures' -import { ChinoClient } from '../ChinoClient.platform' import { Guild, Member, TextChannel } from 'eris' +import { ChinoClient } from '../ChinoClient.platform' import { Config } from '../config' +import { BoosterUtils, EmbedBuilder } from '../structures' export default { name: 'guildMemberUpdate', run: async (client: ChinoClient, guild: Guild, member: Member, oldMember: Member) => { @@ -14,10 +14,18 @@ export default { const channel = guild.channels.get(Config.guild_support.eventLog) as TextChannel if ((member.nick && !oldMember.nick) || (!member.nick && oldMember.nick)) { - channel.createMessage(embed.build()) + try { + channel.createMessage(embed.build()) + } catch (error) { + console.error(error) + } } if (guild.id === Config.guild_support.id && !member.pending && !member.user.bot) member.addRole(Config.guild_support.memberRole) - BoosterUtils.start(client, guild, member) + try { + BoosterUtils.start(client, guild, member) + } catch (err) { + console.error(err) + } } } diff --git a/src/discord/events/MessageDeleteListener.ts b/src/discord/events/MessageDeleteListener.ts index 0d7d9d4..6910f7b 100644 --- a/src/discord/events/MessageDeleteListener.ts +++ b/src/discord/events/MessageDeleteListener.ts @@ -17,6 +17,10 @@ export default { const channel = message.guild.channels.get(Config.guild_support.eventLog) as TextChannel if (!message.content) return - channel.createMessage(embed.build()) + try { + channel.createMessage(embed.build()) + } catch (error) { + console.error(error) + } } } \ No newline at end of file diff --git a/src/discord/events/MessageUpdateListener.ts b/src/discord/events/MessageUpdateListener.ts index 714ccd7..240aebb 100644 --- a/src/discord/events/MessageUpdateListener.ts +++ b/src/discord/events/MessageUpdateListener.ts @@ -18,6 +18,10 @@ export default { if (oldMessage.content === message.content) return if (!message.content) return const channel = message.guild.channels.get(Config.guild_support.eventLog) as TextChannel - channel.createMessage(embed.build()) + try { + channel.createMessage(embed.build()) + } catch (error) { + console.error(error) + } } } \ No newline at end of file diff --git a/src/discord/events/ReadyListener.ts b/src/discord/events/ReadyListener.ts index bff70ea..a2edf80 100644 --- a/src/discord/events/ReadyListener.ts +++ b/src/discord/events/ReadyListener.ts @@ -43,7 +43,7 @@ export default { } else { client.editStatus('online', game) } - }, 15000)]) + }, 15000)]).catch((error) => console.error(error)) } catch (err) { logger.error(err) } diff --git a/src/discord/structures/commands/CommandRunner.ts b/src/discord/structures/commands/CommandRunner.ts index 42e29a6..1c597b9 100644 --- a/src/discord/structures/commands/CommandRunner.ts +++ b/src/discord/structures/commands/CommandRunner.ts @@ -1,6 +1,7 @@ import { Message } from 'eris' import { ChinoClient } from '../../ChinoClient.platform' + export class CommandRunner { static async start(client: ChinoClient, message: Message) { if (message.channel.id === '468880249023889408') { @@ -17,6 +18,10 @@ export class CommandRunner { const command = client.commands.get(commandName) || client.commands.get(client.aliases.get(commandName)) if (command.config.dev && !process.env.DISCORD_BOT_DEVELOPERS.trim().split(',').includes(message.author.id)) return - command.run({ client, message, args }) + try { + command.run({ client, message, args }) + } catch (error) { + console.error(error) + } } } diff --git a/src/discord/structures/utils/BoosterUtils.ts b/src/discord/structures/utils/BoosterUtils.ts index 1de4bd8..a16b7c5 100644 --- a/src/discord/structures/utils/BoosterUtils.ts +++ b/src/discord/structures/utils/BoosterUtils.ts @@ -30,7 +30,11 @@ export class BoosterUtils { } } else { if (member.roles.includes(Config.guild_support.booster.donateRoleID)) { - member.removeRole(Config.guild_support.booster.donateRoleID) + try { + member.removeRole(Config.guild_support.booster.donateRoleID) + } catch (error) { + console.error(error) + } } } } diff --git a/src/index.ts b/src/index.ts index f4644ea..a9db565 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,9 @@ import 'dotenv/config' -import('./Launcher') \ No newline at end of file +import { Logger } from './Logger' +import('./Launcher') + +const logger = new Logger('Launcher') + +process + .on('uncaughtException', (exception) => logger.error(exception)) + .on('uncaughtExceptionMonitor', (exception) => logger.error(exception)) \ No newline at end of file