Setup and migrate to Prisma (#456)

This commit is contained in:
Peerawas Archavanuntakun 2022-01-06 03:30:32 +07:00 committed by GitHub
parent 129d121364
commit 51d378e4cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 605 additions and 273 deletions

View file

@ -2,7 +2,7 @@ import {Client, Message, Collection} from 'discord.js';
import {inject, injectable} from 'inversify';
import ora from 'ora';
import {TYPES} from './types.js';
import {Settings, Shortcut} from './models/index.js';
import {prisma} from './utils/db.js';
import container from './inversify.config.js';
import Command from './commands/index.js';
import debug from './utils/debug.js';
@ -21,7 +21,11 @@ export default class {
private readonly token: string;
private readonly commands!: Collection<string, Command>;
constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Services.NaturalLanguage) naturalLanguage: NaturalLanguage, @inject(TYPES.Config) config: Config) {
constructor(
@inject(TYPES.Client) client: Client,
@inject(TYPES.Services.NaturalLanguage) naturalLanguage: NaturalLanguage,
@inject(TYPES.Config) config: Config,
) {
this.client = client;
this.naturalLanguage = naturalLanguage;
this.token = config.DISCORD_TOKEN;
@ -33,7 +37,9 @@ export default class {
container.getAll<Command>(TYPES.Command).forEach(command => {
const commandNames = [command.name, ...command.aliases];
commandNames.forEach(commandName => this.commands.set(commandName, command));
commandNames.forEach(commandName =>
this.commands.set(commandName, command),
);
});
this.client.on('messageCreate', async (msg: Message) => {
@ -42,7 +48,11 @@ export default class {
return;
}
const settings = await Settings.findByPk(msg.guild.id);
const settings = await prisma.setting.findUnique({
where: {
guildId: msg.guild.id,
},
});
if (!settings) {
// Got into a bad state, send owner welcome message
@ -52,27 +62,42 @@ export default class {
const {prefix, channel} = settings;
if (!msg.content.startsWith(prefix) && !msg.author.bot && msg.channel.id === channel && await this.naturalLanguage.execute(msg)) {
if (
!msg.content.startsWith(prefix)
&& !msg.author.bot
&& msg.channel.id === channel
&& (await this.naturalLanguage.execute(msg))
) {
// Natural language command handled message
return;
}
if (!msg.content.startsWith(prefix) || msg.author.bot || msg.channel.id !== channel) {
if (
!msg.content.startsWith(prefix)
|| msg.author.bot
|| msg.channel.id !== channel
) {
return;
}
let args = msg.content.slice(prefix.length).split(/ +/);
const command = args.shift()!.toLowerCase();
// Get possible shortcut
const shortcut = await Shortcut.findOne({where: {guildId: msg.guild.id, shortcut: command}});
const shortcut = await prisma.shortcut.findFirst({
where: {
guildId: msg.guild.id,
shortcut: command,
},
});
let handler: Command;
if (this.commands.has(command)) {
handler = this.commands.get(command)!;
} else if (shortcut) {
const possibleHandler = this.commands.get(shortcut.command.split(' ')[0]);
const possibleHandler = this.commands.get(
shortcut.command.split(' ')[0],
);
if (possibleHandler) {
handler = possibleHandler;
@ -93,7 +118,9 @@ export default class {
await handler.execute(msg, args);
} catch (error: unknown) {
debug(error);
await msg.channel.send(errorMsg((error as Error).message.toLowerCase()));
await msg.channel.send(
errorMsg((error as Error).message.toLowerCase()),
);
}
});
@ -102,7 +129,10 @@ export default class {
this.client.on('ready', () => {
debug(generateDependencyReport());
spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot&permissions=36752448`);
spinner.succeed(
`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''
}&scope=bot&permissions=36752448`,
);
});
this.client.on('error', console.error);