Skip to content

Commit

Permalink
fix(ai, mod, msgcmd): resolve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
theimperious1 committed Nov 21, 2024
1 parent 16b759e commit fe06ca5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
13 changes: 9 additions & 4 deletions src/discord/commands/global/d.ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ButtonBuilder>()
Expand All @@ -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<ButtonBuilder>()
.addComponents(buttonAiNew, buttonAiModify, buttonAiDelete),
Expand Down Expand Up @@ -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<StringSelectMenuBuilder>().addComponents([
menuAiPublic,
Expand All @@ -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<ButtonBuilder>()
Expand Down
55 changes: 38 additions & 17 deletions src/discord/commands/guild/d.moderate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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');
}
});
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/discord/events/guildBanAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion src/discord/utils/messageCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fe06ca5

Please sign in to comment.