Skip to content

Commit

Permalink
Merge pull request #4 from CHProducts/dev
Browse files Browse the repository at this point in the history
⚡️ Add autocomplete support for resdelete command
  • Loading branch information
MotiCAT authored Jan 2, 2024
2 parents 38dc214 + 4cf3f78 commit 92bd3fc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Events/onInteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import { config } from 'dotenv';
config();

export async function onInteractionCreate(interaction: BaseInteraction): Promise<Awaitable<void>> {
if (interaction.isAutocomplete()) {
switch (interaction.commandName) {
case 'resdelete':
interactions.responseComplete(interaction);
break;
default:
break;
}
}
if (interaction.isButton()) {
switch (interaction.customId) {
case 'createticket':
Expand Down
2 changes: 1 addition & 1 deletion src/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const commands = [
new SlashCommandBuilder()
.setName('resdelete')
.setDescription('レスポンスを削除します')
.addStringOption((option) => option.setName('keyword').setDescription('削除するレスポンス').setRequired(true))
.addStringOption((option) => option.setName('keyword').setDescription('削除するレスポンス').setRequired(true).setAutocomplete(true))
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageMessages),
new SlashCommandBuilder().setName('resnow').setDescription('レスポンスを表示します'),
new SlashCommandBuilder()
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { timeoutCommand } from './timeout';
import { untimeoutCommand } from './untimeout';
import { userCommand } from './user';
import { yesCommand } from './yes';
import { responseComplete } from './response_complete';

export const interactions = {
help: helpCommand,
Expand Down Expand Up @@ -75,5 +76,6 @@ export const interactions = {
createticket: createticketCommand,
closeticket: closeticketCommand,
yes: yesCommand,
no: noCommand
no: noCommand,
responseComplete: responseComplete,
};
19 changes: 19 additions & 0 deletions src/interactions/response_complete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AutocompleteInteraction, Guild } from "discord.js";
import { readFileSync } from "fs";
import { ServerResponseData } from "../Utils/ServerData";

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);
});
const filtered = choices.filter(choice => choice.startsWith(focusedValue));
await interaction.respond(
filtered.map(choice => ({ name: choice, value: choice })),
);
}

0 comments on commit 92bd3fc

Please sign in to comment.