From 33a6ba893bb8b9051516323b93a1eb1e1e28a884 Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Fri, 30 May 2025 18:39:47 -0400 Subject: [PATCH] Make finduser and whois recursive as well --- src/commands/finduser.ts | 9 ++++--- src/events/handle-member-join.ts | 43 +----------------------------- src/utils/alt-utils.ts | 45 ++++++++++++++++++++++++++++++++ src/utils/index.ts | 3 ++- src/utils/whois.ts | 28 +++----------------- 5 files changed, 57 insertions(+), 71 deletions(-) create mode 100644 src/utils/alt-utils.ts diff --git a/src/commands/finduser.ts b/src/commands/finduser.ts index 080764c..65b1196 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, getE621User } from '../utils'; +import { channelIsInStaffCategory, getDiscordAlts, getE621User } from '../utils'; import { config } from '../config'; export default { @@ -29,6 +29,8 @@ export default { if (isStaffChannel) await interaction.deferReply(); else await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); + if (!interaction.guild) return interaction.editReply('This command must be used in a server'); + const username = interaction.options.getString('username'); const id = interaction.options.getString('id'); @@ -43,10 +45,9 @@ export default { return interaction.editReply('I got lost along the way. Who again?'); } - const results = await Database.getDiscordIds(e621User.id); + const content = await getDiscordAlts(e621User.id, interaction.guild, 1, [e621User.id]); - const mappedResults = results.map(id => `- <@${id}>\n`); - interaction.editReply(`[${e621User.name}](${config.E621_BASE_URL}/users/${e621User.id})<${e621User.id}>'s discord account(s):\n${mappedResults}`); + interaction.editReply(`[${e621User.name}](${config.E621_BASE_URL}/users/${e621User.id})<${e621User.id}>'s e621 and discord account(s):\n${content}`); } catch (e) { console.error(e); diff --git a/src/events/handle-member-join.ts b/src/events/handle-member-join.ts index 9cb121a..a10fd69 100644 --- a/src/events/handle-member-join.ts +++ b/src/events/handle-member-join.ts @@ -1,7 +1,7 @@ import { Guild, GuildMember, GuildTextBasedChannel } from 'discord.js'; import { Database } from '../shared/Database'; import { config } from '../config'; -import { userIsBanned } from '../utils'; +import { getE621Alts, userIsBanned } from '../utils'; export async function handleMemberJoin(member: GuildMember) { const guildSettings = await Database.getGuildSettings(member.guild.id); @@ -15,45 +15,4 @@ export async function handleMemberJoin(member: GuildMember) { channel.send(content).catch(console.error); } } -} - -async function getE621Alts(discordId: string, guild: Guild, depth = 1, ignore: number[] = []): Promise { - const e621UserIds = await Database.getE621Ids(discordId); - - const toIgnore = ignore.concat(e621UserIds); - - let content = ''; - - for (const e621Id of e621UserIds) { - if (ignore.includes(e621Id)) continue; - - const alts = await getDiscordAlts(e621Id, guild, depth + 1, toIgnore); - - const banned = await userIsBanned(e621Id); - - content += `${'- '.repeat(depth)}${config.E621_BASE_URL}/users/${e621Id}${banned ? ' [BANNED]' : ''}\n${alts}`; - } - - return content; -} - -async function getDiscordAlts(e621Id: number, guild: Guild, depth = 1, ignore: number[] = []): Promise { - const discordIds = await Database.getDiscordIds(e621Id); - - let content = ''; - - for (const discordId of discordIds) { - const alts = await getE621Alts(discordId, guild, depth + 1, ignore); - - let banned = false; - - // It's either this or fetch all the bans and sift through them for every discord alt. - try { - banned = !!(await guild.bans.fetch(discordId)); - } catch (e) { } - - if (alts || banned) content += `${'- '.repeat(depth)}<@${discordId}>${banned ? ' [BANNED]' : ''}\n${alts}`; - } - - return content; } \ No newline at end of file diff --git a/src/utils/alt-utils.ts b/src/utils/alt-utils.ts new file mode 100644 index 0000000..274b243 --- /dev/null +++ b/src/utils/alt-utils.ts @@ -0,0 +1,45 @@ +import { Guild } from 'discord.js'; +import { Database } from '../shared/Database'; +import { userIsBanned } from './e621-utils'; +import { config } from '../config'; + +export async function getE621Alts(discordId: string, guild: Guild, depth = 1, ignore: number[] = []): Promise { + const e621UserIds = await Database.getE621Ids(discordId); + + const toIgnore = ignore.concat(e621UserIds); + + let content = ''; + + for (const e621Id of e621UserIds) { + if (ignore.includes(e621Id)) continue; + + const alts = await getDiscordAlts(e621Id, guild, depth + 1, toIgnore); + + const banned = await userIsBanned(e621Id); + + content += `${'- '.repeat(depth)}${config.E621_BASE_URL}/users/${e621Id}${banned ? ' [BANNED]' : ''}\n${alts}`; + } + + return content; +} + +export async function getDiscordAlts(e621Id: number, guild: Guild, depth = 1, ignore: number[] = []): Promise { + const discordIds = await Database.getDiscordIds(e621Id); + + let content = ''; + + for (const discordId of discordIds) { + const alts = await getE621Alts(discordId, guild, depth + 1, ignore); + + let banned = false; + + // It's either this or fetch all the bans and sift through them for every discord alt. + try { + banned = !!(await guild.bans.fetch(discordId)); + } catch (e) { } + + if (alts || banned) content += `${'- '.repeat(depth)}<@${discordId}>${banned ? ' [BANNED]' : ''}\n${alts}`; + } + + return content; +} \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index 531cf25..63f68cd 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,4 @@ +export * from './alt-utils'; export * from './array-utils'; export * from './audit-log-utils'; export * from './channel-utils'; @@ -6,9 +7,9 @@ export * from './e621-utils'; export * from './message-logger'; export * from './message-utils'; export * from './ms-to-human'; +export * from './note-utils'; export * from './refresh-commands'; export * from './string-utils'; export * from './ticket-utils'; export * from './wait'; -export * from './note-utils'; export * from './whois'; diff --git a/src/utils/whois.ts b/src/utils/whois.ts index 0e26354..97c01aa 100644 --- a/src/utils/whois.ts +++ b/src/utils/whois.ts @@ -1,35 +1,15 @@ import { CommandInteraction, MessageFlags } from 'discord.js'; import { config } from '../config'; import { Database } from '../shared/Database'; +import { getE621Alts } from './alt-utils'; export async function handleWhoIsInteraction(interaction: CommandInteraction, idToUse: string, ephemeral = false) { if (ephemeral) await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); else await interaction.deferReply(); - const results = await Database.getE621Ids(idToUse); + if (!interaction.guild) return interaction.editReply('This command must be used in a server'); - if (results.length > 0) { - const mappedResults = results.map(e => `- ${config.E621_BASE_URL}/users/${e}\n`); + const content = await getE621Alts(idToUse, interaction.guild!); - const alts: string[] = []; - - for (const e621Id of results) { - const discordIds = await Database.getDiscordIds(e621Id); - - for (const discordId of discordIds) { - if (discordId != idToUse && !alts.includes(discordId)) alts.push(discordId); - } - } - - let content = `<@${idToUse}>'s e621 account(s):\n${mappedResults}`; - - if (alts.length > 0) { - const mappedAlts = alts.map(id => `- <@${id}>\n`); - content += `\n\nDiscord alts found:\n${mappedAlts}`; - } - - interaction.editReply(content); - } else { - interaction.editReply('No e621 accounts found for this user'); - } + interaction.editReply(`<@${idToUse}>'s e621 and discord account(s):\n${content}`); } \ No newline at end of file