Skip to content

Commit

Permalink
feat/error trace (#363)
Browse files Browse the repository at this point in the history
* feat: error trace
  • Loading branch information
JustDams authored Jun 20, 2023
1 parent 2109beb commit 8650d1e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"invite": "https://discord.com/oauth2/authorize?client_id=838088031222235197&permissions=275280873472&scope=bot%20applications.commands",
"vote": "https://top.gg/bot/838088031222235197/vote",
"join": "https://discord.gg/uPAdfvfuTP",
"logChannel": "1120786737636048976",
"itemByPage": 25,
"color": {
"primary": "#14578c",
Expand Down
35 changes: 19 additions & 16 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ const errorCard = require('../templates/errorCard')
const { InteractionType } = require('discord.js')
const CommandsStats = require('../database/commandsStats')
const { getTranslation } = require('../languages/setup')
const errorHandler = require('../functions/error')

const editInteraction = (interaction, resp) => {
if (!resp) return
interaction.editReply(noMention(resp)).catch(console.error)
interaction.editReply(noMention(resp)).catch((error) => errorHandler(interaction, error))
}

const errorInteraction = (interaction, error, message) => {
console.error(error)
interaction.followUp(noMention(errorCard(typeof error !== 'string' ? message : error, interaction.locale))).catch(console.error)
errorHandler(interaction, error)

interaction.followUp(noMention(errorCard(typeof error !== 'string' ? message : error, interaction.locale)))
.catch((error) => errorHandler(interaction, error))
}

const updateUser = (interaction, interactionEl, json = null) => {
Expand Down Expand Up @@ -47,9 +50,9 @@ module.exports = {
content: ' ',
...errorCard('error.bot.channelNotAccessible', interaction.locale),
})
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
})
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
/**
* Check if the interaction is a selectmenu
*/
Expand All @@ -68,17 +71,17 @@ module.exports = {
.then(e => editInteraction(interaction, e))
.catch(err => errorInteraction(interaction, err, getTranslation('error.execution.selectmenu', interaction.locale)))
})
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
else
interaction
.deferReply({ ephemeral: true })
.then(() => {
CommandsStats.create(interaction.customId, 'selectmenu', interaction)
interactionSelectMenu?.execute(interaction, updateUser(interaction, interactionSelectMenu))
.then(e => interaction.editReply(noMention(e)).catch(console.error))
.then(e => interaction.editReply(noMention(e)).catch((error) => errorHandler(interaction, error)))
.catch(err => errorInteraction(interaction, err, getTranslation('error.execution.selectmenu', interaction.locale)))
})
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
}
/**
* Check if the interaction is a button
Expand All @@ -95,13 +98,13 @@ module.exports = {
interactionButton?.execute(interaction, json)
.then(e => editInteraction(interaction, e))
.catch(err => errorInteraction(interaction, err, getTranslation('error.execution.button', interaction.locale)))
}).catch(console.error)
}).catch((error) => errorHandler(interaction, error))
else
interaction.deferReply({ ephemeral: true }).then(() => {
interactionButton?.execute(interaction, updateUser(interaction, interactionButton, json))
.then(e => interaction.editReply(noMention(e)).catch(console.error))
.then(e => interaction.editReply(noMention(e)).catch((error) => errorHandler(interaction, error)))
.catch(err => errorInteraction(interaction, err, getTranslation('error.execution.button', interaction.locale)))
}).catch(console.error)
}).catch((error) => errorHandler(interaction, error))
}
/**
* Check if the interaction is a contextmenu
Expand All @@ -114,10 +117,10 @@ module.exports = {
interaction.client.contextmenus.get(interaction.commandName)?.execute(interaction)
.then(resp => interaction
.followUp(resp)
.catch(console.error))
.catch((error) => errorHandler(interaction, error)))
.catch(err => errorInteraction(interaction, err, getTranslation('error.execution.contextmenu', interaction.locale)))
})
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
/**
* Check if the interaction is a command
*/
Expand All @@ -134,15 +137,15 @@ module.exports = {
resp
.forEach(r => interaction
.followUp(r)
.catch(console.error))
.catch((error) => errorHandler(interaction, error)))
else
interaction
.followUp(resp)
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
})
.catch(err => errorInteraction(interaction, err, getTranslation('error.execution.command', interaction.locale)))
})
.catch(console.error)
.catch((error) => errorHandler(interaction, error))
}
}
}
19 changes: 19 additions & 0 deletions functions/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { logChannel } = require('../config.json')

module.exports = (interaction, error) => {
interaction.followUp().catch(() => null)

interaction.client.channels.fetch(logChannel).then(channel => {
channel.send({
content: `
\`\`\`js
Guild: ${interaction.guild.name} (${interaction.guild.id})
Channel: #${interaction.channel.name} (${interaction.channel.id})
User: ${interaction.user.tag} (${interaction.user.id})
Date: ${new Date().toLocaleString('fr-FR', { timeZone: 'Europe/Paris', timeStyle: 'short', dateStyle: 'long' })}
${Error(error).stack}
\`\`\`
`,
})
}).catch(console.error)
}
10 changes: 5 additions & 5 deletions functions/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ const setupRoles = async (client, user, guildId, remove) => {
const guildDatas = await client.guilds.fetch(guildId)
let members

if (user) members = [await guildDatas.members.fetch(user.at(0).discordId).catch(() => null)]
if (user.length > 0) members = [await guildDatas.members.fetch({ user: user.at(0).discordId, cache: false }).catch(() => null)]
else members = await guildDatas.members.fetch({ cache: false })

const roles = await GuildCustomRole.getRolesOf(guildDatas.id)

members?.forEach(async (member) => {
if (!member) return
if (!member?.user) return
let user = await User.get(member.user.id)

if (!(user.length > 0)) return
else if (user.length > 1) user = user.filter(e => e.guildId === guildDatas.id)

user = user.flat().at(0)

const playerDatas = await getFaceitPlayerDatas(user.faceitId).catch(console.error)
const playerDatas = await getFaceitPlayerDatas(user.faceitId).catch(() => null)
if (!playerDatas?.games?.csgo) return

const playerElo = playerDatas.games.csgo.faceit_elo

if (user.nickname) await member.edit({ nick: playerDatas.nickname }).catch(console.error)
if (user.nickname) await member.edit({ nick: playerDatas.nickname }).catch(() => null)

roles.forEach(async (role) => {
const removeRole = remove || playerElo < role.eloMin || playerElo > role.eloMax
Expand All @@ -44,7 +44,7 @@ const setupRoles = async (client, user, guildId, remove) => {
const updateRoles = async (client, discordId, guildId, remove = false) => {
let user, guilds

if (discordId) user = await User.get(discordId)
if (discordId) user = [await User.get(discordId)].flat()

if (guildId) guilds = [guildId].flat()
else guilds = (await GuildCustomRole.getAll()).map(e => e.guildId).filter((e, i, a) => a.indexOf(e) === i)
Expand Down
3 changes: 2 additions & 1 deletion templates/loadingCard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Discord = require('discord.js')
const { color, name } = require('../config.json')
const { getTranslation } = require('../languages/setup')
const errorHandler = require('../functions/error')

module.exports = (interaction) => {
if (!interaction.channel.permissionsFor(interaction.client.user).has('ViewChannel')) return
Expand All @@ -15,5 +16,5 @@ module.exports = (interaction) => {
],
attachments: [],
components: [],
}).catch(console.error)
}).catch((error) => errorHandler(interaction, error))
}
24 changes: 0 additions & 24 deletions updateRole.js

This file was deleted.

0 comments on commit 8650d1e

Please sign in to comment.