Skip to content

Commit

Permalink
⚡️ Autocomplete changes (to be available sometime in the future)
Browse files Browse the repository at this point in the history
  • Loading branch information
MotiCAT committed Feb 16, 2024
1 parent c551607 commit d51d19e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Events/onInteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ config();
export async function onInteractionCreate(interaction: BaseInteraction): Promise<Awaitable<void>> {
if (interaction.isAutocomplete()) {
switch (interaction.commandName) {
case 'resdelete':
case 'response':
interactions.responseComplete(interaction);
break;
default:
Expand Down
29 changes: 19 additions & 10 deletions src/interactions/response_complete.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { ServerResponseData } from '../Utils/ServerData';
import { list } from '../index';
import { connect, Database } from 'aurora-mongo';
import { AutocompleteInteraction, Guild } from 'discord.js';
import { readFileSync } from 'fs';

export async function responseComplete(interaction: AutocompleteInteraction) {
const focusedValue = interaction.options.getFocused();
const rawData = readFileSync('./database/responses.json', 'utf-8');
const data: Record<string, ServerResponseData> = JSON.parse(rawData);
const choices = [] as string[];
const serverData = data[(interaction.guild as Guild).id];
if (!serverData || Object.keys(serverData).length === 0) return;
Object.keys(data[(interaction.guild as Guild).id]).forEach((key) => {
choices.push(key);
});
await connect(process.env.MONGO_URL!);
const Response = new Database('Response');
list['response'] = await Response.keys();
const matchingKeys = list['response'].filter((key) => key.split(',')[0] === (interaction.guild as Guild).id);
const serverData: { [key: string]: string } = {};
await Promise.all(
matchingKeys.map(async (key) => {
const value = await Response.get(key);
serverData[key] = value;
})
);
const choices: string[] = [];
for (const key in serverData) {
const response = serverData[key];
choices.push(response);
}

const filtered = choices.filter((choice) => choice.startsWith(focusedValue));
await interaction.respond(filtered.map((choice) => ({ name: choice, value: choice })));
}

0 comments on commit d51d19e

Please sign in to comment.