From 02ee8aefc86b3a9902827874260d59115e73af13 Mon Sep 17 00:00:00 2001 From: Federico Rapetti Date: Sat, 4 Mar 2023 22:39:33 +0100 Subject: [PATCH] Create guild settings if not found (#911) Co-authored-by: Max Isom --- CHANGELOG.md | 1 + src/commands/config.ts | 7 ++----- src/events/guild-create.ts | 8 ++++---- src/events/voice-state-update.ts | 8 ++------ src/services/add-query-to-queue.ts | 8 ++------ src/services/player.ts | 8 ++------ src/utils/get-guild-settings.ts | 12 ++++++++++++ 7 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 src/utils/get-guild-settings.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0386d30..b7aa1c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed +- Create the guild settings when not found instead of returning an error - Add temporary workaround to avoid VoiceConnection being stuck in signalling state ## [2.2.0] - 2023-02-26 diff --git a/src/commands/config.ts b/src/commands/config.ts index 9ed78d7..158f4f4 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -3,6 +3,7 @@ import {ChatInputCommandInteraction, EmbedBuilder, PermissionFlagsBits} from 'di import {injectable} from 'inversify'; import {prisma} from '../utils/db.js'; import Command from './index.js'; +import {getGuildSettings} from '../utils/get-guild-settings'; @injectable() export default class implements Command { @@ -96,11 +97,7 @@ export default class implements Command { case 'get': { const embed = new EmbedBuilder().setTitle('Config'); - const config = await prisma.setting.findUnique({where: {guildId: interaction.guild!.id}}); - - if (!config) { - throw new Error('no config found'); - } + const config = await getGuildSettings(interaction.guild!.id); const settingsToShow = { 'Playlist Limit': config.playlistLimit, diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts index af195ce..d566c13 100644 --- a/src/events/guild-create.ts +++ b/src/events/guild-create.ts @@ -8,20 +8,20 @@ import {REST} from '@discordjs/rest'; import {Setting} from '@prisma/client'; import registerCommandsOnGuild from '../utils/register-commands-on-guild.js'; -export async function createGuildSettings(guild: Guild): Promise { +export async function createGuildSettings(guildId: string): Promise { return prisma.setting.upsert({ where: { - guildId: guild.id, + guildId, }, create: { - guildId: guild.id, + guildId, }, update: {}, }); } export default async (guild: Guild): Promise => { - await createGuildSettings(guild); + await createGuildSettings(guild.id); const config = container.get(TYPES.Config); diff --git a/src/events/voice-state-update.ts b/src/events/voice-state-update.ts index 60a40b1..e941a70 100644 --- a/src/events/voice-state-update.ts +++ b/src/events/voice-state-update.ts @@ -3,7 +3,7 @@ import container from '../inversify.config.js'; import {TYPES} from '../types.js'; import PlayerManager from '../managers/player.js'; import {getSizeWithoutBots} from '../utils/channels.js'; -import {prisma} from '../utils/db.js'; +import {getGuildSettings} from '../utils/get-guild-settings'; export default async (oldState: VoiceState, _: VoiceState): Promise => { const playerManager = container.get(TYPES.Managers.Player); @@ -12,11 +12,7 @@ export default async (oldState: VoiceState, _: VoiceState): Promise => { if (player.voiceConnection) { const voiceChannel: VoiceChannel = oldState.guild.channels.cache.get(player.voiceConnection.joinConfig.channelId!) as VoiceChannel; - const settings = await prisma.setting.findUnique({where: {guildId: player.guildId}}); - - if (!settings) { - throw new Error('Could not find settings for guild'); - } + const settings = await getGuildSettings(player.guildId); const {leaveIfNoListeners} = settings; if (!voiceChannel || (getSizeWithoutBots(voiceChannel) === 0 && leaveIfNoListeners)) { diff --git a/src/services/add-query-to-queue.ts b/src/services/add-query-to-queue.ts index 15192b4..dc972cd 100644 --- a/src/services/add-query-to-queue.ts +++ b/src/services/add-query-to-queue.ts @@ -6,9 +6,9 @@ import {TYPES} from '../types.js'; import GetSongs from '../services/get-songs.js'; import {SongMetadata, STATUS} from './player.js'; import PlayerManager from '../managers/player.js'; -import {prisma} from '../utils/db.js'; import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; import {getMemberVoiceChannel, getMostPopularVoiceChannel} from '../utils/channels.js'; +import {getGuildSettings} from '../utils/get-guild-settings'; @injectable() export default class AddQueryToQueue { @@ -34,11 +34,7 @@ export default class AddQueryToQueue { const [targetVoiceChannel] = getMemberVoiceChannel(interaction.member as GuildMember) ?? getMostPopularVoiceChannel(interaction.guild!); - const settings = await prisma.setting.findUnique({where: {guildId}}); - - if (!settings) { - throw new Error('Could not find settings for guild'); - } + const settings = await getGuildSettings(guildId); const {playlistLimit} = settings; diff --git a/src/services/player.ts b/src/services/player.ts index 0b57ad7..9c1a122 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -18,7 +18,7 @@ import { } from '@discordjs/voice'; import FileCacheProvider from './file-cache.js'; import debug from '../utils/debug.js'; -import {prisma} from '../utils/db.js'; +import {getGuildSettings} from '../utils/get-guild-settings'; export enum MediaSource { Youtube, @@ -272,11 +272,7 @@ export default class { this.audioPlayer?.stop(); this.status = STATUS.IDLE; - const settings = await prisma.setting.findUnique({where: {guildId: this.guildId}}); - - if (!settings) { - throw new Error('Could not find settings for guild'); - } + const settings = await getGuildSettings(this.guildId); const {secondsToWaitAfterQueueEmpties} = settings; if (secondsToWaitAfterQueueEmpties !== 0) { diff --git a/src/utils/get-guild-settings.ts b/src/utils/get-guild-settings.ts new file mode 100644 index 0000000..f001aab --- /dev/null +++ b/src/utils/get-guild-settings.ts @@ -0,0 +1,12 @@ +import {Setting} from '@prisma/client'; +import {prisma} from './db'; +import {createGuildSettings} from '../events/guild-create'; + +export async function getGuildSettings(guildId: string): Promise { + const config = await prisma.setting.findUnique({where: {guildId}}); + if (!config) { + return createGuildSettings(guildId); + } + + return config; +}