126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
import { AutocompleteInteraction, User } from 'discord.js';
|
|
import { CustomItem, ItemInventory, db } from './db';
|
|
|
|
type DefaultItem = Omit<CustomItem, 'guild'>; // uses negative IDs
|
|
type Item = DefaultItem | CustomItem;
|
|
|
|
interface Behavior {
|
|
name: string,
|
|
description: string,
|
|
itemType: 'plain' | 'weapon' | 'consumable',
|
|
// triggers upon use
|
|
// for 'weapons', this is on hit
|
|
// for 'consumable', this is on use
|
|
// for 'plain', ...??
|
|
// returns `true` upon success, `false` otherwise
|
|
action?: (item: Item, user: User) => Promise<boolean>
|
|
}
|
|
|
|
export const defaultItems: DefaultItem[] = [
|
|
{
|
|
'id': -1,
|
|
'name': 'Coin',
|
|
'emoji': '🪙',
|
|
'type': 'plain',
|
|
'maxStack': 9999,
|
|
'untradable': false
|
|
}
|
|
];
|
|
|
|
export const behaviors: Behavior[] = [
|
|
{
|
|
'name': 'heal',
|
|
'description': 'Heals the user by `behaviorValue`',
|
|
'itemType': 'consumable',
|
|
'action': async (item: Item, user: User) => {
|
|
// todo
|
|
return false;
|
|
}
|
|
}
|
|
];
|
|
|
|
export async function getCustomItem(id: number) {
|
|
return await db<CustomItem>('customItems')
|
|
.where('id', id)
|
|
.first();
|
|
}
|
|
|
|
export async function getItem(id: number): Promise<Item | undefined> {
|
|
if (id >= 0) {
|
|
return await getCustomItem(id);
|
|
} else {
|
|
return defaultItems.find(item => item.id === id);
|
|
}
|
|
}
|
|
|
|
export async function getItemQuantity(user: string, itemID: number) {
|
|
return (await db<ItemInventory>('itemInventories')
|
|
.where('item', itemID)
|
|
.where('user', user)
|
|
.first())
|
|
|| {
|
|
'user': user,
|
|
'item': itemID,
|
|
'quantity': 0
|
|
};
|
|
}
|
|
|
|
export async function giveItem(user: string, item: Item, quantity = 1) {
|
|
const storedItem = await db<ItemInventory>('itemInventories')
|
|
.where('user', user)
|
|
.where('item', item.id)
|
|
.first();
|
|
|
|
let inv;
|
|
if (storedItem) {
|
|
inv = await db<ItemInventory>('itemInventories')
|
|
.update({
|
|
'quantity': db.raw('MIN(quantity + ?, ?)', [quantity, getMaxStack(item)])
|
|
})
|
|
.limit(1)
|
|
.where('user', user)
|
|
.where('item', item.id)
|
|
.returning('*');
|
|
} else {
|
|
inv = await db<ItemInventory>('itemInventories')
|
|
.insert({
|
|
'user': user,
|
|
'item': Math.min(item.id, getMaxStack(item)),
|
|
'quantity': quantity
|
|
})
|
|
.returning('*');
|
|
}
|
|
|
|
return inv[0];
|
|
}
|
|
|
|
export function getMaxStack(item: Item) {
|
|
return item.type === 'weapon' ? 1 : item.maxStack;
|
|
}
|
|
|
|
export function formatItem(item: Item | undefined) {
|
|
if (!item) return '? **MISSINGNO**';
|
|
return `${item.emoji} **${item.name}**`;
|
|
}
|
|
export function formatItems(item: Item | undefined, quantity: number) {
|
|
return `${quantity}x ${formatItem(item)}`;
|
|
}
|
|
|
|
export async function itemAutocomplete(interaction: AutocompleteInteraction) {
|
|
const focused = interaction.options.getFocused();
|
|
|
|
const customItems = await db<CustomItem>('customItems')
|
|
.select('emoji', 'name', 'id')
|
|
// @ts-expect-error this LITERALLY works
|
|
.whereLike(db.raw('UPPER(name)'), `%${focused.toUpperCase()}%`)
|
|
.where('guild', interaction.guildId!)
|
|
.limit(25);
|
|
|
|
const foundDefaultItems = defaultItems.filter(item => item.name.toUpperCase().includes(focused.toUpperCase()));
|
|
|
|
const items = [...foundDefaultItems, ...customItems];
|
|
|
|
await interaction.respond(
|
|
items.map(choice => ({ name: `${choice.emoji} ${choice.name}`, value: choice.id.toString() }))
|
|
);
|
|
} |