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 = '🪣' ;
2023-04-19 21:15:01 +02:00
export const CREAM_EMOJI = '🥛' ;
2023-04-19 21:05:15 +02:00
function getCounterFile ( cream : boolean ) {
return cream ? counterFileCream : counterFile ;
}
2022-07-19 21:39:34 +02:00
2023-04-19 21:12:45 +02:00
let pissCounter = 0 ;
let creamCounter = 0 ;
2022-07-19 21:39:34 +02:00
2023-04-19 21:05:15 +02:00
export async function initializeCounter ( cream : boolean ) {
const filename = getCounterFile ( cream ) ;
if ( await exists ( filename ) ) {
2023-04-19 21:12:45 +02:00
const count = parseInt ( await fsp . readFile ( filename , 'utf8' ) ) ;
if ( cream ) {
creamCounter = count ;
} else {
pissCounter = count ;
}
2022-07-19 21:39:34 +02:00
} else {
2023-04-19 21:12:45 +02:00
if ( cream ) {
creamCounter = 0 ;
} else {
pissCounter = 0 ;
}
2023-04-19 21:05:15 +02:00
await saveCounter ( cream ) ;
2022-07-19 21:39:34 +02:00
}
}
2023-04-19 21:15:01 +02:00
export function getCounter ( cream : boolean ) {
2023-04-19 21:12:45 +02:00
return cream ? creamCounter : pissCounter ;
}
2023-04-19 21:05:15 +02:00
async function saveCounter ( cream : boolean ) {
2023-04-19 21:12:45 +02:00
fsp . writeFile ( getCounterFile ( cream ) , Math . trunc ( getCounter ( cream ) ) . 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 ) {
2023-04-19 21:12:45 +02:00
if ( cream ) {
creamCounter += delta ;
} else {
pissCounter += delta ;
}
2023-04-19 21:05:15 +02:00
await saveCounter ( cream ) ;
2023-04-19 21:12:45 +02:00
return getCounter ( cream ) ;
2022-07-19 21:39:34 +02:00
}
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:12:45 +02:00
const content = ` [ ${ getEmoji ( cream ) } ] x ${ getCounter ( cream ) } ` ;
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:12:45 +02:00
. setFooter ( ` [ ${ getEmoji ( cream ) } ] x ${ getCounter ( cream ) } ` ) ;
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
} ) ;
}