-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd:rrht): add rentsch-time command
resolves #13
- Loading branch information
Showing
6 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { successEmbed } from '#src/utils/embed-utils'; | ||
import { getGuildCollection, getGuildData } from '#src/utils/firestore-utils'; | ||
import { ApplyOptions } from '@sapphire/decorators'; | ||
import type { Args } from '@sapphire/framework'; | ||
import { send } from '@sapphire/plugin-editable-commands'; | ||
import { SubCommandPluginCommand } from '@sapphire/plugin-subcommands'; | ||
import dayjs from 'dayjs'; | ||
import { Guild, Message, MessageEmbed } from 'discord.js'; | ||
import { FieldValue } from 'firebase-admin/firestore'; | ||
import ms from 'ms'; | ||
|
||
const getCounterValue = async (guild: Guild): Promise<string> => { | ||
const guildData = await getGuildData(guild); | ||
return dayjs | ||
.duration(guildData.data()?.counters?.rentschTime ?? 0) | ||
.format('H[h] m[m]'); | ||
}; | ||
|
||
@ApplyOptions<SubCommandPluginCommand.Options>({ | ||
name: 'rentsch-time', | ||
aliases: ['rrht', 'rentschtime'], | ||
description: | ||
'The break time RRH stole from us. (!rrht help for all commands)', | ||
subCommands: [ | ||
{ input: 'get', default: true }, | ||
{ input: '+=', output: 'add' }, | ||
{ input: '-=', output: 'subtract' }, | ||
{ input: '=', output: 'set' }, | ||
'help', | ||
], | ||
enabled: true, | ||
runIn: 'GUILD_TEXT', | ||
}) | ||
export default class RentschTimeCommand extends SubCommandPluginCommand { | ||
public async get(message: Message) { | ||
const { logger } = this.container; | ||
if (!message.guild) { | ||
logger.error('No guild'); | ||
return; | ||
} | ||
|
||
await send(message, { | ||
embeds: [ | ||
new MessageEmbed() | ||
.setColor('#71cfcf') | ||
.setTitle('⏰ Lost time') | ||
.setDescription( | ||
`${await getCounterValue( | ||
message.guild, | ||
)} of time lost :(`, | ||
), | ||
], | ||
}); | ||
} | ||
|
||
public async add(message: Message, args: Args) { | ||
const { logger } = this.container; | ||
if (!message.guild) { | ||
logger.error('No guild'); | ||
return; | ||
} | ||
|
||
const time = ms(await args.rest('string')); | ||
|
||
const guildDb = await getGuildCollection(message.guild); | ||
await guildDb.set( | ||
{ | ||
counters: { | ||
rentschTime: FieldValue.increment( | ||
time, | ||
) as unknown as number, | ||
}, | ||
}, | ||
{ merge: true }, | ||
); | ||
|
||
await send(message, { | ||
embeds: [ | ||
successEmbed( | ||
`Time updated to: ${await getCounterValue(message.guild)}`, | ||
), | ||
], | ||
}); | ||
} | ||
|
||
public async subtract(message: Message, args: Args) { | ||
const { logger } = this.container; | ||
if (!message.guild) { | ||
logger.error('No guild'); | ||
return; | ||
} | ||
|
||
const time = ms(await args.rest('string')); | ||
|
||
const guildDb = await getGuildCollection(message.guild); | ||
await guildDb.set( | ||
{ | ||
counters: { | ||
rentschTime: FieldValue.increment( | ||
-time, | ||
) as unknown as number, | ||
}, | ||
}, | ||
{ merge: true }, | ||
); | ||
|
||
await send(message, { | ||
embeds: [ | ||
successEmbed( | ||
`Time updated to: ${await getCounterValue(message.guild)}`, | ||
), | ||
], | ||
}); | ||
} | ||
|
||
public async set(message: Message, args: Args) { | ||
const { logger } = this.container; | ||
if (!message.guild) { | ||
logger.error('No guild'); | ||
return; | ||
} | ||
|
||
const time = ms(await args.rest('string')); | ||
|
||
const guildDb = await getGuildCollection(message.guild); | ||
await guildDb.set({ counters: { rentschTime: time } }, { merge: true }); | ||
|
||
await send(message, { | ||
embeds: [ | ||
successEmbed( | ||
`Time updated to: ${await getCounterValue(message.guild)}`, | ||
), | ||
], | ||
}); | ||
} | ||
|
||
public async help(message: Message) { | ||
await send(message, { | ||
embeds: [ | ||
new MessageEmbed() | ||
.setColor('#71cfcf') | ||
.setTitle('Time counter commands') | ||
.setDescription('Manage the RRH time counter.') | ||
.addFields( | ||
{ | ||
name: 'Get the counter', | ||
value: '!rrht [get]', | ||
}, | ||
{ | ||
name: 'Add', | ||
value: '!rrht += time', | ||
}, | ||
{ | ||
name: 'Subtract', | ||
value: '!rrht -= time', | ||
}, | ||
{ | ||
name: 'Set specific', | ||
value: '!rrht = amount', | ||
}, | ||
), | ||
], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export interface GuildDocument { | ||
counters: Record<CounterId, number>; | ||
counters: Partial<Record<CounterId, number>>; | ||
} | ||
|
||
export type CounterId = 'beers'; | ||
export type CounterId = 'beers' | 'rentschTime'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters