jillo-bot/src/commands/item.ts

221 lines
8.0 KiB
TypeScript

import { AutocompleteInteraction, Interaction, SlashCommandBuilder } from 'discord.js';
import { CustomItem, ItemInventory, db } from '../lib/db';
import { behaviors, formatItems, getItem, getMaxStack, giveItem, itemAutocomplete } from '../lib/items';
//function extendOption(t: string) {
// return {name: t, value: t};
//}
module.exports = {
data: new SlashCommandBuilder()
.setName('item')
.setDescription('[ADMIN] Create, edit and otherwise deal with custom items')
.addSubcommandGroup(grp =>
grp
.setName('add')
.setDescription('[ADMIN] Create an item')
.addSubcommand(cmd =>
cmd
.setName('plain')
.setDescription('A normal, functionless item')
.addStringOption(opt =>
opt
.setName('name')
.setDescription('The item name')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('emoji')
.setDescription('An emoji or symbol that could represent this item')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('description')
.setDescription('A short description')
)
.addIntegerOption(opt =>
opt
.setName('maxstack')
.setDescription('Maximum amount of this item you\'re able to hold at once')
)
.addStringOption(opt =>
opt
.setName('behavior')
.setDescription('Special behavior type')
.setChoices(...behaviors.filter(b => b.itemType === 'plain').map(b => ({name: `${b.name} - ${b.description}`, value: b.name})))
)
.addBooleanOption(opt =>
opt
.setName('untradable')
.setDescription('Can you give this item to other people?')
)
.addNumberOption(opt =>
opt
.setName('behaviorvalue')
.setDescription('A value to use for the behavior type; not always applicable')
)
)
.addSubcommand(cmd =>
cmd
.setName('weapon')
.setDescription('A weapon that you can attack things with')
.addStringOption(opt =>
opt
.setName('name')
.setDescription('The item name')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('emoji')
.setDescription('An emoji or symbol that could represent this item')
.setRequired(true)
)
.addIntegerOption(opt =>
opt
.setName('damage')
.setDescription('How much base damage this weapon is intended to deal')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('description')
.setDescription('A short description')
)
.addStringOption(opt =>
opt
.setName('behavior')
.setDescription('Special behavior type')
.setChoices(...behaviors.filter(b => b.itemType === 'weapon').map(b => ({name: `${b.name} - ${b.description}`, value: b.name})))
)
.addBooleanOption(opt =>
opt
.setName('untradable')
.setDescription('Can you give this item to other people?')
)
.addNumberOption(opt =>
opt
.setName('behaviorvalue')
.setDescription('A value to use for the behavior type; not always applicable')
)
)
.addSubcommand(cmd =>
cmd
.setName('consumable')
.setDescription('Consumable item, usable once and never again')
.addStringOption(opt =>
opt
.setName('name')
.setDescription('The item name')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('emoji')
.setDescription('An emoji or symbol that could represent this item')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('description')
.setDescription('A short description')
)
.addIntegerOption(opt =>
opt
.setName('maxstack')
.setDescription('Maximum amount of this item you\'re able to hold at once')
)
.addStringOption(opt =>
opt
.setName('behavior')
.setDescription('Special behavior type')
.setChoices(...behaviors.filter(b => b.itemType === 'consumable').map(b => ({name: `${b.name} - ${b.description}`, value: b.name})))
)
.addBooleanOption(opt =>
opt
.setName('untradable')
.setDescription('Can you give this item to other people?')
)
.addNumberOption(opt =>
opt
.setName('behaviorvalue')
.setDescription('A value to use for the behavior type; not always applicable')
)
)
)
.addSubcommand(cmd =>
cmd
.setName('give')
.setDescription('[ADMIN] Give a user an item')
.addUserOption(opt =>
opt
.setName('who')
.setDescription('The user')
.setRequired(true)
)
.addStringOption(opt =>
opt
.setName('item')
.setDescription('The item')
.setAutocomplete(true)
.setRequired(true)
)
.addIntegerOption(opt =>
opt
.setName('quantity')
.setDescription('Amount of items to give')
)
)
.setDefaultMemberPermissions('0')
.setDMPermission(false),
execute: async (interaction: Interaction) => {
if (!interaction.isChatInputCommand()) return;
await interaction.deferReply({ephemeral: true});
const subcommand = interaction.options.getSubcommand(true);
const group = interaction.options.getSubcommandGroup();
if (group === 'add') {
const item = await db<CustomItem>('customItems')
.insert({
'guild': interaction.guildId!,
'name': interaction.options.getString('name', true).trim(),
'description': interaction.options.getString('description') || undefined,
'emoji': interaction.options.getString('emoji', true).trim(),
'type': subcommand as 'plain' | 'weapon' | 'consumable', // kind of wild that ts makes you do this
'maxStack': (interaction.options.getInteger('maxstack') || interaction.options.getInteger('damage')) || (subcommand === 'weapon' ? 1 : 64),
'behavior': interaction.options.getString('behavior') || undefined,
'untradable': interaction.options.getBoolean('untradable') || false,
'behaviorValue': interaction.options.getNumber('behaviorValue') || undefined,
})
.returning('*');
await interaction.followUp(`${JSON.stringify(item[0])}`);
} else {
if (subcommand === 'give') {
const user = interaction.options.getUser('who', true);
const itemID = parseInt(interaction.options.getString('item', true));
const quantity = interaction.options.getInteger('quantity') || 1;
const item = await getItem(itemID);
if (!item) return interaction.followUp('No such item exists!');
const inv = await giveItem(user.id, item, quantity);
await interaction.followUp(`${user.toString()} now has ${formatItems(item, inv.quantity)}.`);
}
}
},
autocomplete: async (interaction: AutocompleteInteraction) => {
const focused = interaction.options.getFocused(true);
if (focused.name === 'item') {
return itemAutocomplete(interaction);
}
}
};