From 51d378e4cb4584196b14132b4d330b8d370f8fb3 Mon Sep 17 00:00:00 2001 From: Peerawas Archavanuntakun Date: Thu, 6 Jan 2022 03:30:32 +0700 Subject: Setup and migrate to Prisma (#456) --- src/commands/config.ts | 38 ++++++++++++++++++++++++++++++++------ src/commands/help.ts | 8 ++++++-- src/commands/play.ts | 13 ++++++++++--- src/commands/shortcuts.ts | 43 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 83 insertions(+), 19 deletions(-) (limited to 'src/commands') 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 { 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(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 { 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 { 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'); -- cgit v1.2.3 From 892c06a1109480ba581f99b09d55c929c9a37f0a Mon Sep 17 00:00:00 2001 From: Max Isom Date: Fri, 7 Jan 2022 12:38:07 -0600 Subject: Bump discord.js --- src/commands/queue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/commands') diff --git a/src/commands/queue.ts b/src/commands/queue.ts index 1c0735c..58d6377 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -63,7 +63,7 @@ export default class implements Command { footer += ` (${currentlyPlaying.playlist.title})`; } - embed.setFooter(footer); + embed.setFooter({text: footer}); const queuePageBegin = (queuePage - 1) * PAGE_SIZE; const queuePageEnd = queuePageBegin + PAGE_SIZE; -- cgit v1.2.3 From 7090ed2a521d32d094ba4e980f8d7b09f85f34e9 Mon Sep 17 00:00:00 2001 From: Koopa Date: Mon, 17 Jan 2022 14:17:31 -0500 Subject: Add option to shuffle newly added playlist (#473) Co-authored-by: Max Isom --- src/commands/play.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/commands') diff --git a/src/commands/play.ts b/src/commands/play.ts index 710bb06..784885e 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,6 +1,7 @@ import {TextChannel, Message} from 'discord.js'; import {URL} from 'url'; import {Except} from 'type-fest'; +import shuffle from 'array-shuffle'; import {TYPES} from '../types.js'; import {inject, injectable} from 'inversify'; import {QueuedSong, STATUS} from '../services/player.js'; @@ -26,6 +27,8 @@ export default class implements Command { ['play https://open.spotify.com/playlist/37i9dQZF1DX94qaYRnkufr?si=r2fOVL_QQjGxFM5MWb84Xw', 'adds all songs from playlist to the queue'], ['play cool music immediate', 'adds the first search result for "cool music" to the front of the queue'], ['play cool music i', 'adds the first search result for "cool music" to the front of the queue'], + ['play https://www.youtube.com/watch?list=PLi9drqWffJ9FWBo7ZVOiaVy0UQQEm4IbP shuffle', 'adds the shuffled playlist to the queue'], + ['play https://www.youtube.com/watch?list=PLi9drqWffJ9FWBo7ZVOiaVy0UQQEm4IbP s', 'adds the shuffled playlist to the queue'], ]; public requiresVC = true; @@ -78,8 +81,9 @@ export default class implements Command { } const addToFrontOfQueue = args[args.length - 1] === 'i' || args[args.length - 1] === 'immediate'; + const shuffleAdditions = args[args.length - 1] === 's' || args[args.length - 1] === 'shuffle'; - const newSongs: Array> = []; + let newSongs: Array> = []; let extraMsg = ''; // Test if it's a complete URL @@ -150,6 +154,10 @@ export default class implements Command { return; } + if (shuffleAdditions) { + newSongs = shuffle(newSongs); + } + newSongs.forEach(song => { player.add({...song, addedInChannelId: msg.channel.id}, {immediate: addToFrontOfQueue}); }); -- cgit v1.2.3