Turn this into a function

This commit is contained in:
Tarrgon
2026-07-06 21:48:52 -04:00
parent a67adb1e6c
commit 99d38c6fbd
7 changed files with 20 additions and 44 deletions
+2 -7
View File
@@ -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;
+3 -10
View File
@@ -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.');
+2 -7
View File
@@ -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);
+3 -11
View File
@@ -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([]);
+1 -1
View File
@@ -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')
+2 -7
View File
@@ -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)));
}
+7 -1
View File
@@ -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<User | null | undefined> {
@@ -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<boolean> {
const latestTicket = await Database.getLatestPrivateHelpTicketBy(id);