Skip to content

Commit

Permalink
add more error and promise handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nayvcake committed Dec 23, 2023
1 parent 4551048 commit 2dba989
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 25 deletions.
8 changes: 7 additions & 1 deletion src/discord/ChinoClient.platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
}

Expand Down
8 changes: 1 addition & 7 deletions src/discord/events/ErrorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
18 changes: 12 additions & 6 deletions src/discord/events/GuildMemberAddListener.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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('<a:chino_petpet:760564885280784405> 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))
}
}
2 changes: 1 addition & 1 deletion src/discord/events/GuildMemberRemoveListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
16 changes: 12 additions & 4 deletions src/discord/events/GuildMemberUpdateListener.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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)
}
}
}
6 changes: 5 additions & 1 deletion src/discord/events/MessageDeleteListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 5 additions & 1 deletion src/discord/events/MessageUpdateListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion src/discord/events/ReadyListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
} else {
client.editStatus('online', game)
}
}, 15000)])
}, 15000)]).catch((error) => console.error(error))
} catch (err) {
logger.error(err)
}
Expand Down
7 changes: 6 additions & 1 deletion src/discord/structures/commands/CommandRunner.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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)
}
}
}
6 changes: 5 additions & 1 deletion src/discord/structures/utils/BoosterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
import 'dotenv/config'
import('./Launcher')
import { Logger } from './Logger'
import('./Launcher')

const logger = new Logger('Launcher')

process
.on('uncaughtException', (exception) => logger.error(exception))
.on('uncaughtExceptionMonitor', (exception) => logger.error(exception))

0 comments on commit 2dba989

Please sign in to comment.