From fe06ca54e3a83740b489df0e95c5183b9a6c2c70 Mon Sep 17 00:00:00 2001 From: theimperious1 Date: Thu, 21 Nov 2024 08:06:35 -0500 Subject: [PATCH] fix(ai, mod, msgcmd): resolve errors --- src/discord/commands/global/d.ai.ts | 13 ++++-- src/discord/commands/guild/d.moderate.ts | 55 ++++++++++++++++-------- src/discord/events/guildBanAdd.ts | 2 +- src/discord/utils/messageCommand.ts | 6 ++- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/discord/commands/global/d.ai.ts b/src/discord/commands/global/d.ai.ts index 83d11d69..8dcab9eb 100644 --- a/src/discord/commands/global/d.ai.ts +++ b/src/discord/commands/global/d.ai.ts @@ -1123,7 +1123,12 @@ async function personasPage( // If the user is a developer in the home guild, show the buttonAiModify button const tripsitGuild = await discordClient.guilds.fetch(env.DISCORD_GUILD_ID); - const tripsitMember = await tripsitGuild.members.fetch(interaction.user.id); + let tripsitMember = null; + try { + tripsitMember = await tripsitGuild.members.fetch(interaction.user.id); + } catch (err) { + // do nothing + } const components = [ new ActionRowBuilder() @@ -1149,7 +1154,7 @@ async function personasPage( }); log.debug(F, 'Adding three buttons to personaButtons'); - if (tripsitMember.roles.cache.has(env.ROLE_DEVELOPER)) { + if (tripsitMember && tripsitMember.roles.cache.has(env.ROLE_DEVELOPER)) { components.push( new ActionRowBuilder() .addComponents(buttonAiNew, buttonAiModify, buttonAiDelete), @@ -1182,7 +1187,7 @@ async function personasPage( ); } - if (tripsitMember.roles.cache.has(env.ROLE_DEVELOPER)) { + if (tripsitMember && tripsitMember.roles.cache.has(env.ROLE_DEVELOPER)) { components.push( new ActionRowBuilder().addComponents([ menuAiPublic, @@ -1196,7 +1201,7 @@ async function personasPage( }; } - if (tripsitMember.roles.cache.has(env.ROLE_DEVELOPER)) { + if (tripsitMember && tripsitMember.roles.cache.has(env.ROLE_DEVELOPER)) { log.debug(F, 'Adding buttonAiNew to components'); components.push( new ActionRowBuilder() diff --git a/src/discord/commands/guild/d.moderate.ts b/src/discord/commands/guild/d.moderate.ts index f8ce7951..00cb47d3 100644 --- a/src/discord/commands/guild/d.moderate.ts +++ b/src/discord/commands/guild/d.moderate.ts @@ -1220,9 +1220,18 @@ async function messageUser( const messageFilter = (mi: MessageComponentInteraction) => mi.user.id === target.id; const collector = message.createMessageComponentCollector({ filter: messageFilter, time: 0 }); + // Fetch the mod thread channel once + let targetChan: TextChannel | null = null; + try { + targetChan = targetData.mod_thread_id + ? await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel + : null; + } catch (error) { + log.info(F, 'Failed to fetch mod thread. It was likely deleted.'); + } + collector.on('collect', async (mi: MessageComponentInteraction) => { if (mi.customId.startsWith('acknowledgeButton')) { - const targetChan = await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel; if (targetChan) { await targetChan.send({ embeds: [embedTemplate() @@ -1234,16 +1243,16 @@ async function messageUser( await mi.update({ components: [] }); mi.user.send('Thanks for understanding! We appreciate your cooperation and will consider this in the future!'); } else if (mi.customId.startsWith('refusalButton')) { - const targetChan = await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel; - await targetChan.send({ - embeds: [embedTemplate() - .setColor(Colors.Red) - .setDescription(`${target.username} has refused their timeout and was kicked.`)], - }); + if (targetChan) { + await targetChan.send({ + embeds: [embedTemplate() + .setColor(Colors.Red) + .setDescription(`${target.username} has refused their timeout and was kicked.`)], + }); + } // remove the components from the message await mi.update({ components: [] }); - mi.user.send(stripIndents`Thanks for admitting this, you\'ve been removed from the guild. - You can rejoin if you ever decide to cooperate.`); + mi.user.send(stripIndents`Thanks for admitting this, you\'ve been removed from the guild. You can rejoin if you ever decide to cooperate.`); await guild.members.kick(target, 'Refused to acknowledge timeout'); } }); @@ -1264,7 +1273,12 @@ export async function acknowledgeButton( update: { }, }); - const targetChan = await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel; + let targetChan: TextChannel | null = null; + try { + targetChan = targetData.mod_thread_id ? await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel : null; + } catch (error) { + log.info(F, 'Failed to fetch mod thread. It was likely deleted.'); + } if (targetChan) { await targetChan.send({ embeds: [embedTemplate() @@ -1273,8 +1287,11 @@ export async function acknowledgeButton( }); } // remove the components from the message - await interaction.update({ components: [] }); - interaction.user.send('Thanks for understanding! We appreciate your cooperation and will consider this in the future!'); + try { + await interaction.update({ components: [] }); + } catch (err) { + log.debug(F, 'Failed to remove warning components for moderation acknowledgement'); + } } export async function refusalButton( @@ -1290,19 +1307,23 @@ export async function refusalButton( update: { }, }); - const targetChan = await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel; + + let targetChan: TextChannel | null = null; + try { + targetChan = targetData.mod_thread_id ? await discordClient.channels.fetch(targetData.mod_thread_id as Snowflake) as TextChannel : null; + } catch (error) { + log.info(F, 'Failed to fetch mod thread. It was likely deleted.'); + } if (targetChan) { await targetChan.send({ embeds: [embedTemplate() - .setColor(Colors.Green) + .setColor(Colors.Red) .setDescription(`${interaction.user.username} has refused their warning and was kicked.`)], }); + await targetChan.guild.members.kick(interaction.user, 'Refused to acknowledge warning'); } // remove the components from the message await interaction.update({ components: [] }); - await interaction.user.send('Thanks for admitting this, you\'ve been removed from the guild. You can rejoin if you ever decide to cooperate.'); - - await targetChan.guild.members.kick(interaction.user, 'Refused to acknowledge warning'); } export async function moderate( diff --git a/src/discord/events/guildBanAdd.ts b/src/discord/events/guildBanAdd.ts index bfe7c0ab..4bb5242e 100644 --- a/src/discord/events/guildBanAdd.ts +++ b/src/discord/events/guildBanAdd.ts @@ -102,7 +102,7 @@ export const guildBanAdd: GuildBanAddEvent = { 5: Colors.Red, }; - log.info(F, 'attemtping to send messages'); + log.info(F, 'attempting to send messages'); await Promise.all(mutualGuilds.map(async guild => { if (!guild) return; // await sendCooperativeMessage( diff --git a/src/discord/utils/messageCommand.ts b/src/discord/utils/messageCommand.ts index fb59a46a..1b0a3d73 100644 --- a/src/discord/utils/messageCommand.ts +++ b/src/discord/utils/messageCommand.ts @@ -389,7 +389,11 @@ give people a chance to answer 😄 If no one answers in 5 minutes you can try a if (message.author.bot) return; if (message.guild.id !== env.DISCORD_GUILD_ID) return; // log.debug(F, 'Sad/lovey stuff detected'); - await message.react(heartEmojis[Math.floor(Math.random() * heartEmojis.length)]); + try { + await message.react(heartEmojis[Math.floor(Math.random() * heartEmojis.length)]); + } catch (err) { + log.info(F, `Failed to add heart reaction in ${message.guild.name}(${message.guild.id}).`); + } } if (!message.author.bot) {