2022-07-19 21:39:34 +02:00
import { Client , CommandInteraction , GuildMember , MessageEmbed , TextChannel } from 'discord.js' ;
import * as fsp from 'fs/promises' ;
import { exists , getSign } from './util' ;
const counterFile = './counter.json' ;
2023-04-19 21:05:15 +02:00
const counterFileCream = './counterCream.json' ;
2022-07-19 21:39:34 +02:00
const counterMessageFile = './counterMessageID.txt' ;
2023-04-19 21:05:15 +02:00
const counterCreamMessageFile = './counterCreamMessageID.txt' ;
2022-07-19 21:39:34 +02:00
const PISS_CHANNEL = '975802147126018150' ;
2023-04-19 21:05:15 +02:00
const CREAM_CHANNEL = '1098319707741900910' ;
export const PISS_EMOJI = '🪣' ;
export const CREAM_EMOJI = '<:PoolOfCum:1070350928240181340>' ;
function getCounterFile ( cream : boolean ) {
return cream ? counterFileCream : counterFile ;
}
2022-07-19 21:39:34 +02:00
let counter = 0 ;
2023-04-19 21:05:15 +02:00
export async function initializeCounter ( cream : boolean ) {
const filename = getCounterFile ( cream ) ;
if ( await exists ( filename ) ) {
counter = parseInt ( await fsp . readFile ( filename , 'utf8' ) ) ;
2022-07-19 21:39:34 +02:00
} else {
counter = 0 ;
2023-04-19 21:05:15 +02:00
await saveCounter ( cream ) ;
2022-07-19 21:39:34 +02:00
}
}
2023-04-19 21:05:15 +02:00
async function saveCounter ( cream : boolean ) {
fsp . writeFile ( getCounterFile ( cream ) , Math . trunc ( counter ) . toString ( ) ) ;
2022-07-19 21:39:34 +02:00
}
2023-04-19 21:05:15 +02:00
export async function changeCounter ( delta : number , cream : boolean ) {
2022-07-19 21:39:34 +02:00
counter += delta ;
2023-04-19 21:05:15 +02:00
await saveCounter ( cream ) ;
2022-07-19 21:39:34 +02:00
return counter ;
}
2023-04-19 21:05:15 +02:00
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' ) ;
2022-07-19 21:39:34 +02:00
return str ;
} else {
return null ;
}
}
2023-04-19 21:05:15 +02:00
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 ;
2022-07-19 21:39:34 +02:00
}
2023-04-19 21:05:15 +02:00
export async function updateCounter ( bot : Client , cream : boolean ) {
const channel = await bot . channels . fetch ( getChannel ( cream ) ) as TextChannel ;
const messageID = await getCounterMessageID ( cream ) ;
2022-07-19 21:39:34 +02:00
2023-04-19 21:05:15 +02:00
const content = ` [ ${ getEmoji ( cream ) } ] x ${ counter } ` ;
2022-07-19 21:39:34 +02:00
// 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 ( ) ;
2023-04-19 21:05:15 +02:00
await saveCounterMessageID ( message . id , cream ) ;
2022-07-19 21:39:34 +02:00
}
}
2023-04-19 21:05:15 +02:00
export async function announceCounterUpdate ( bot : Client , member : GuildMember , delta : number , cream : boolean ) {
const channel = await bot . channels . fetch ( getChannel ( cream ) ) as TextChannel ;
2022-07-19 21:39:34 +02:00
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 ( )
2023-04-19 21:05:15 +02:00
. setFooter ( ` [ ${ getEmoji ( cream ) } ] x ${ counter } ` ) ;
2022-07-19 21:39:34 +02:00
await channel . send ( {
embeds : [ embed ]
} ) ;
}
2023-04-19 21:05:15 +02:00
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 ) ;
2022-08-26 18:46:19 +02:00
interaction . followUp ( {
2023-04-19 21:05:15 +02:00
content : ` ${ getEmoji ( cream ) } **You have ${ amount > 0 ? 'increased' : 'decreased' } the counter.** \ n \` \` \` diff \ n ${ newCount - amount } \ n ${ getSign ( amount ) } ${ Math . abs ( amount ) } \ n ${ newCount } \` \` \` `
2022-07-19 21:39:34 +02:00
} ) ;
}