jillo-bot/src/lib/counter.ts

122 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-11-11 00:01:38 +01:00
import { Client, CommandInteraction, GuildMember, EmbedBuilder, TextChannel, AutocompleteInteraction } from 'discord.js';
import { getSign } from './util';
import { Counter, db } from './db';
2022-07-19 21:39:34 +02:00
2023-11-11 00:01:38 +01:00
export async function getCounter(type: string) {
const counter = await db<Counter>('counters')
.where('key', type)
.first();
2022-07-19 21:39:34 +02:00
2023-11-11 00:01:38 +01:00
if (!counter) throw 'No such counter';
2023-04-19 21:12:45 +02:00
2023-11-11 00:01:38 +01:00
return counter.value;
2022-07-19 21:39:34 +02:00
}
2023-11-11 00:01:38 +01:00
export async function changeCounter(delta: number, type: string) {
const value = await getCounter(type);
const newValue = value + delta;
2022-07-19 21:39:34 +02:00
2023-11-11 00:01:38 +01:00
await db<Counter>('counters')
.where('key', type)
.update({
'value': newValue
});
2023-04-19 21:05:15 +02:00
2023-11-11 00:01:38 +01:00
return newValue;
2022-07-19 21:39:34 +02:00
}
2023-11-11 00:01:38 +01:00
export async function getCounterData(type: string) {
const counter = await db<Counter>('counters')
.select('*')
.where('key', type)
.first();
2023-04-19 21:05:15 +02:00
2023-11-11 00:01:38 +01:00
if (!counter) throw 'No such counter';
2023-04-19 21:05:15 +02:00
2023-11-11 00:01:38 +01:00
return counter;
2022-07-19 21:39:34 +02:00
}
2023-11-12 14:35:04 +01:00
export async function updateCounter(bot: Client, counter: Counter, value: number) {
2023-11-11 00:01:38 +01:00
const channel = await bot.channels.fetch(counter.channel) as TextChannel;
const messageID = counter.message;
2022-07-19 21:39:34 +02:00
2023-11-12 14:35:04 +01:00
const content = `[${counter.emoji}] x${value}`;
2022-07-19 21:39:34 +02:00
// bit janky
2023-11-11 00:01:38 +01:00
// yeah you don't say
2022-07-19 21:39:34 +02:00
try {
if (messageID) {
const message = await channel.messages.fetch(messageID);
if (!message) throw new Error();
await message.edit(content);
} else {
throw new Error();
}
} catch(err) {
const message = await channel.send(content);
message.pin();
2023-11-11 00:01:38 +01:00
await db<Counter>('counters')
.where('key', counter.key)
.update({
'message': message.id
});
2022-07-19 21:39:34 +02:00
}
}
2023-11-12 14:35:04 +01:00
export async function announceCounterUpdate(bot: Client, member: GuildMember, delta: number, counter: Counter, value: number) {
2023-11-11 00:01:38 +01:00
const channel = await bot.channels.fetch(counter.channel) as TextChannel;
2022-07-19 21:39:34 +02:00
const embed = new EmbedBuilder()
.setAuthor({
name: `${member.user.username}#${member.user.discriminator}`,
iconURL: member.user.displayAvatarURL()
})
2022-07-19 21:39:34 +02:00
.setDescription(`**${member.toString()}** has ${delta > 0 ? 'increased' : 'decreased'} the counter by **${Math.abs(delta)}**.`)
.setColor(member.displayColor)
.setTimestamp()
.setFooter({
2023-11-12 14:35:04 +01:00
text: `[${counter.emoji}] x${value}`
});
2022-07-19 21:39:34 +02:00
await channel.send({
embeds: [embed]
});
}
2023-11-11 00:01:38 +01:00
export async function changeCounterInteraction(interaction: CommandInteraction, member: GuildMember, amount: number, type: string) {
try {
const counter = await getCounterData(type);
2023-11-11 00:01:38 +01:00
const newCount = await changeCounter(amount, type);
await updateCounter(interaction.client, counter, newCount);
await announceCounterUpdate(interaction.client, member, amount, counter, newCount);
interaction.followUp({
content: `${counter.emoji} **You have ${amount > 0 ? 'increased' : 'decreased'} the counter.**\n\`\`\`diff\n ${newCount - amount}\n${getSign(amount)}${Math.abs(amount)}\n ${newCount}\`\`\``
});
} catch(err) {
interaction.followUp({
content: (err as Error).toString()
});
}
2023-11-11 00:01:38 +01:00
}
export async function counterAutocomplete(interaction: AutocompleteInteraction) {
const focusedValue = interaction.options.getFocused();
const guild = interaction.guildId;
const query = db<Counter>('counters')
.select('name', 'key')
.whereLike('name', `%${focusedValue.toLowerCase()}%`)
.limit(25);
if (guild) {
query.where('guild', guild);
}
const foundCounters = await query;
await interaction.respond(
foundCounters.map(choice => ({ name: choice.name, value: choice.key }))
);
2022-07-19 21:39:34 +02:00
}