diff --git a/src/commands/finduser.ts b/src/commands/finduser.ts index 65b1196..c1d647c 100644 --- a/src/commands/finduser.ts +++ b/src/commands/finduser.ts @@ -1,6 +1,6 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { Database } from '../shared/Database'; -import { channelIsInStaffCategory, getDiscordAlts, getE621User } from '../utils'; +import { channelIsInStaffCategory, deferInteraction, getDiscordAlts, getE621User } from '../utils'; import { config } from '../config'; export default { @@ -24,10 +24,7 @@ export default { .setRequired(false) ), handler: async function (client: Client, interaction: ChatInputCommandInteraction) { - const isStaffChannel = await channelIsInStaffCategory(interaction.channel as GuildBasedChannel); - - if (isStaffChannel) await interaction.deferReply(); - else await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); + await deferInteraction(interaction); if (!interaction.guild) return interaction.editReply('This command must be used in a server'); diff --git a/src/commands/notes.ts b/src/commands/notes.ts index 289d8bc..57ddb8a 100644 --- a/src/commands/notes.ts +++ b/src/commands/notes.ts @@ -1,6 +1,6 @@ import { ApplicationCommandOptionType, ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { Database } from '../shared/Database'; -import { channelIsInStaffCategory } from '../utils'; +import { channelIsInStaffCategory, deferInteraction } from '../utils'; import { getNoteMessage } from '../utils/note-utils'; export default { @@ -61,10 +61,7 @@ export default { const subcommand = interaction.options.getSubcommand(true); const user = interaction.options.getUser('user', true); - const isStaffChannel = await channelIsInStaffCategory(interaction.channel as GuildBasedChannel); - - if (isStaffChannel) await interaction.deferReply(); - else await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); + await deferInteraction(interaction); if (subcommand == 'add') { const reason = interaction.options.getString('reason', true); diff --git a/src/commands/records.ts b/src/commands/records.ts index 0c87a8a..fe58be9 100644 --- a/src/commands/records.ts +++ b/src/commands/records.ts @@ -1,5 +1,5 @@ import { ApplicationIntegrationType, BitFieldResolvable, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; -import { channelIsInStaffCategory, handleWhoIsInteraction } from '../utils'; +import { channelIsInStaffCategory, deferInteraction, handleWhoIsInteraction } from '../utils'; import { getRecordMessageFromDiscordId } from '../utils/record-utils'; export default { @@ -23,10 +23,7 @@ export default { .setRequired(false) ), handler: async function (client: Client, interaction: ChatInputCommandInteraction) { - const isStaffChannel = await channelIsInStaffCategory(interaction.channel as GuildBasedChannel); - - if (isStaffChannel) await interaction.deferReply(); - else await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); + await deferInteraction(interaction); if (!interaction.guild) return interaction.editReply('This command must be used in a server'); diff --git a/src/context-menus/records.ts b/src/context-menus/records.ts new file mode 100644 index 0000000..bf8e5f6 --- /dev/null +++ b/src/context-menus/records.ts @@ -0,0 +1,23 @@ +import { ApplicationIntegrationType, Client, InteractionContextType, PermissionFlagsBits, ContextMenuCommandBuilder, ApplicationCommandType, UserContextMenuCommandInteraction, GuildBasedChannel, MessageFlags } from 'discord.js'; +import { channelIsInStaffCategory, deferInteraction, getRecordMessageFromDiscordId, handleWhoIsInteraction } from '../utils'; + +export default { + name: 'Get Records', + data: new ContextMenuCommandBuilder() + .setName('Get Records') + .setIntegrationTypes(ApplicationIntegrationType.GuildInstall) + .setContexts(InteractionContextType.Guild) + .setDefaultMemberPermissions(PermissionFlagsBits.BanMembers) + .setType(ApplicationCommandType.User), + handler: async function (client: Client, interaction: UserContextMenuCommandInteraction) { + await deferInteraction(interaction); + + const idToUse = interaction.targetUser.id; + + const recordMessage = await getRecordMessageFromDiscordId(idToUse, 1, interaction.guild!); + + if (!recordMessage) return interaction.editReply('No records found on any linked accounts.'); + + interaction.editReply(recordMessage); + } +}; \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index 8ea839f..c3ed727 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,6 +4,7 @@ export * from './audit-log-utils'; export * from './channel-utils'; export * from './commands'; export * from './e621-utils'; +export * from './interaction-utils'; export * from './message-logger'; export * from './message-utils'; export * from './ms-to-human'; diff --git a/src/utils/interaction-utils.ts b/src/utils/interaction-utils.ts new file mode 100644 index 0000000..2c3a47e --- /dev/null +++ b/src/utils/interaction-utils.ts @@ -0,0 +1,9 @@ +import { ChatInputCommandInteraction, ContextMenuCommandInteraction, GuildBasedChannel, MessageFlags } from 'discord.js'; +import { channelIsInStaffCategory } from './channel-utils'; + +export async function deferInteraction(interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction) { + const isStaffChannel = await channelIsInStaffCategory(interaction.channel as GuildBasedChannel); + + if (isStaffChannel) await interaction.deferReply(); + else await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); +} \ No newline at end of file