mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-05 06:28:52 +00:00
rename
This commit is contained in:
1 parent
0b0c3c4326
commit
53d39e2210
3 files changed
+39
-39
No files matched your search
@@ -7,7 +7,7 @@ datasource db {
|
|||||||
url = "file:./dev.db"
|
url = "file:./dev.db"
|
||||||
}
|
}
|
||||||
|
|
||||||
model GuildSetting {
|
model GuildConfig {
|
||||||
defaultVolume Int @default(50)
|
defaultVolume Int @default(50)
|
||||||
locale String @default("en-US")
|
locale String @default("en-US")
|
||||||
actionPermissions String?
|
actionPermissions String?
|
||||||
|
|||||||
+7
-7
@@ -1,10 +1,10 @@
|
|||||||
import { EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from 'discord.js';
|
import { EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from 'discord.js';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../classes/interactions';
|
||||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../types/interactionTypes';
|
||||||
import { localizeCommand, useServerTranslator } from '../../../common/localeUtil';
|
import { localizeCommand, useServerTranslator } from '../../common/localeUtil';
|
||||||
import { Logger } from 'pino';
|
import { Logger } from 'pino';
|
||||||
import { TFunction } from 'i18next';
|
import { TFunction } from 'i18next';
|
||||||
import guildDatabaseService from '../../../services/guildDatabaseService';
|
import guildDatabaseClient from '../../services/guildDatabaseClient';
|
||||||
|
|
||||||
class SettingsCommand extends BaseSlashCommandInteraction {
|
class SettingsCommand extends BaseSlashCommandInteraction {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -63,7 +63,7 @@ class SettingsCommand extends BaseSlashCommandInteraction {
|
|||||||
logger.debug('Showing settings');
|
logger.debug('Showing settings');
|
||||||
|
|
||||||
// get settings from database
|
// get settings from database
|
||||||
const settings = await guildDatabaseService.getGuildSettings(interaction.guild!.id, executionId, interaction);
|
const settings = await guildDatabaseClient.getGuildConfig(interaction.guild!.id, executionId, interaction);
|
||||||
|
|
||||||
console.log(settings);
|
console.log(settings);
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ class SettingsCommand extends BaseSlashCommandInteraction {
|
|||||||
|
|
||||||
const volume = interaction.options.getInteger('percentage');
|
const volume = interaction.options.getInteger('percentage');
|
||||||
|
|
||||||
const settings = await guildDatabaseService.getGuildSettings(interaction.guild!.id, executionId, interaction);
|
const settings = await guildDatabaseClient.getGuildConfig(interaction.guild!.id, executionId, interaction);
|
||||||
|
|
||||||
if (!volume) {
|
if (!volume) {
|
||||||
if (!settings) {
|
if (!settings) {
|
||||||
@@ -136,7 +136,7 @@ class SettingsCommand extends BaseSlashCommandInteraction {
|
|||||||
defaultVolume: volume
|
defaultVolume: volume
|
||||||
};
|
};
|
||||||
|
|
||||||
await guildDatabaseService.createOrUpdateGuildSettings(
|
await guildDatabaseClient.createOrUpdateGuildConfig(
|
||||||
executionId,
|
executionId,
|
||||||
interaction.guild!.id,
|
interaction.guild!.id,
|
||||||
updatedSettings,
|
updatedSettings,
|
||||||
@@ -14,94 +14,94 @@ function getLogger(executionId: string, interaction?: Interaction | MessageCompo
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GuildSettings = {
|
export type GuildConfig = {
|
||||||
defaultVolume?: number;
|
defaultVolume?: number;
|
||||||
locale?: string;
|
locale?: string;
|
||||||
actionPermissions?: Record<string, string[]>;
|
actionPermissions?: Record<string, string[]>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const guildDatabaseService = {
|
const guildDatabaseClient = {
|
||||||
async getGuildSettings(
|
async getGuildConfig(
|
||||||
guildId: string,
|
guildId: string,
|
||||||
executionId: string,
|
executionId: string,
|
||||||
interaction?: Interaction | MessageComponentInteraction
|
interaction?: Interaction | MessageComponentInteraction
|
||||||
) {
|
) {
|
||||||
const logger = getLogger(executionId, interaction);
|
const logger = getLogger(executionId, interaction);
|
||||||
logger.debug(`Getting settings for guild ${guildId} from database.`);
|
logger.debug(`Getting config for guild ${guildId} from database.`);
|
||||||
const settings = await prisma.guildSetting.findUnique({
|
const config = await prisma.guildConfig.findUnique({
|
||||||
where: { guildId: guildId }
|
where: { guildId: guildId }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!settings) {
|
if (!config) {
|
||||||
logger.debug(`No settings found for guild ${guildId}.`);
|
logger.debug(`No config found for guild ${guildId}.`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
...settings,
|
...config,
|
||||||
actionPermissions: deserializeActionPermissions(settings.actionPermissions ?? '')
|
actionPermissions: deserializeActionPermissions(config.actionPermissions ?? '')
|
||||||
};
|
};
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
|
|
||||||
async createOrUpdateGuildSettings(
|
async createOrUpdateGuildConfig(
|
||||||
executionId: string,
|
executionId: string,
|
||||||
guildId: string,
|
guildId: string,
|
||||||
settingsData: GuildSettings,
|
configData: GuildConfig,
|
||||||
interaction?: Interaction | MessageComponentInteraction
|
interaction?: Interaction | MessageComponentInteraction
|
||||||
) {
|
) {
|
||||||
const logger = getLogger(executionId, interaction);
|
const logger = getLogger(executionId, interaction);
|
||||||
logger.debug(`Creating or updating guild settings for guild ${guildId} in database.`);
|
logger.debug(`Creating or updating guild config for guild ${guildId} in database.`);
|
||||||
|
|
||||||
const validSettings = validateGuildSettings(logger, settingsData);
|
const validConfig = validateGuildConfig(logger, configData);
|
||||||
return await prisma.guildSetting.upsert({
|
return await prisma.guildConfig.upsert({
|
||||||
where: { guildId: guildId },
|
where: { guildId: guildId },
|
||||||
update: {
|
update: {
|
||||||
...validSettings
|
...validConfig
|
||||||
},
|
},
|
||||||
create: {
|
create: {
|
||||||
guildId: guildId,
|
guildId: guildId,
|
||||||
...validSettings
|
...validConfig
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteGuildSettings(
|
async deleteGuildConfig(
|
||||||
executionId: string,
|
executionId: string,
|
||||||
guildId: string,
|
guildId: string,
|
||||||
interaction?: Interaction | MessageComponentInteraction
|
interaction?: Interaction | MessageComponentInteraction
|
||||||
) {
|
) {
|
||||||
const logger = getLogger(executionId, interaction);
|
const logger = getLogger(executionId, interaction);
|
||||||
logger.debug(`Deleting guild ${guildId} and settings from database.`);
|
logger.debug(`Deleting guild ${guildId} and config from database.`);
|
||||||
|
|
||||||
return await prisma.guildSetting.delete({
|
return await prisma.guildConfig.delete({
|
||||||
where: { guildId: guildId }
|
where: { guildId: guildId }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function validateGuildSettings(logger: Logger, settingsData: GuildSettings) {
|
export function validateGuildConfig(logger: Logger, configData: GuildConfig) {
|
||||||
if (settingsData.defaultVolume && (settingsData.defaultVolume < 0 || settingsData.defaultVolume > 100)) {
|
if (configData.defaultVolume && (configData.defaultVolume < 0 || configData.defaultVolume > 100)) {
|
||||||
logger.debug(`defaultVolume ${settingsData.defaultVolume} is not between 0 and 100`);
|
logger.debug(`defaultVolume ${configData.defaultVolume} is not between 0 and 100`);
|
||||||
throw new Error('defaultVolume must be between 0 and 100');
|
throw new Error('defaultVolume must be between 0 and 100');
|
||||||
}
|
}
|
||||||
|
|
||||||
// get available locale from i18next?
|
// get available locale from i18next?
|
||||||
if (settingsData.locale && !['en-US', 'no', 'ro', 'en-ES'].includes(settingsData.locale)) {
|
if (configData.locale && !['en-US', 'no', 'ro', 'en-ES'].includes(configData.locale)) {
|
||||||
logger.debug(`locale ${settingsData.locale} is not in list of available locales`);
|
logger.debug(`locale ${configData.locale} is not in list of available locales`);
|
||||||
throw new Error(`locale ${settingsData.locale} is not in list of available locales`);
|
throw new Error(`locale ${configData.locale} is not in list of available locales`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: validate actionPermissions
|
// TODO: validate actionPermissions
|
||||||
|
|
||||||
const validSettings = {
|
const validConfig = {
|
||||||
defaultVolume: settingsData.defaultVolume,
|
defaultVolume: configData.defaultVolume,
|
||||||
locale: settingsData.locale,
|
locale: configData.locale,
|
||||||
actionPermissions: serializeActionPermissions(settingsData.actionPermissions ?? {})
|
actionPermissions: serializeActionPermissions(configData.actionPermissions ?? {})
|
||||||
};
|
};
|
||||||
|
|
||||||
return validSettings;
|
return validConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializeActionPermissions(permissions: Record<string, string[]>): string {
|
function serializeActionPermissions(permissions: Record<string, string[]>): string {
|
||||||
@@ -112,4 +112,4 @@ function deserializeActionPermissions(serializedPermissions: string): Record<str
|
|||||||
return JSON.parse(serializedPermissions);
|
return JSON.parse(serializedPermissions);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default guildDatabaseService;
|
export default guildDatabaseClient;
|
||||||
Reference in new issue
Block a user