diff options
| author | Peerawas Archavanuntakun <[email protected]> | 2022-01-06 03:30:32 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-05 14:30:32 -0600 |
| commit | 51d378e4cb4584196b14132b4d330b8d370f8fb3 (patch) | |
| tree | 176959c304bcb28ef33df0a3546bb4b88d691b09 /src/bot.ts | |
| parent | 129d121364c7e976c7bf5e2da3976da230058d77 (diff) | |
| download | muse-51d378e4cb4584196b14132b4d330b8d370f8fb3.tar.xz muse-51d378e4cb4584196b14132b4d330b8d370f8fb3.zip | |
Setup and migrate to Prisma (#456)
Diffstat (limited to 'src/bot.ts')
| -rw-r--r-- | src/bot.ts | 52 |
1 files changed, 41 insertions, 11 deletions
@@ -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); |
