import { Client, CommandInteraction, GuildMember, MessageEmbed, TextChannel } from 'discord.js'; import * as fsp from 'fs/promises'; import { exists, getSign } from './util'; const counterFile = './counter.json'; const counterFileCream = './counterCream.json'; const counterMessageFile = './counterMessageID.txt'; const counterCreamMessageFile = './counterCreamMessageID.txt'; const PISS_CHANNEL = '975802147126018150'; const CREAM_CHANNEL = '1098319707741900910'; export const PISS_EMOJI = '🪣'; export const CREAM_EMOJI = '<:PoolOfCum:1070350928240181340>'; function getCounterFile(cream: boolean) { return cream ? counterFileCream : counterFile; } let counter = 0; export async function initializeCounter(cream: boolean) { const filename = getCounterFile(cream); if (await exists(filename)) { counter = parseInt(await fsp.readFile(filename, 'utf8')); } else { counter = 0; await saveCounter(cream); } } async function saveCounter(cream: boolean) { fsp.writeFile(getCounterFile(cream), Math.trunc(counter).toString()); } export async function changeCounter(delta: number, cream: boolean) { counter += delta; await saveCounter(cream); return counter; } function getCounterMessageFile(cream: boolean) { return cream ? counterMessageFile : counterCreamMessageFile; } async function getCounterMessageID(cream: boolean) { const filename = getCounterMessageFile(cream); if (await exists(filename)) { const str = await fsp.readFile(filename, 'utf8'); return str; } else { return null; } } function saveCounterMessageID(id: string, cream: boolean) { return fsp.writeFile(getCounterMessageFile(cream), id); } function getEmoji(cream: boolean) { return cream ? CREAM_EMOJI : PISS_EMOJI; } function getChannel(cream: boolean) { return cream ? CREAM_CHANNEL : PISS_CHANNEL; } export async function updateCounter(bot: Client, cream: boolean) { const channel = await bot.channels.fetch(getChannel(cream)) as TextChannel; const messageID = await getCounterMessageID(cream); const content = `[${getEmoji(cream)}] x${counter}`; // bit janky 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(); await saveCounterMessageID(message.id, cream); } } export async function announceCounterUpdate(bot: Client, member: GuildMember, delta: number, cream: boolean) { const channel = await bot.channels.fetch(getChannel(cream)) as TextChannel; const embed = new MessageEmbed() .setAuthor(`${member.user.username}#${member.user.discriminator}`, member.user.displayAvatarURL()) .setDescription(`**${member.toString()}** has ${delta > 0 ? 'increased' : 'decreased'} the counter by **${Math.abs(delta)}**.`) .setColor(member.displayColor) .setTimestamp() .setFooter(`[${getEmoji(cream)}] x${counter}`); await channel.send({ embeds: [embed] }); } export async function changeCounterInteraction(interaction: CommandInteraction, member: GuildMember, amount: number, cream: boolean) { const newCount = await changeCounter(amount, cream); await updateCounter(interaction.client, cream); await announceCounterUpdate(interaction.client, member, amount, cream); interaction.followUp({ content: `${getEmoji(cream)} **You have ${amount > 0 ? 'increased' : 'decreased'} the counter.**\n\`\`\`diff\n ${newCount - amount}\n${getSign(amount)}${Math.abs(amount)}\n ${newCount}\`\`\`` }); }