Skip to content

Commit

Permalink
wip: feat(framework): slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesteery committed Jul 11, 2021
1 parent fdae04d commit 011af73
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 12,713 deletions.
12,683 changes: 0 additions & 12,683 deletions package-lock.json

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"homepage": "https://github.com/ES-Community/bot#readme",
"dependencies": {
"cron": "^1.8.2",
"discord.js": "^12.5.3",
"discord.js": "^13.0.0-dev.d433fe8.1626004976",
"dotenv": "^10.0.0",
"emoji-regex": "^9.2.2",
"got": "^11.8.2",
Expand All @@ -41,6 +41,7 @@
"@types/ws": "^7.4.6",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"discord-api-types": "^0.19.0-next.f393ba520d7d6d2aacaca7b3ca5d355fab614f6e",
"eslint": "^7.30.0",
"jest": "^27.0.6",
"nodemon": "^2.0.12",
Expand Down
1 change: 1 addition & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Dotenv.config();

const bot = new Bot({
token: process.env.DISCORD_TOKEN,
commands: path.join(__dirname, 'commands'),
crons: path.join(__dirname, 'crons'),
formatCheckers: path.join(__dirname, 'format-checkers'),
});
Expand Down
27 changes: 27 additions & 0 deletions src/commands/Hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Command, CommandOptionTypes } from '../framework';

export default new Command({
name: 'hello',
description: 'Vous salue.',
enabled: true,
options: {
etoiles: {
description: "Nombre d'étoiles accompagnant le salut.",
required: false,
type: CommandOptionTypes.Integer,
choices: [
{ name: '1 étoile', value: 1 },
{ name: '2 étoiles', value: 2 },
{ name: '3 étoiles', value: 3 },
{ name: '4 étoiles', value: 4 },
{ name: '5 étoiles', value: 5 },
] as const,
},
},
handle({ interaction, args }) {
if (args.etoiles) {
return interaction.reply(`Salut ${'⭐'.repeat(args.etoiles)}`);
}
return interaction.reply(`Salut !`);
},
});
16 changes: 9 additions & 7 deletions src/crons/CommitStrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export default new Cron({
throw new Error('found no #gif channel');
}

await channel.send(
new MessageEmbed()
.setTitle(latestCommitStrip.title)
.setURL(latestCommitStrip.link)
.setImage(latestCommitStrip.imageUrl),
);
await channel.send({
embeds: [
new MessageEmbed()
.setTitle(latestCommitStrip.title)
.setURL(latestCommitStrip.link)
.setImage(latestCommitStrip.imageUrl),
],
});
},
});

Expand Down Expand Up @@ -90,7 +92,7 @@ async function getRecentCommitStrip(now: Date): Promise<CommitStrip | null> {

const [strip] = posts;

const stripDate = new Date(strip.date_gmt + ".000Z");
const stripDate = new Date(strip.date_gmt + '.000Z');
const stripTime = stripDate.getTime();
const nowTime = now.getTime();
const thirtyMinutes = 1000 * 60 * 30;
Expand Down
37 changes: 21 additions & 16 deletions src/crons/EpicGames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ export default new Cron({
for (const game of games) {
context.logger.info(`Found a new offered game (${game.title})`);

await channel.send(
new MessageEmbed({ title: game.title, url: game.link })
.setThumbnail(game.thumbnail)
.addField(
'Début',
game.discountStartDate.toLocaleDateString('fr-FR', dateFmtOptions),
true,
)
.addField(
'Fin',
game.discountEndDate.toLocaleDateString('fr-FR', dateFmtOptions),
true,
)
.addField('Prix', `${game.originalPrice} → **Gratuit**`)
.setTimestamp(),
);
await channel.send({
embeds: [
new MessageEmbed({ title: game.title, url: game.link })
.setThumbnail(game.thumbnail)
.addField(
'Début',
game.discountStartDate.toLocaleDateString(
'fr-FR',
dateFmtOptions,
),
true,
)
.addField(
'Fin',
game.discountEndDate.toLocaleDateString('fr-FR', dateFmtOptions),
true,
)
.addField('Prix', `${game.originalPrice} → **Gratuit**`)
.setTimestamp(),
],
});
}
},
});
Expand Down
32 changes: 28 additions & 4 deletions src/framework/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { once } from 'events';
import fs from 'fs';
import path from 'path';

import { Client } from 'discord.js';
import { Client, Intents } from 'discord.js';
import pino from 'pino';

import { Cron } from './Cron';
import { Base, BaseConfig } from './Base';
import { Command, CommandManager } from './Command';
import { Cron } from './Cron';
import { FormatChecker } from './FormatChecker';

export interface BotOptions {
Expand All @@ -15,6 +16,10 @@ export interface BotOptions {
* Defaults to `process.env.DISCORD_TOKEN`.
*/
token?: string;
/**
* Directory that contains the `Command` definitions.
*/
commands?: string;
/**
* Directory that contains the `Cron` definitions.
*/
Expand All @@ -27,11 +32,14 @@ export interface BotOptions {

type Constructor<T extends Base, U extends BaseConfig> = {
new (config: U): T;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
new <Any>(config: U): T;
};

export class Bot {
private readonly token?: string;
private _client: Client | null;
private commandManager?: CommandManager;
private crons: Cron[] = [];
private formatCheckers: FormatChecker[] = [];

Expand All @@ -42,9 +50,16 @@ export class Bot {
this._client = null;
this.logger = pino();

if (options.commands) {
this.commandManager = new CommandManager(
this.loadDirectory(options.commands, 'commands', Command),
);
}

if (options.crons) {
this.crons = this.loadDirectory(options.crons, 'crons', Cron);
}

if (options.formatCheckers) {
this.formatCheckers = this.loadDirectory(
options.formatCheckers,
Expand Down Expand Up @@ -132,12 +147,18 @@ export class Bot {
if (this._client) {
throw new Error('Bot can only be started once');
}
this._client = new Client();
this._client = new Client({
intents: new Intents(['GUILDS', 'GUILD_MESSAGES']),
});

try {
await Promise.all([
this.client.login(this.token),
once(this.client, 'ready'),
]);
if (this.commandManager) {
await this.commandManager.start(this);
}
this.startCrons();
this.startFormatCheckers();
} catch (error) {
Expand All @@ -149,10 +170,13 @@ export class Bot {
/**
* Stop the bot.
*/
public stop(): void {
public async stop(): Promise<void> {
if (!this._client) {
throw new Error('Bot was not started');
}
if (this.commandManager) {
await this.commandManager.stop(this);
}
this.stopCrons();
this.stopFormatCheckers();
this._client.destroy();
Expand Down
Loading

0 comments on commit 011af73

Please sign in to comment.