2023-11-15 16:11:48 +01:00
|
|
|
import { AutocompleteInteraction } from 'discord.js';
|
2023-11-15 11:57:20 +01:00
|
|
|
import { CustomItem, ItemInventory, db } from '../db';
|
2023-11-15 01:40:57 +01:00
|
|
|
|
2023-11-15 11:59:38 +01:00
|
|
|
export type DefaultItem = Omit<CustomItem, 'guild'>; // uses negative IDs
|
|
|
|
export type Item = DefaultItem | CustomItem;
|
2023-11-15 01:40:57 +01:00
|
|
|
|
2023-11-15 11:59:38 +01:00
|
|
|
export interface Items {
|
2023-11-15 11:56:46 +01:00
|
|
|
item: Item,
|
|
|
|
quantity: number
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:11:48 +01:00
|
|
|
export enum DefaultItems {
|
|
|
|
COIN = 1,
|
|
|
|
WORKBENCH = 2,
|
|
|
|
PEBBLE = 3
|
2023-11-15 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
2023-11-15 16:11:48 +01:00
|
|
|
export const defaultItems: DefaultItem[] = [
|
|
|
|
{
|
|
|
|
id: -1,
|
|
|
|
name: 'Coin',
|
|
|
|
emoji: '🪙',
|
|
|
|
type: 'plain',
|
|
|
|
maxStack: 9999,
|
|
|
|
untradable: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: -2,
|
|
|
|
name: 'Workbench',
|
|
|
|
description: 'A place for you to work with tools, for simple things',
|
|
|
|
emoji: '🛠️',
|
|
|
|
type: 'plain',
|
|
|
|
maxStack: 1,
|
|
|
|
untradable: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: -3,
|
|
|
|
name: 'Pebble',
|
|
|
|
description: 'If you get 5 of them you will instantly ! !!!',
|
|
|
|
emoji: '🪨',
|
|
|
|
type: 'plain',
|
|
|
|
maxStack: 64,
|
|
|
|
untradable: false
|
|
|
|
}
|
|
|
|
];
|
2023-11-15 16:04:36 +01:00
|
|
|
|
2023-11-15 01:40:57 +01:00
|
|
|
|
2023-11-15 11:56:46 +01:00
|
|
|
export function getDefaultItem(id: DefaultItems): Item
|
|
|
|
export function getDefaultItem(id: number): Item | undefined {
|
|
|
|
return defaultItems.find(item => Math.abs(item.id) === Math.abs(id));
|
|
|
|
}
|
|
|
|
|
2023-11-15 01:40:57 +01:00
|
|
|
export async function getItem(id: number): Promise<Item | undefined> {
|
|
|
|
if (id >= 0) {
|
|
|
|
return await getCustomItem(id);
|
|
|
|
} else {
|
2023-11-15 11:56:46 +01:00
|
|
|
return getDefaultItem(id);
|
2023-11-15 01:40:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:11:48 +01:00
|
|
|
export async function getCustomItem(id: number) {
|
|
|
|
return await db<CustomItem>('customItems')
|
|
|
|
.where('id', id)
|
|
|
|
.first();
|
2023-11-15 16:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getItemQuantity(user: string, itemID: number): Promise<ItemInventory> {
|
2023-11-15 11:03:01 +01:00
|
|
|
return (await db<ItemInventory>('itemInventories')
|
|
|
|
.where('item', itemID)
|
|
|
|
.where('user', user)
|
|
|
|
.first())
|
|
|
|
|| {
|
2023-11-15 12:01:51 +01:00
|
|
|
user: user,
|
|
|
|
item: itemID,
|
|
|
|
quantity: 0
|
2023-11-15 11:03:01 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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({
|
2023-11-15 12:01:51 +01:00
|
|
|
quantity: db.raw('MIN(quantity + ?, ?)', [quantity, getMaxStack(item)])
|
2023-11-15 11:03:01 +01:00
|
|
|
})
|
|
|
|
.limit(1)
|
|
|
|
.where('user', user)
|
|
|
|
.where('item', item.id)
|
|
|
|
.returning('*');
|
|
|
|
} else {
|
|
|
|
inv = await db<ItemInventory>('itemInventories')
|
|
|
|
.insert({
|
2023-11-15 12:01:51 +01:00
|
|
|
user: user,
|
|
|
|
item: Math.min(item.id, getMaxStack(item)),
|
|
|
|
quantity: quantity
|
2023-11-15 11:03:01 +01:00
|
|
|
})
|
|
|
|
.returning('*');
|
|
|
|
}
|
|
|
|
|
|
|
|
return inv[0];
|
|
|
|
}
|
|
|
|
|
2023-11-15 01:40:57 +01:00
|
|
|
export function getMaxStack(item: Item) {
|
|
|
|
return item.type === 'weapon' ? 1 : item.maxStack;
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:22:57 +01:00
|
|
|
export function formatItem(item: Item | undefined, disableBold = false) {
|
|
|
|
if (!item) return disableBold ? '? MISSINGNO' : '? **MISSINGNO**';
|
|
|
|
return disableBold ? `${item.emoji} ${item.name}` : `${item.emoji} **${item.name}**`;
|
2023-11-15 01:40:57 +01:00
|
|
|
}
|
2023-11-15 16:22:57 +01:00
|
|
|
export function formatItems(item: Item | undefined, quantity: number, disableBold = false) {
|
|
|
|
return `${quantity}x ${formatItem(item, disableBold)}`;
|
2023-11-15 11:03:01 +01:00
|
|
|
}
|
2023-11-15 16:22:57 +01:00
|
|
|
export function formatItemsArray(items: Items[], disableBold = false) {
|
|
|
|
if (items.length === 0) return disableBold ? 'nothing' : '**nothing**';
|
|
|
|
return items.map(i => formatItems(i.item, i.quantity, disableBold)).join(' ');
|
2023-11-15 11:56:46 +01:00
|
|
|
}
|
2023-11-15 11:03:01 +01:00
|
|
|
|
|
|
|
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() }))
|
|
|
|
);
|
2023-11-15 01:40:57 +01:00
|
|
|
}
|