diff --git a/src/commands/ban.ts b/src/commands/ban.ts index d26b54d..44ce1f0 100644 --- a/src/commands/ban.ts +++ b/src/commands/ban.ts @@ -1,8 +1,6 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, Guild, GuildMember, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder, time, TimestampStyles, User } from 'discord.js'; import { Database } from '../shared/Database'; -import { AltData, comprehensiveAltLookupFromDiscord, deferInteraction } from '../utils'; - -const mentionRegex = new RegExp(MessageMentions.UsersPattern); +import { AltData, comprehensiveAltLookupFromDiscord, deferInteraction, getIdFromInput } from '../utils'; export default { name: 'ban', @@ -66,10 +64,7 @@ export default { const input = interaction.options.getString('user', true); - const matches = mentionRegex.exec(input); - mentionRegex.lastIndex = 0; - - const idToUse = matches ? matches.groups!.id : input; + const idToUse = getIdFromInput(input); const reason = interaction.options.getString('reason') ?? ''; const hours = (interaction.options.getNumber('hours') ?? 0) * 3.6e+6; diff --git a/src/commands/github-mapping.ts b/src/commands/github-mapping.ts index 2f330f5..db79f92 100644 --- a/src/commands/github-mapping.ts +++ b/src/commands/github-mapping.ts @@ -1,7 +1,6 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { Database } from '../shared/Database'; - -const mentionRegex = new RegExp(MessageMentions.UsersPattern); +import { getIdFromInput } from '../utils'; export default { name: 'github-mapping', @@ -52,10 +51,7 @@ export default { if (subcommand == 'add') { const discordUserInput = interaction.options.getString('discord-user', true); - const matches = mentionRegex.exec(discordUserInput); - mentionRegex.lastIndex = 0; - - const idToUse = matches ? matches.groups!.id : discordUserInput; + const idToUse = getIdFromInput(discordUserInput); const githubName = interaction.options.getString('github-name', true); @@ -71,10 +67,7 @@ export default { } else if (subcommand == 'remove') { const discordUserInput = interaction.options.getString('discord-user', true); - const matches = mentionRegex.exec(discordUserInput); - mentionRegex.lastIndex = 0; - - const idToUse = matches ? matches.groups!.id : discordUserInput; + const idToUse = getIdFromInput(discordUserInput); Database.removeGithubUserMapping(idToUse); return interaction.editReply('Mapping removed.'); diff --git a/src/commands/link.ts b/src/commands/link.ts index ceda1f9..f1c3242 100644 --- a/src/commands/link.ts +++ b/src/commands/link.ts @@ -1,9 +1,7 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { config } from '../config'; import { Database } from '../shared/Database'; -import { logCustomEvent, resolveUser } from '../utils'; - -const mentionRegex = new RegExp(MessageMentions.UsersPattern); +import { getIdFromInput, logCustomEvent, resolveUser } from '../utils'; export default { name: 'link', @@ -54,10 +52,7 @@ export default { const discordUserInput = interaction.options.getString('discord-user', true); - const matches = mentionRegex.exec(discordUserInput); - mentionRegex.lastIndex = 0; - - const idToUse = matches ? matches.groups!.id : discordUserInput; + const idToUse = getIdFromInput(discordUserInput); const user = await resolveUser(client, idToUse, interaction.guild); diff --git a/src/commands/notes.ts b/src/commands/notes.ts index 3957413..ce8bcba 100644 --- a/src/commands/notes.ts +++ b/src/commands/notes.ts @@ -1,10 +1,8 @@ import { ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { Database } from '../shared/Database'; -import { deferInteraction, logCustomEvent, resolveUser } from '../utils'; +import { deferInteraction, getIdFromInput, logCustomEvent, resolveUser } from '../utils'; import { getNoteMessage } from '../utils/note-utils'; -const mentionRegex = new RegExp(MessageMentions.UsersPattern); - export default { name: 'notes', data: new SlashCommandBuilder() @@ -88,10 +86,7 @@ export default { const input = interaction.options.getString('user', true); - const matches = mentionRegex.exec(input); - mentionRegex.lastIndex = 0; - - const idToUse = matches ? matches.groups!.id : input; + const idToUse = getIdFromInput(input); await deferInteraction(interaction); @@ -215,10 +210,7 @@ export default { autoComplete: async function (client: Client, interaction: AutocompleteInteraction) { const input = interaction.options.getString('user', true); - const matches = mentionRegex.exec(input); - mentionRegex.lastIndex = 0; - - const idToUse = matches ? matches.groups!.id : input; + const idToUse = getIdFromInput(input); if (!idToUse) return interaction.respond([]); diff --git a/src/commands/role-buttons.ts b/src/commands/role-buttons.ts index da695fd..aec4ea1 100644 --- a/src/commands/role-buttons.ts +++ b/src/commands/role-buttons.ts @@ -8,7 +8,7 @@ export default { .setDescription('Manage role buttons.') .setIntegrationTypes(ApplicationIntegrationType.GuildInstall) .setContexts(InteractionContextType.Guild) - .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) + .setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles) .addSubcommand(subcommand => subcommand .setName('create') diff --git a/src/commands/whois.ts b/src/commands/whois.ts index 17e77fd..103e6c1 100644 --- a/src/commands/whois.ts +++ b/src/commands/whois.ts @@ -1,7 +1,5 @@ import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; -import { channelIsInStaffCategory, handleWhoIsInteraction } from '../utils'; - -const mentionRegex = new RegExp(MessageMentions.UsersPattern); +import { channelIsInStaffCategory, getIdFromInput, handleWhoIsInteraction } from '../utils'; export default { name: 'whois', @@ -20,10 +18,7 @@ export default { handler: async function (client: Client, interaction: ChatInputCommandInteraction) { const input = interaction.options.getString('user', true); - const matches = mentionRegex.exec(input); - mentionRegex.lastIndex = 0; - - const valueToUse = matches ? matches.groups!.id : input; + const valueToUse = getIdFromInput(input); handleWhoIsInteraction(interaction, valueToUse, !(await channelIsInStaffCategory(interaction.channel as GuildBasedChannel))); } diff --git a/src/utils/discord-user-utils.ts b/src/utils/discord-user-utils.ts index 4c5accc..5d9dc75 100644 --- a/src/utils/discord-user-utils.ts +++ b/src/utils/discord-user-utils.ts @@ -1,4 +1,4 @@ -import { Client, Guild, User } from 'discord.js'; +import { Client, Guild, MessageMentions, User } from 'discord.js'; import { Database, PrivateHelpTicketStatus } from '../shared/Database'; export async function resolveUser(client: Client, value: string, guild: Guild | null = null): Promise { @@ -27,6 +27,12 @@ export async function resolveUser(client: Client, value: string, guild: Guild | return user; } +export function getIdFromInput(input: string): string { + const matches = new RegExp(MessageMentions.UsersPattern).exec(input); + + return matches ? matches.groups!.id : input; +} + export async function canOpenPrivateHelpTicket(id: string): Promise { const latestTicket = await Database.getLatestPrivateHelpTicketBy(id);