Implement functionality to reset settings

This commit is contained in:
Erik 2024-10-28 19:14:13 +02:00
parent ad685d0143
commit 5d7b3c1cee
7 changed files with 89 additions and 14 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-duplicate-enum-values */
// Galactic - Discord moderation bot // Galactic - Discord moderation bot
// Copyright (C) 2024 Navy.gif // Copyright (C) 2024 Navy.gif
@ -307,12 +308,8 @@ export type SettingAction = {
} }
export type SettingMethod = 'add' | 'remove' | 'edit' | 'list' | 'reset' export type SettingMethod = 'add' | 'remove' | 'edit' | 'list' | 'reset'
export type SettingTypeResolve = 'USER' | 'GUILD'; export type SettingTypeResolve = 'USER' | 'GUILD';
export type SettingEmojiOption = { export type SettingEmojiOption = object
// export type SettingApiDefinitions = object
}
export type SettingApiDefinitions = {
//
}
export type BaseSetting = object export type BaseSetting = object
export type SettingOptions<IsGuildSetting extends boolean> = { export type SettingOptions<IsGuildSetting extends boolean> = {
name?: string, name?: string,
@ -372,9 +369,7 @@ export type AdditionalInfractionData = {
seconds?: number, seconds?: number,
} }
export type InfractionFlags = { export type InfractionFlags = object
//
}
export type InfractionChangeType = export type InfractionChangeType =
| 'UNRESOLVE' | 'UNRESOLVE'

View File

@ -380,7 +380,7 @@ class DiscordClient extends Client
} }
} }
this.#defaultConfig[type] = def; this.#defaultConfig[type] = def;
return JSON.parse(JSON.stringify(def)); return Util.clone(def);
} }
// Helper function to pass options to the logger in a unified way // Helper function to pass options to the logger in a unified way

View File

@ -15,12 +15,14 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { APIEmbed, APIEmbedField } from 'discord.js'; import { APIEmbed, APIEmbedField } from 'discord.js';
import { CommandOptionType } from '../../../../../@types/Client.js'; import { CommandOptionType, CommandParams } from '../../../../../@types/Client.js';
import DiscordClient from '../../../DiscordClient.js'; import DiscordClient from '../../../DiscordClient.js';
import SlashCommand from '../../../interfaces/commands/SlashCommand.js'; import SlashCommand from '../../../interfaces/commands/SlashCommand.js';
import InvokerWrapper from '../../wrappers/InvokerWrapper.js'; import InvokerWrapper from '../../wrappers/InvokerWrapper.js';
import Util from '../../../../utilities/Util.js'; import Util from '../../../../utilities/Util.js';
import { EmbedDefaultColor, EmbedLimits, ZeroWidthChar } from '../../../../constants/Constants.js'; import { EmbedDefaultColor, EmbedLimits, ZeroWidthChar } from '../../../../constants/Constants.js';
import CommandError from '../../../interfaces/CommandError.js';
import Setting from '../../../interfaces/Setting.js';
class SettingsCommand extends SlashCommand class SettingsCommand extends SlashCommand
{ {
@ -44,15 +46,46 @@ class SettingsCommand extends SlashCommand
name: 'export', name: 'export',
description: 'Display ALL current configurations (big embed)', description: 'Display ALL current configurations (big embed)',
type: CommandOptionType.SUB_COMMAND type: CommandOptionType.SUB_COMMAND
}, {
name: 'reset',
description: 'Reset the setting',
type: CommandOptionType.SUB_COMMAND,
options: [
{
name: 'setting',
description: 'Setting to reset',
type: CommandOptionType.COMPONENT
},
{
name: 'all',
description: 'Reset all settings',
type: CommandOptionType.BOOLEAN,
flag: true,
valueOptional: true,
defaultValue: true,
}
]
} }
], ],
memberPermissions: [ 'ManageGuild' ] memberPermissions: [ 'ManageGuild' ]
}); });
} }
async execute (invoker: InvokerWrapper) async execute (invoker: InvokerWrapper, opts: CommandParams)
{ {
const subcmd = invoker.subcommand!.name; const subcmd = invoker.subcommand!.name;
if (subcmd === 'reset')
{
if (opts.all && opts.all.value)
return this.#resetSettings(invoker);
if (!opts.setting)
throw new CommandError(invoker, { index: 'COMMAND_SETTINGS_MANDATORY_MISSING' });
const setting = opts.setting.value;
if (!(setting instanceof Setting))
throw new CommandError(invoker, { index: 'COMMAND_SETTINGS_INVALID' });
return this.#resetSetting(invoker, setting);
}
if (subcmd === 'list') if (subcmd === 'list')
return this.#listSettings(invoker); return this.#listSettings(invoker);
else if (subcmd === 'current') else if (subcmd === 'current')
@ -162,6 +195,31 @@ class SettingsCommand extends SlashCommand
// //
} }
async #resetSetting (invoker: InvokerWrapper<true>, setting: Setting)
{
const { guild } = invoker;
const def = setting.default;
guild._settings[setting.name] = def[setting.name];
await guild.updateSettings(def);
await invoker.reply({
index: 'COMMAND_SETTINGS_RESET_SUCCESS',
params: { setting: setting.name },
emoji: 'success'
});
}
async #resetSettings (invoker: InvokerWrapper<true>)
{
const { guild } = invoker;
const defaults = this.client.defaultConfig('GUILD');
guild._settings = defaults;
await guild.updateSettings(defaults);
await invoker.reply({
index: 'COMMAND_SETTINGS_RESET_ALL',
emoji: 'success'
});
}
} }
export default SettingsCommand; export default SettingsCommand;

