Skip to content

Commit

Permalink
feat(cmd:rrht): add rentsch-time command
Browse files Browse the repository at this point in the history
resolves #13
  • Loading branch information
Lutonite committed Mar 24, 2022
1 parent 7671031 commit ec7810d
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 10 deletions.
38 changes: 32 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"erlpack": "github:discord/erlpack",
"firebase-admin": "^10.0.2",
"ioredis": "^4.28.5",
"ms": "^2.1.3",
"utf-8-validate": "^5.0.9",
"zlib-sync": "^0.1.7"
},
Expand All @@ -51,6 +52,7 @@
"@commitlint/config-conventional": "^16.2.1",
"@sapphire/ts-config": "^3.3.3",
"@types/bull": "^3.15.8",
"@types/ms": "^0.7.31",
"@types/node": "^17.0.21",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
Expand Down
4 changes: 4 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { ScheduledTaskRedisStrategy } from '@sapphire/plugin-scheduled-tasks/reg
import dayjs from 'dayjs';
import dayjsParser from 'dayjs-parser';
import 'dayjs/locale/fr-ch';
import duration from 'dayjs/plugin/duration';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import relativeTime from 'dayjs/plugin/relativeTime';
import utc from 'dayjs/plugin/utc';
import weekday from 'dayjs/plugin/weekday';
import weekOfYear from 'dayjs/plugin/weekOfYear';
Expand All @@ -20,6 +22,8 @@ dayjs.extend(utc);
dayjs.extend(weekOfYear);
dayjs.extend(weekday);
dayjs.extend(localizedFormat);
dayjs.extend(duration);
dayjs.extend(relativeTime);
dayjs.extend(dayjsParser);

initializeApp({
Expand Down
165 changes: 165 additions & 0 deletions src/commands/rentsch-time.command.ts
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',
},
),
],
});
}
}
4 changes: 2 additions & 2 deletions src/database/guild-document.ts
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';
4 changes: 2 additions & 2 deletions src/listeners/command-denied.listener.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { errorEmbed } from '#src/utils/embed-utils';
import { ApplyOptions } from '@sapphire/decorators';
import { CommandErrorPayload, Listener } from '@sapphire/framework';
import { CommandDeniedPayload, Listener } from '@sapphire/framework';

@ApplyOptions<Listener.Options>({
event: 'commandDenied',
})
export default class CommandDeniedListener extends Listener {
run(error: Error, { message }: CommandErrorPayload): unknown {
run(error: Error, { message }: CommandDeniedPayload): unknown {
return message.reply({
embeds: [
errorEmbed(
Expand Down

0 comments on commit ec7810d

Please sign in to comment.