message log setting

This commit is contained in:
Erik 2022-01-17 17:44:30 +02:00
parent 461ffbf4ef
commit 0cc520eb53
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589

View File

@ -0,0 +1,123 @@
const { Setting, CommandOption } = require("../../../interfaces");
const Util = require('../../../../Util.js');
class MessageLog extends Setting {
constructor(client) {
super(client, {
name: 'messagelog',
module: 'logging',
description: 'Configure message logging',
clientPermissions: ['MANAGE_WEBHOOKS'],
default: {
channel: null,
ignore: [],
bypass: [],
attachments: false,
webhook: null
},
commandOptions: [
new CommandOption({
name: 'channel',
description: 'Channel in which to output logs',
type: 'TEXT_CHANNEL'
}),
new CommandOption({
name: 'enabled',
description: 'Toggle logging on or off',
type: 'BOOLEAN'
}),
new CommandOption({
name: 'attachments',
description: 'Whether to log attachments. PREMIUM TIER 1',
type: 'BOOLEAN'
}),
new CommandOption({
name: 'list',
description: 'Select which list to modify',
type: 'STRING',
choices: [
{ name: 'bypass', value: 'bypass' },
{ name: 'ignore', value: 'ignore' },
]
}),
new CommandOption({
name: 'method',
description: 'Select which modification method to use',
type: 'STRING',
choices: [
{ name: 'add', value: 'add' },
{ name: 'remove', value: 'remove' },
{ name: 'set', value: 'set' },
{ name: 'reset', value: 'reset' },
]
}),
]
});
}
async execute(interaction, opts, setting) {
const method = opts.method?.value;
const list = opts.list?.value;
const { guild } = interaction;
setting.enabled = opts.enabled?.value || false;
if (opts.channel) {
const channel = opts.channel.value;
const perms = channel.permissionsFor(guild.me);
const missingPerms = perms.missing(['SEND_MESSAGES', 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS', 'EMBED_LINKS']);
if (missingPerms.length) return {
error: true,
index: 'ERR_CHANNEL_PERMS',
params: { channel: channel.name, perms: missingPerms.join(', ') }
};
let hook = await guild.getWebhook(this.name);
if (hook) await hook.edit({ channel });
else {
hook = await channel.createWebhook('Galactic Bot message logs',
{ reason: 'Message logs webhook.' }); // avatar: './util/GBotTest.png',
await guild.updateWebhook(this.name, hook);
setting.webhook = hook.id;
}
setting.channel = channel.id;
}
if (method && list) {
const time = 120;
const content = await this._prompt(interaction, {
message: guild.format(`SETTING_PROMPT_${method.toUpperCase()}`,
{ method, list }) + '\n' + guild.format('TIMEOUT_IN', { time }),
time
});
if (content.error) return content;
const words = Util.parseQuotes(content).map(([word]) => word),
params = [];
if (list === 'bypass') params.push(...await guild.resolveRoles(words)
.then((roles) => roles.map((role) => role.id)));
else if (list === 'ignore') params.push(...await guild.resolveChannels(words)
.then((channels) => channels.map((channel) => channel.id)));
if (!params.length) return { error: true, index: 'RESOLVE_FAIL', params: { type: list === 'bypass' ? 'roles' : 'channels' } };
this[method](setting[list], params);
}
return { error: false, index: 'SETTING_SUCCESS_ALT', params: { updated: list } };
}
}
module.exports = MessageLog;