From 00ba521c2d133ca91a28876adfa1961e7983aeea Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Sat, 16 Sep 2023 08:31:00 +0530 Subject: [PATCH] fix: add autocmpletes to /hub blacklist --- src/Commands/Network/blacklist.ts | 83 ++++++++++++++++++++++++++----- src/Scripts/blacklist/server.ts | 2 +- 2 files changed, 71 insertions(+), 14 deletions(-) diff --git a/src/Commands/Network/blacklist.ts b/src/Commands/Network/blacklist.ts index 454c0de8..dc8462d2 100644 --- a/src/Commands/Network/blacklist.ts +++ b/src/Commands/Network/blacklist.ts @@ -19,6 +19,7 @@ export default { hubOption .setName('hub') .setDescription('The name of the hub to blacklist the user from.') + .setAutocomplete(true) .setRequired(true), ) .addStringOption(user => @@ -50,6 +51,7 @@ export default { hubOption .setName('hub') .setDescription('The name of the hub to blacklist the user from.') + .setAutocomplete(true) .setRequired(true), ) .addStringOption(server => @@ -89,6 +91,7 @@ export default { hubOption .setName('hub') .setDescription('The name of the hub to blacklist the user from.') + .setAutocomplete(true) .setRequired(true), ) .addStringOption(user => @@ -112,12 +115,14 @@ export default { hubOption .setName('hub') .setDescription('The name of the hub to blacklist the user from.') + .setAutocomplete(true) .setRequired(true), ) .addStringOption(server => server .setName('server') .setDescription('The server to remove from the blacklist.') + .setAutocomplete(true) .setRequired(true), ) .addStringOption(string => @@ -135,6 +140,7 @@ export default { hubOption .setName('hub') .setDescription('The name of the hub to blacklist the user from.') + .setAutocomplete(true) .setRequired(true), ) .addStringOption(string => @@ -171,29 +177,80 @@ export default { }, async autocomplete(interaction: AutocompleteInteraction) { + const db = getDb(); const action = interaction.options.getSubcommand() as 'user' | 'server'; - const hubName = interaction.options.getString('hub', true); + const focusedHub = interaction.options.get('hub'); + + if (typeof focusedHub?.value !== 'string') return; - const focusedValue = interaction.options.getFocused().toLowerCase(); - let choices; + if (focusedHub.focused) { + const hub = await db.hubs.findMany({ + where: { + name: { mode: 'insensitive', contains: focusedHub.value }, + OR: [ + { ownerId: interaction.user.id }, + { moderators: { some: { userId: interaction.user.id } } }, + ], + }, + take: 25, + }); + + const filtered = hub.map(({ name: hubName }) => ({ name: hubName, value: hubName })); + return interaction.respond(filtered); + } switch (action) { case 'user': { - const allUsers = await getDb().blacklistedUsers.findMany({ where: { hub: { name: hubName } } }); - choices = allUsers.map((user) => { return { name: user.username, value: user.userId }; }); + const userOpt = interaction.options.get('user'); + + if (!userOpt?.focused || typeof userOpt.value !== 'string') return; + + const filteredUsers = await db.blacklistedUsers.findMany({ + where: { + hub: { + name: focusedHub.value, + OR: [ + { ownerId: interaction.user.id }, + { moderators: { some: { userId: interaction.user.id } } }, + ], + }, + OR: [ + { username: { mode: 'insensitive', contains: userOpt.value } }, + { userId: { mode: 'insensitive', contains: userOpt.value } }, + ], + }, + take: 25, + }); + + const choices = filteredUsers.map((user) => { return { name: user.username, value: user.userId }; }); + interaction.respond(choices); break; } case 'server': { - const allServers = await getDb().blacklistedServers.findMany({ where: { hub: { name: hubName } } }); - choices = allServers.map((server) => { return { name: server.serverName, value: server.serverId }; }); + const serverOpt = interaction.options.get('server', true); + + if (!serverOpt.focused || typeof serverOpt.value !== 'string') return; + + const allServers = await db.blacklistedServers.findMany({ + where: { + hub: { + name: focusedHub.value, + OR: [ + { ownerId: interaction.user.id }, + { moderators: { some: { userId: interaction.user.id } } }, + ], + }, + OR: [ + { serverName: { mode: 'insensitive', contains: serverOpt.value } }, + { serverId: { mode: 'insensitive', contains: serverOpt.value } }, + ], + }, + take: 25, + }); + const choices = allServers.map(({ serverName, serverId }) => { return { name: serverName, value: serverId }; }); + await interaction.respond(choices); break; } } - - const filtered = choices - .filter((choice) => choice.name.toLowerCase().includes(focusedValue) || choice.value.toLowerCase().includes(focusedValue)) - .slice(0, 25); - - interaction.respond(filtered); }, }; diff --git a/src/Scripts/blacklist/server.ts b/src/Scripts/blacklist/server.ts index 5bd75b3a..82a3e9b0 100644 --- a/src/Scripts/blacklist/server.ts +++ b/src/Scripts/blacklist/server.ts @@ -62,7 +62,7 @@ export = { } else if (subCommandGroup == 'remove') { const reason = interaction.options.getString('reason'); - if (!serverInBlacklist) return await interaction.reply('The server is not blacklisted.'); + if (!serverInBlacklist) return await interaction.reply({ content: 'The server is not blacklisted.', ephemeral: true }); await blacklistedServers.deleteMany({ where: { serverId: serverOpt, hubId: hub.id } }); // Using name from DB since the bot can't access server through API.