From c50c474c4002a29c503f63edff1703c13a56609b Mon Sep 17 00:00:00 2001 From: Jill Monoids Date: Tue, 19 Jul 2022 22:39:34 +0300 Subject: [PATCH] counter --- .gitignore | 4 +- src/commands/decrease.ts | 21 ++++++++++ src/commands/increase.ts | 21 ++++++++++ src/index.ts | 3 ++ src/lib/counter.ts | 90 ++++++++++++++++++++++++++++++++++++++++ src/lib/util.ts | 16 +++++++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/commands/decrease.ts create mode 100644 src/commands/increase.ts create mode 100644 src/lib/counter.ts create mode 100644 src/lib/util.ts diff --git a/.gitignore b/.gitignore index 41ac105..f0459bd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ config.json next.json node_modules tsconfig.tsbuildinfo -built/* \ No newline at end of file +built/* +counter.json +counterMessageID.txt \ No newline at end of file diff --git a/src/commands/decrease.ts b/src/commands/decrease.ts new file mode 100644 index 0000000..fae6be4 --- /dev/null +++ b/src/commands/decrease.ts @@ -0,0 +1,21 @@ +import { SlashCommandBuilder } from '@discordjs/builders'; +import { CommandInteraction, GuildMember } from 'discord.js'; +import { changeCounterInteraction } from '../lib/counter'; + +module.exports = { + data: new SlashCommandBuilder() + .setName('decrease') + .setDescription('Decrease the counter') + .addNumberOption((option) => + option + .setName('amount') + .setRequired(false) + .setDescription('Amount to decrease the counter by') + .setMinValue(1) + ), + + execute: async (interaction: CommandInteraction, member: GuildMember) => { + const amount = Math.trunc(interaction.options.getNumber('amount') || 1); + changeCounterInteraction(interaction, member, -amount); + } +}; \ No newline at end of file diff --git a/src/commands/increase.ts b/src/commands/increase.ts new file mode 100644 index 0000000..7afee15 --- /dev/null +++ b/src/commands/increase.ts @@ -0,0 +1,21 @@ +import { SlashCommandBuilder } from '@discordjs/builders'; +import { CommandInteraction, GuildMember } from 'discord.js'; +import { changeCounterInteraction } from '../lib/counter'; + +module.exports = { + data: new SlashCommandBuilder() + .setName('increase') + .setDescription('Increase the counter') + .addNumberOption((option) => + option + .setName('amount') + .setRequired(false) + .setDescription('Amount to increase the counter by') + .setMinValue(1) + ), + + execute: async (interaction: CommandInteraction, member: GuildMember) => { + const amount = Math.trunc(interaction.options.getNumber('amount') || 1); + changeCounterInteraction(interaction, member, amount); + } +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6509649..1fa0885 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import * as Discord from 'discord.js'; import * as fs from 'fs'; const { token, disableDaytimeAnnouncements } = JSON.parse(fs.readFileSync('./config.json', 'utf8')); import * as path from 'path'; +import { initializeCounter } from './lib/counter'; const morningHour = 8; const eveningHour = 21; @@ -89,6 +90,8 @@ const night = [ ]; bot.on('ready', async () => { + initializeCounter(); + setInterval(() => { const current = new Date().getTime(); diff --git a/src/lib/counter.ts b/src/lib/counter.ts new file mode 100644 index 0000000..2d364c0 --- /dev/null +++ b/src/lib/counter.ts @@ -0,0 +1,90 @@ +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 counterMessageFile = './counterMessageID.txt'; + +const PISS_CHANNEL = '975802147126018150'; +export const EMOJI = '🪣'; + +let counter = 0; + +export async function initializeCounter() { + if (await exists(counterFile)) { + counter = parseInt(await fsp.readFile(counterFile, 'utf8')); + } else { + counter = 0; + await saveCounter(); + } +} + +async function saveCounter() { + fsp.writeFile(counterFile, Math.trunc(counter).toString()); +} + +export async function changeCounter(delta: number) { + counter += delta; + await saveCounter(); + return counter; +} + +async function getCounterMessageID() { + if (await exists(counterMessageFile)) { + const str = await fsp.readFile(counterMessageFile, 'utf8'); + return str; + } else { + return null; + } +} + +function saveCounterMessageID(id: string) { + return fsp.writeFile(counterMessageFile, id); +} + +export async function updateCounter(bot: Client) { + const channel = await bot.channels.fetch(PISS_CHANNEL) as TextChannel; + const messageID = await getCounterMessageID(); + + const content = `[${EMOJI}] 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); + } +} + +export async function announceCounterUpdate(bot: Client, member: GuildMember, delta: number) { + const channel = await bot.channels.fetch(PISS_CHANNEL) 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(`[${EMOJI}] x${counter}`); + + await channel.send({ + embeds: [embed] + }); +} + +export async function changeCounterInteraction(interaction: CommandInteraction, member: GuildMember, amount: number) { + const newCount = await changeCounter(amount); + await updateCounter(interaction.client); + await announceCounterUpdate(interaction.client, member, amount); + interaction.reply({ + content: `${EMOJI} **You have ${amount > 0 ? 'increased' : 'decreased'} the counter.**\n\`\`\`diff\n ${newCount - amount}\n${getSign(amount)}${Math.abs(amount)}\n ${newCount}\`\`\``, + ephemeral: true + }); +} \ No newline at end of file diff --git a/src/lib/util.ts b/src/lib/util.ts new file mode 100644 index 0000000..cd08573 --- /dev/null +++ b/src/lib/util.ts @@ -0,0 +1,16 @@ +import * as fsp from 'fs/promises'; + +export async function exists(file: string) { + try { + await fsp.stat(file); + return true; + } catch(_err) { + return false; + } +} + +export function getSign(n: number) { + if (n > 0) return '+'; + if (n < 0) return '-'; + return ' '; +} \ No newline at end of file