This commit is contained in:
Jill 2022-07-19 22:39:34 +03:00
parent 116dee9731
commit c50c474c40
6 changed files with 154 additions and 1 deletions

4
.gitignore vendored
View File

@ -3,4 +3,6 @@ config.json
next.json
node_modules
tsconfig.tsbuildinfo
built/*
built/*
counter.json
counterMessageID.txt

21
src/commands/decrease.ts Normal file
View File

@ -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);
}
};

21
src/commands/increase.ts Normal file
View File

@ -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);
}
};

View File

@ -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();

90
src/lib/counter.ts Normal file
View File

@ -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
});
}

16
src/lib/util.ts Normal file
View File

@ -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 ' ';
}