Turn this into a function
This commit is contained in:
+2
-7
@@ -1,8 +1,6 @@
|
|||||||
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, Guild, GuildMember, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder, time, TimestampStyles, User } from 'discord.js';
|
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, Guild, GuildMember, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder, time, TimestampStyles, User } from 'discord.js';
|
||||||
import { Database } from '../shared/Database';
|
import { Database } from '../shared/Database';
|
||||||
import { AltData, comprehensiveAltLookupFromDiscord, deferInteraction } from '../utils';
|
import { AltData, comprehensiveAltLookupFromDiscord, deferInteraction, getIdFromInput } from '../utils';
|
||||||
|
|
||||||
const mentionRegex = new RegExp(MessageMentions.UsersPattern);
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'ban',
|
name: 'ban',
|
||||||
@@ -66,10 +64,7 @@ export default {
|
|||||||
|
|
||||||
const input = interaction.options.getString('user', true);
|
const input = interaction.options.getString('user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(input);
|
const idToUse = getIdFromInput(input);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const idToUse = matches ? matches.groups!.id : input;
|
|
||||||
const reason = interaction.options.getString('reason') ?? '';
|
const reason = interaction.options.getString('reason') ?? '';
|
||||||
|
|
||||||
const hours = (interaction.options.getNumber('hours') ?? 0) * 3.6e+6;
|
const hours = (interaction.options.getNumber('hours') ?? 0) * 3.6e+6;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||||
import { Database } from '../shared/Database';
|
import { Database } from '../shared/Database';
|
||||||
|
import { getIdFromInput } from '../utils';
|
||||||
const mentionRegex = new RegExp(MessageMentions.UsersPattern);
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'github-mapping',
|
name: 'github-mapping',
|
||||||
@@ -52,10 +51,7 @@ export default {
|
|||||||
if (subcommand == 'add') {
|
if (subcommand == 'add') {
|
||||||
const discordUserInput = interaction.options.getString('discord-user', true);
|
const discordUserInput = interaction.options.getString('discord-user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(discordUserInput);
|
const idToUse = getIdFromInput(discordUserInput);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const idToUse = matches ? matches.groups!.id : discordUserInput;
|
|
||||||
|
|
||||||
const githubName = interaction.options.getString('github-name', true);
|
const githubName = interaction.options.getString('github-name', true);
|
||||||
|
|
||||||
@@ -71,10 +67,7 @@ export default {
|
|||||||
} else if (subcommand == 'remove') {
|
} else if (subcommand == 'remove') {
|
||||||
const discordUserInput = interaction.options.getString('discord-user', true);
|
const discordUserInput = interaction.options.getString('discord-user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(discordUserInput);
|
const idToUse = getIdFromInput(discordUserInput);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const idToUse = matches ? matches.groups!.id : discordUserInput;
|
|
||||||
|
|
||||||
Database.removeGithubUserMapping(idToUse);
|
Database.removeGithubUserMapping(idToUse);
|
||||||
return interaction.editReply('Mapping removed.');
|
return interaction.editReply('Mapping removed.');
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||||
import { config } from '../config';
|
import { config } from '../config';
|
||||||
import { Database } from '../shared/Database';
|
import { Database } from '../shared/Database';
|
||||||
import { logCustomEvent, resolveUser } from '../utils';
|
import { getIdFromInput, logCustomEvent, resolveUser } from '../utils';
|
||||||
|
|
||||||
const mentionRegex = new RegExp(MessageMentions.UsersPattern);
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'link',
|
name: 'link',
|
||||||
@@ -54,10 +52,7 @@ export default {
|
|||||||
|
|
||||||
const discordUserInput = interaction.options.getString('discord-user', true);
|
const discordUserInput = interaction.options.getString('discord-user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(discordUserInput);
|
const idToUse = getIdFromInput(discordUserInput);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const idToUse = matches ? matches.groups!.id : discordUserInput;
|
|
||||||
|
|
||||||
const user = await resolveUser(client, idToUse, interaction.guild);
|
const user = await resolveUser(client, idToUse, interaction.guild);
|
||||||
|
|
||||||
|
|||||||
+3
-11
@@ -1,10 +1,8 @@
|
|||||||
import { ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
import { ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||||
import { Database } from '../shared/Database';
|
import { Database } from '../shared/Database';
|
||||||
import { deferInteraction, logCustomEvent, resolveUser } from '../utils';
|
import { deferInteraction, getIdFromInput, logCustomEvent, resolveUser } from '../utils';
|
||||||
import { getNoteMessage } from '../utils/note-utils';
|
import { getNoteMessage } from '../utils/note-utils';
|
||||||
|
|
||||||
const mentionRegex = new RegExp(MessageMentions.UsersPattern);
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'notes',
|
name: 'notes',
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@@ -88,10 +86,7 @@ export default {
|
|||||||
|
|
||||||
const input = interaction.options.getString('user', true);
|
const input = interaction.options.getString('user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(input);
|
const idToUse = getIdFromInput(input);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const idToUse = matches ? matches.groups!.id : input;
|
|
||||||
|
|
||||||
await deferInteraction(interaction);
|
await deferInteraction(interaction);
|
||||||
|
|
||||||
@@ -215,10 +210,7 @@ export default {
|
|||||||
autoComplete: async function (client: Client, interaction: AutocompleteInteraction) {
|
autoComplete: async function (client: Client, interaction: AutocompleteInteraction) {
|
||||||
const input = interaction.options.getString('user', true);
|
const input = interaction.options.getString('user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(input);
|
const idToUse = getIdFromInput(input);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const idToUse = matches ? matches.groups!.id : input;
|
|
||||||
|
|
||||||
if (!idToUse) return interaction.respond([]);
|
if (!idToUse) return interaction.respond([]);
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export default {
|
|||||||
.setDescription('Manage role buttons.')
|
.setDescription('Manage role buttons.')
|
||||||
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
|
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
|
||||||
.setContexts(InteractionContextType.Guild)
|
.setContexts(InteractionContextType.Guild)
|
||||||
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
|
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
|
||||||
.addSubcommand(subcommand =>
|
.addSubcommand(subcommand =>
|
||||||
subcommand
|
subcommand
|
||||||
.setName('create')
|
.setName('create')
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageMentions, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||||
import { channelIsInStaffCategory, handleWhoIsInteraction } from '../utils';
|
import { channelIsInStaffCategory, getIdFromInput, handleWhoIsInteraction } from '../utils';
|
||||||
|
|
||||||
const mentionRegex = new RegExp(MessageMentions.UsersPattern);
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'whois',
|
name: 'whois',
|
||||||
@@ -20,10 +18,7 @@ export default {
|
|||||||
handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
|
handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
|
||||||
const input = interaction.options.getString('user', true);
|
const input = interaction.options.getString('user', true);
|
||||||
|
|
||||||
const matches = mentionRegex.exec(input);
|
const valueToUse = getIdFromInput(input);
|
||||||
mentionRegex.lastIndex = 0;
|
|
||||||
|
|
||||||
const valueToUse = matches ? matches.groups!.id : input;
|
|
||||||
|
|
||||||
handleWhoIsInteraction(interaction, valueToUse, !(await channelIsInStaffCategory(interaction.channel as GuildBasedChannel)));
|
handleWhoIsInteraction(interaction, valueToUse, !(await channelIsInStaffCategory(interaction.channel as GuildBasedChannel)));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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';
|
import { Database, PrivateHelpTicketStatus } from '../shared/Database';
|
||||||
|
|
||||||
export async function resolveUser(client: Client, value: string, guild: Guild | null = null): Promise<User | null | undefined> {
|
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;
|
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> {
|
export async function canOpenPrivateHelpTicket(id: string): Promise<boolean> {
|
||||||
const latestTicket = await Database.getLatestPrivateHelpTicketBy(id);
|
const latestTicket = await Database.getLatestPrivateHelpTicketBy(id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user