Make finduser and whois recursive as well

This commit is contained in:
Tarrgon
2025-05-30 18:39:47 -04:00
parent 87de789e7d
commit 33a6ba893b
5 changed files with 57 additions and 71 deletions
+5 -4
View File
@@ -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);
+1 -42
View File
@@ -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);
@@ -16,44 +16,3 @@ export async function handleMemberJoin(member: GuildMember) {
}
}
}
async function getE621Alts(discordId: string, guild: Guild, depth = 1, ignore: number[] = []): Promise<string> {
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<string> {
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;
}
+45
View File
@@ -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<string> {
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<string> {
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;
}
+2 -1
View File
@@ -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';
+4 -24
View File
@@ -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}`);
}