Create guild settings if not found (#911)

Co-authored-by: Max Isom <codetheweb@users.noreply.github.com>
This commit is contained in:
Federico Rapetti 2023-03-04 22:39:33 +01:00 committed by GitHub
parent 6926e39c56
commit 02ee8aefc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 27 deletions

View file

@ -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

View file

@ -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,

View file

@ -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<Setting> {
export async function createGuildSettings(guildId: string): Promise<Setting> {
return prisma.setting.upsert({
where: {
guildId: guild.id,
guildId,
},
create: {
guildId: guild.id,
guildId,
},
update: {},
});
}
export default async (guild: Guild): Promise<void> => {
await createGuildSettings(guild);
await createGuildSettings(guild.id);
const config = container.get<Config>(TYPES.Config);

View file

@ -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<void> => {
const playerManager = container.get<PlayerManager>(TYPES.Managers.Player);
@ -12,11 +12,7 @@ export default async (oldState: VoiceState, _: VoiceState): Promise<void> => {
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)) {

View file

@ -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;

View file

@ -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) {

View file

@ -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<Setting> {
const config = await prisma.setting.findUnique({where: {guildId}});
if (!config) {
return createGuildSettings(guildId);
}
return config;
}