View File

@ -59,7 +59,7 @@ class ModerationLog extends Setting
infractions: Infractions, infractions: Infractions,
anonymous: false, anonymous: false,
enabled: false, enabled: false,
autoLog: true, autolog: true,
}, },
definitions: { definitions: {
channel: 'GUILD_TEXT', channel: 'GUILD_TEXT',
@ -193,7 +193,7 @@ class ModerationLog extends Setting
inline: true inline: true
}, { }, {
name: 'GENERAL_CHANNEL', name: 'GENERAL_CHANNEL',
value: `<#${setting.channel}>`, value: setting.channel ? `<#${setting.channel}>` : '**N/A**',
inline: true inline: true
}, { }, {
name: 'GENERAL_INFRACTIONS', name: 'GENERAL_INFRACTIONS',

View File

@ -551,6 +551,11 @@ class GuildWrapper
return this.#settings; return this.#settings;
} }
set _settings (val: GuildSettings)
{
this.#settings = val;
}
// Primarily used by the API // Primarily used by the API
toJSON (): GuildJSON toJSON (): GuildJSON
{ {

View File

@ -69,6 +69,18 @@ Configure utility settings.
[COMMAND_ADMINISTRATION_HELP] [COMMAND_ADMINISTRATION_HELP]
Configure administrative settings. Configure administrative settings.
[COMMAND_SETTINGS_MANDATORY_MISSING]
Missing mandatory option "setting"
[COMMAND_SETTINGS_INVALID]
The provided component does not seem to be a setting
[COMMAND_SETTINGS_RESET_ALL]
Successfully reset all settings.
[COMMAND_SETTINGS_RESET_SUCCESS]
Successfully reset {setting} setting.
// Import // Import
[COMMAND_IMPORT_HELP] [COMMAND_IMPORT_HELP]
Import configuration and data from old versions of the bot. Import configuration and data from old versions of the bot.

View File

@ -259,6 +259,11 @@ class Util
return result; return result;
} }
static clone (object: object)
{
return JSON.parse(JSON.stringify(object));
}
static wait (ms: number) static wait (ms: number)
{ {
return this.delayFor(ms); return this.delayFor(ms);