Skip to content

Commit

Permalink
Improve output messages and fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusOtter committed Nov 13, 2021
1 parent 3fea6bc commit 4dd0f35
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
14 changes: 3 additions & 11 deletions src/commands/close.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { CommandInteraction, GuildMember, MessageComponentInteraction, MessageEmbed, Permissions } from "discord.js";
import { CommandInteraction, GuildMember, MessageComponentInteraction, Permissions } from "discord.js";
import { ephemeralReply, getThreadStartMessage } from "../helpers/messageHelpers";
import { NeedleCommand } from "../types/needleCommand";

Expand Down Expand Up @@ -28,7 +28,7 @@ export const command: NeedleCommand = {

const parentMessage = await getThreadStartMessage(channel);
if (!parentMessage) {
return ephemeralReply(interaction, "An unexpected error occurred.");
return ephemeralReply(interaction, "Could not find the start message of this thread.");
}

const hasChangeTitlePermissions = member.permissionsIn(channel).has(Permissions.FLAGS.MANAGE_THREADS, true);
Expand All @@ -40,15 +40,7 @@ export const command: NeedleCommand = {
return ephemeralReply(interaction, "This server already has the auto-archive duration set to one hour.");
}

const previousAutoArchiveDuration = !channel.autoArchiveDuration || channel.autoArchiveDuration === "MAX"
? ""
: ` (${channel.autoArchiveDuration / 60} hours)`;

await channel.setAutoArchiveDuration(60);
await interaction.reply({ embeds: [
new MessageEmbed()
.setTitle("This thread will be archived soon 🗃️") // :card_box:
.setDescription(`As requested by <@${member.id}>, this thread will automatically be archived when one hour passes without any new messages.\n\nThe thread's content will still be searchable with Discord's search function, and anyone will be able to un-archive it at any point in the future by simply sending a message in the thread again.\n\nIf you believe this was an error, you can ask a server moderator to undo this by setting the auto-archive duration back to what it was previously${previousAutoArchiveDuration}.`),
] });
await interaction.reply(`**This thread will be archived soon** :card_box:\n\nAs requested by <@${member.user.id}>, this thread will automatically be archived when one hour passes without any new messages.\n\nThe thread's content will still be searchable with Discord's search function, and anyone will be able to un-archive it at any point in the future by simply sending a message in the thread again.\n\nA server moderator can undo this action by manually setting the auto-archive duration back to what it was previously.`);
},
};
4 changes: 2 additions & 2 deletions src/commands/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const command: NeedleCommand = {

const parentMessage = await getThreadStartMessage(channel);
if (!parentMessage) {
return ephemeralReply(interaction, "An unexpected error occurred.");
return ephemeralReply(interaction, "Could not find the start message of this thread.");
}

const hasChangeTitlePermissions = member.permissionsIn(channel).has(Permissions.FLAGS.MANAGE_THREADS, true);
Expand All @@ -52,6 +52,6 @@ export const command: NeedleCommand = {
// Current rate limit is 2 renames per thread per 10 minutes (2021-09-17).
// If that rate limit is hit, it will wait here until it is able to rename the thread.
await channel.setName(newThreadName, `Changed by ${member.user.tag} (${member.id})`);
await ephemeralReply(interaction, `Successfully changed title from \`${oldThreadName}\` to \`${newThreadName}\`.`);
await interaction.reply(`Successfully changed title from \`${oldThreadName}\` to \`${newThreadName}\`.`);
},
};
10 changes: 8 additions & 2 deletions src/helpers/messageHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ export async function getThreadStartMessage(threadChannel: TextBasedChannels | n
const parentChannel = await threadChannel.guild?.channels.fetch(threadChannel.parentId);
if (!parentChannel?.isText()) { return null; }

// The thread's channel ID is the same as the start message's ID.
return parentChannel.messages.fetch(threadChannel.id);
// The thread's channel ID is the same as the start message's ID,
// but if the start message has been deleted this will throw an exception
return parentChannel.messages
.fetch(threadChannel.id)
.catch(() => {
console.error(`Start message has been deleted in thread "${threadChannel.name}"`);
return null;
});
}

export function ephemeralReply(interaction: BaseCommandInteraction | MessageComponentInteraction, replyContent: string): Promise<void> {
Expand Down

0 comments on commit 4dd0f35

Please sign in to comment.