items part 1
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import type { ChatInputCommandInteraction, GuildMember } from "discord.js";
|
||||
import type { AutocompleteInteraction, ChatInputCommandInteraction, GuildMember } from "discord.js";
|
||||
import { EmbedBuilder } from "discord.js";
|
||||
|
||||
import type { User } from "../db";
|
||||
import { get_user } from "../db";
|
||||
import { BotError } from "./error";
|
||||
import { BotError } from "./common/error";
|
||||
import config from "../config.json";
|
||||
|
||||
import say from "./say";
|
||||
@@ -12,6 +12,10 @@ import register_user from "./register_user";
|
||||
import bal from "./bal";
|
||||
import change_bal from "./change_bal";
|
||||
import transfer from "./transfer";
|
||||
import items from "./items";
|
||||
import create_item from "./create_item";
|
||||
import store from "./store";
|
||||
import change_item_balance from "./change_item_balance";
|
||||
|
||||
export interface CommandData {
|
||||
name: string;
|
||||
@@ -20,17 +24,16 @@ export interface CommandData {
|
||||
ephemeral: boolean;
|
||||
admin_only: boolean;
|
||||
run: (interaction: ChatInputCommandInteraction, user?: User) => Promise<any>;
|
||||
autocomplete?: (interaction: AutocompleteInteraction) => Promise<any>;
|
||||
};
|
||||
|
||||
const commands: CommandData[] = [say, roll, register_user, bal, change_bal, transfer];
|
||||
const commands: CommandData[] = [say, roll, register_user, bal, change_bal, transfer, items, create_item, store, change_item_balance];
|
||||
|
||||
function is_admin(interaction: ChatInputCommandInteraction): boolean {
|
||||
return (interaction.member as GuildMember).roles.cache.some((r) => r.id === config.admin_role);
|
||||
}
|
||||
|
||||
export default async function run(interaction: ChatInputCommandInteraction) {
|
||||
const name = interaction.commandName;
|
||||
|
||||
async function run(interaction: ChatInputCommandInteraction, found: CommandData, name: string) {
|
||||
//help command is "auto-generated"
|
||||
if (name === "help") {
|
||||
//max of 25 fields per embed, so if too many commands, this section needs a rewrite
|
||||
@@ -58,7 +61,6 @@ export default async function run(interaction: ChatInputCommandInteraction) {
|
||||
return await interaction.reply({ embeds, ephemeral: true });
|
||||
}
|
||||
|
||||
const found = commands.find((c) => c.name === name);
|
||||
try {
|
||||
//admin stuff should be ideally handled by register.ts, but this is a fallback
|
||||
if (found.admin_only && !is_admin(interaction)) throw new BotError("Admin permission needed to run that command");
|
||||
@@ -87,3 +89,14 @@ export default async function run(interaction: ChatInputCommandInteraction) {
|
||||
}
|
||||
}
|
||||
|
||||
export default async function handle_interaction(interaction: AutocompleteInteraction | ChatInputCommandInteraction) {
|
||||
const name = interaction.commandName;
|
||||
const found = commands.find((c) => c.name === name);
|
||||
|
||||
if (interaction.isChatInputCommand()) {
|
||||
return await run(interaction, found, name);
|
||||
} else if (interaction.isAutocomplete()) {
|
||||
return await found.autocomplete(interaction);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user