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/commands | |
| parent | 129d121364c7e976c7bf5e2da3976da230058d77 (diff) | |
| download | muse-51d378e4cb4584196b14132b4d330b8d370f8fb3.tar.xz muse-51d378e4cb4584196b14132b4d330b8d370f8fb3.zip | |
Setup and migrate to Prisma (#456)
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/config.ts | 38 | ||||
| -rw-r--r-- | src/commands/help.ts | 8 | ||||
| -rw-r--r-- | src/commands/play.ts | 13 | ||||
| -rw-r--r-- | src/commands/shortcuts.ts | 43 |
4 files changed, 83 insertions, 19 deletions
diff --git a/src/commands/config.ts b/src/commands/config.ts index dbe0949..20dafed 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -1,8 +1,8 @@ import {TextChannel, Message, GuildChannel, ThreadChannel} from 'discord.js'; import {injectable} from 'inversify'; -import {Settings} from '../models/index.js'; import errorMsg from '../utils/error-msg.js'; import Command from '.'; +import {prisma} from '../utils/db.js'; @injectable() export default class implements Command { @@ -17,9 +17,13 @@ export default class implements Command { public async execute(msg: Message, args: string []): Promise<void> { if (args.length === 0) { // Show current settings - const settings = await Settings.findByPk(msg.guild!.id); + const settings = await prisma.setting.findUnique({ + where: { + guildId: msg.guild!.id, + }, + }); - if (settings) { + if (settings?.channel) { let response = `prefix: \`${settings.prefix}\`\n`; // eslint-disable-next-line @typescript-eslint/no-base-to-string response += `channel: ${msg.guild!.channels.cache.get(settings.channel)!.toString()}\n`; @@ -47,7 +51,14 @@ export default class implements Command { case 'prefix': { const newPrefix = args[1]; - await Settings.update({prefix: newPrefix}, {where: {guildId: msg.guild!.id}}); + await prisma.setting.update({ + where: { + guildId: msg.guild!.id, + }, + data: { + prefix: newPrefix, + }, + }); await msg.channel.send(`👍 prefix updated to \`${newPrefix}\``); break; @@ -63,7 +74,14 @@ export default class implements Command { } if (channel && channel.type === 'GUILD_TEXT') { - await Settings.update({channel: channel.id}, {where: {guildId: msg.guild!.id}}); + await prisma.setting.update({ + where: { + guildId: msg.guild!.id, + }, + data: { + channel: channel.id, + }, + }); await Promise.all([ (channel as TextChannel).send('hey apparently I\'m bound to this channel now'), @@ -83,7 +101,15 @@ export default class implements Command { return; } - await Settings.update({playlistLimit}, {where: {guildId: msg.guild!.id}}); + await prisma.setting.update({ + where: { + guildId: msg.guild!.id, + }, + data: { + playlistLimit, + }, + }); + await msg.channel.send(`👍 playlist-limit updated to ${playlistLimit}`); break; } diff --git a/src/commands/help.ts b/src/commands/help.ts index 7058efd..ebac00b 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -2,8 +2,8 @@ import {Message, Util} from 'discord.js'; import {injectable} from 'inversify'; import Command from '.'; import {TYPES} from '../types.js'; -import {Settings} from '../models/index.js'; import container from '../inversify.config.js'; +import {prisma} from '../utils/db.js'; @injectable() export default class implements Command { @@ -21,7 +21,11 @@ export default class implements Command { this.commands = container.getAll<Command>(TYPES.Command); } - const settings = await Settings.findOne({where: {guildId: msg.guild!.id}}); + const settings = await prisma.setting.findUnique({ + where: { + guildId: msg.guild!.id, + }, + }); if (!settings) { return; diff --git a/src/commands/play.ts b/src/commands/play.ts index 6561fa9..710bb06 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -10,7 +10,7 @@ import LoadingMessage from '../utils/loading-message.js'; import errorMsg from '../utils/error-msg.js'; import Command from '.'; import GetSongs from '../services/get-songs.js'; -import Settings from '../models/settings.js'; +import {prisma} from '../utils/db.js'; @injectable() export default class implements Command { @@ -41,8 +41,15 @@ export default class implements Command { // eslint-disable-next-line complexity public async execute(msg: Message, args: string[]): Promise<void> { const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!); - const settings = await Settings.findByPk(msg.guild!.id); - const {playlistLimit} = settings!; + const setting = await prisma.setting.findUnique({ + where: { + guildId: msg.guild!.id, + }}); + if (!setting) { + throw new Error(`Couldn't find settings for guild ${msg.guild!.id}`); + } + + const {playlistLimit} = setting; const res = new LoadingMessage(msg.channel as TextChannel); await res.start(); diff --git a/src/commands/shortcuts.ts b/src/commands/shortcuts.ts index 929a5c1..e40d10a 100644 --- a/src/commands/shortcuts.ts +++ b/src/commands/shortcuts.ts @@ -1,8 +1,8 @@ import {Message} from 'discord.js'; import {injectable} from 'inversify'; -import {Shortcut, Settings} from '../models/index.js'; import errorMsg from '../utils/error-msg.js'; import Command from '.'; +import {prisma} from '../utils/db.js'; @injectable() export default class implements Command { @@ -18,7 +18,11 @@ export default class implements Command { public async execute(msg: Message, args: string []): Promise<void> { if (args.length === 0) { // Get shortcuts for guild - const shortcuts = await Shortcut.findAll({where: {guildId: msg.guild!.id}}); + const shortcuts = await prisma.shortcut.findMany({ + where: { + guildId: msg.guild!.id, + }, + }); if (shortcuts.length === 0) { await msg.channel.send('no shortcuts exist'); @@ -26,7 +30,11 @@ export default class implements Command { } // Get prefix for guild - const settings = await Settings.findOne({where: {guildId: msg.guild!.id}}); + const settings = await prisma.setting.findUnique({ + where: { + guildId: msg.guild!.id, + }, + }); if (!settings) { return; @@ -48,7 +56,12 @@ export default class implements Command { switch (action) { case 'set': { - const shortcut = await Shortcut.findOne({where: {guildId: msg.guild!.id, shortcut: shortcutName}}); + const shortcut = await prisma.shortcut.findFirst({ + where: { + guildId: msg.guild!.id, + shortcut: shortcutName, + }, + }); const command = args.slice(2).join(' '); @@ -60,10 +73,15 @@ export default class implements Command { return; } - await shortcut.update(newShortcut); + await prisma.shortcut.update({ + where: { + id: shortcut.id, + }, + data: newShortcut, + }); await msg.channel.send('shortcut updated'); } else { - await Shortcut.create(newShortcut); + await prisma.shortcut.create({data: newShortcut}); await msg.channel.send('shortcut created'); } @@ -72,7 +90,12 @@ export default class implements Command { case 'delete': { // Check if shortcut exists - const shortcut = await Shortcut.findOne({where: {guildId: msg.guild!.id, shortcut: shortcutName}}); + const shortcut = await prisma.shortcut.findFirst({ + where: { + guildId: msg.guild!.id, + shortcut: shortcutName, + }, + }); if (!shortcut) { await msg.channel.send(errorMsg('shortcut doesn\'t exist')); @@ -85,7 +108,11 @@ export default class implements Command { return; } - await shortcut.destroy(); + await prisma.shortcut.delete({ + where: { + id: shortcut.id, + }, + }); await msg.channel.send('shortcut deleted'); |
