Files
discordjs-verification-bot/slashCommands/staff/settings.js
T
2024-03-21 07:12:04 +00:00

490 lines
24 KiB
JavaScript

const { ApplicationCommandType, ApplicationCommandOptionType, EmbedBuilder } = require('discord.js');
const cfg = require('../../config/config.js');
const { getActionDuration, convertActionDurationFromMsToTime, convertActionDurationFromTimeToMs } = require('../../functions/utils.js');
module.exports = {
name: 'settings',
description: 'Change the settings for the bot.',
type: ApplicationCommandType.ChatInput,
options: [
{
name: 'change',
description: 'Set the settings of the bot.',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'set',
description: 'The settings to change.',
type: ApplicationCommandOptionType.String,
required: true,
choices: [
{
name: 'Verification',
value: 'verification'
},
{
name: 'Anti New User',
value: 'anti-new-user'
}
]
},
{
name: 'enable-or-disable',
description: 'Enable or disable the setting.',
type: ApplicationCommandOptionType.String,
required: false,
choices: [
{
name: 'Enable',
value: 'true'
},
{
name: 'Disable',
value: 'false'
}
]
},
{
name: 'automod-action',
description: 'The action to take when the threshold is reached.',
type: ApplicationCommandOptionType.String,
required: false,
choices: [
{
name: 'Timeout',
value: 'Timeout'
},
{
name: 'Warn',
value: 'warn'
},
{
name: 'Kick',
value: 'kick'
},
{
name: 'Ban',
value: 'ban'
},
{
name: 'None',
value: 'none'
}
]
},
{
name: 'automod-action-reason',
description: 'The reason for the action.',
type: ApplicationCommandOptionType.String,
required: false
},
{
name: 'automod-action-threshold',
description: 'The threshold for the setting.',
type: ApplicationCommandOptionType.Integer,
required: false
},
{
name: 'automod-action-duration',
description: 'The duration for the action.',
type: ApplicationCommandOptionType.String,
required: false
}
]
},
{
name: 'check',
description: 'Check the current settings of the bot.',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'settings',
description: 'The settings to check.',
type: ApplicationCommandOptionType.String,
required: true,
choices: [
{
name: 'General',
value: 'general'
},
{
name: 'Actions',
value: 'actions'
},
{
name: 'All',
value: 'all'
}
]
}
]
},
{
name: 'reset',
description: 'Reset the settings of the bot.',
type: ApplicationCommandOptionType.Subcommand
},
{
name: 'rawcheck',
description: 'Check the raw settings of the bot.',
type: ApplicationCommandOptionType.Subcommand
}
],
default_member_permissions: ["Administrator"],
run: async (client, interaction) => {
await interaction.deferReply({ ephemeral: true });
const filter = {
guildId: interaction.guild.id
}
const subcommand = interaction.options.getSubcommand();
const settings = interaction.options.get('set')?.value;
const enableOrDisable = interaction.options.get('enable-or-disable')?.value;
const automodAction = interaction.options.get('automod-action')?.value;
const automodActionReason = interaction.options.get('automod-action-reason')?.value;
const automodActionThreshold = interaction.options.get('automod-action-threshold')?.value;
const automodActionDuration = interaction.options.get('automod-action-duration')?.value;
const checksettings = interaction.options.get('settings')?.value;
const GuildSettings = require('../../database/models/GuildSettings');
const NewUserAction = require('../../database/models/NewUserAction.js');
const VerifyFailAction = require('../../database/models/VerifyFailAction.js');
const guildSettings = await GuildSettings.findOne(filter);
const newUserAction = await NewUserAction.findOne(filter);
const verifyFailAction = await VerifyFailAction.findOne(filter);
switch (subcommand) {
case 'change':
switch (settings) {
case 'verification':
switch (automodAction) {
case undefined:
console.log('a')
switch (enableOrDisable) {
case 'true': case 'false':
console.log(enableOrDisable)
switch (guildSettings) {
case null:
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
verifyEnabled: enableOrDisable
});
await newGuildSettings.save();
break;
default:
guildSettings.verifyEnabled = enableOrDisable;
await guildSettings.save();
break;
}
break;
}
break;
case 'none':
console.log('b')
await VerifyFailAction.find(filter).deleteMany();
break;
default:
console.log('c')
switch (enableOrDisable) {
case 'true': case 'false':
switch (guildSettings) {
case null:
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
verifyEnabled: enableOrDisable
});
await newGuildSettings.save();
break;
default:
guildSettings.verifyEnabled = enableOrDisable;
await guildSettings.save();
break;
}
break;
}
switch (verifyFailAction) {
case null:
const newVerifyFailAction = new VerifyFailAction({
guildId: interaction.guild.id,
action: automodAction,
reason: automodActionReason || 'User failed to verify.',
threshold: automodActionThreshold || 3,
duration: automodActionDuration || '7d'
});
await newVerifyFailAction.save();
break;
default:
verifyFailAction.action = automodAction || verifyFailAction?.action;
verifyFailAction.reason = `${automodActionReason?.toString()}` || verifyFailAction?.reason || 'User failed to verify.';
verifyFailAction.threshold = automodActionThreshold || verifyFailAction?.threshold || 3;
verifyFailAction.duration = automodActionDuration || verifyFailAction?.duration || '7d';
await verifyFailAction.save();
break;
}
break;
}
break;
case 'anti-new-user':
switch (automodAction) {
case undefined:
switch (enableOrDisable) {
case 'true': case 'false':
switch (guildSettings) {
case null:
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
antiNewUserEnabled: enableOrDisable
});
await newGuildSettings.save();
break;
default:
guildSettings.antiNewUserEnabled = enableOrDisable;
await guildSettings.save();
break;
}
break;
}
break;
case 'none':
await NewUserAction.find(filter).deleteMany();
break;
default:
switch (enableOrDisable) {
case 'true': case 'false':
switch (guildSettings) {
case null:
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
antiNewUserEnabled: enableOrDisable
});
await newGuildSettings.save();
break;
default:
guildSettings.antiNewUserEnabled = enableOrDisable;
await guildSettings.save();
break;
}
break;
}
switch (newUserAction) {
case null:
const newNewUserAction = new NewUserAction({
guildId: interaction.guild.id,
action: automodAction,
reason: automodActionReason || 'User account age is way too low.',
threshold: automodActionThreshold || 3,
duration: automodActionDuration || '7d'
});
await newNewUserAction.save();
break;
default:
newUserAction.action = automodAction || newUserAction?.action;
newUserAction.reason = `${automodActionReason?.toString()}` || newUserAction?.reason || 'User account age is way too low.';
newUserAction.threshold = automodActionThreshold || newUserAction?.threshold || 3;
newUserAction.duration = automodActionDuration || newUserAction?.duration || '7d';
await newUserAction.save();
break;
}
break;
}
break;
}
await interaction.editReply({ content: 'Settings have been updated.' });
break;
case 'check':
function verification() {
if (guildSettings?.verifyEnabled === 'true') {
return 'Enabled';
} else if (guildSettings?.verifyEnabled === 'false') {
return 'Disabled';
} else {
return 'Disabled';
}
}
function antiNewUser() {
if (guildSettings?.antiNewUserEnabled === 'true') {
return 'Enabled';
} else if (guildSettings?.antiNewUserEnabled === 'false') {
return 'Disabled';
} else {
return 'Disabled';
}
}
const embed = new EmbedBuilder()
.setTitle('Settings')
.addFields({
name: 'Verification',
value: `${verification()}`,
},
{
name: 'Anti New User',
value: `${antiNewUser()}`,
})
.setColor("Gold");
const embedd = new EmbedBuilder()
.setTitle('Settings')
.addFields({
name: 'Verification Auto Action',
value: `${verifyFailAction ? `Action: ${verifyFailAction?.action.toUpperCase()}\nReason: ${verifyFailAction?.reason}\nThreshold: ${verifyFailAction?.threshold}\nDuration: ${await getActionDuration(verifyFailAction?.duration)}` : "None"}`,
},
{
name: 'Anti New User Auto Action',
value: `${newUserAction ? `Action: ${newUserAction?.action.toUpperCase()}\nReason: ${newUserAction?.reason}\nThreshold: ${newUserAction?.threshold}\nDuration: ${await getActionDuration(newUserAction?.duration)}` : "None"}`,
})
.setColor("Gold");
console.log(verifyFailAction?.duration, await getActionDuration(verifyFailAction?.duration), await convertActionDurationFromTimeToMs(verifyFailAction?.duration));
console.log(newUserAction?.duration, await getActionDuration(newUserAction?.duration), await convertActionDurationFromTimeToMs(newUserAction?.duration));
switch (checksettings) {
case 'general':
await interaction.editReply({ embeds: [embed] });
break;
case 'actions':
await interaction.editReply({ embeds: [embedd] });
break;
case 'all':
await interaction.editReply({ embeds: [embed, embedd] });
break;
}
break;
case 'reset':
await GuildSettings.find(filter).deleteMany();
await NewUserAction.find(filter).deleteMany();
await VerifyFailAction.find(filter).deleteMany();
await interaction.editReply({ content: 'Settings have been reset.' });
break;
case 'rawcheck':
console.log(guildSettings);
console.log(verifyFailAction);
console.log(newUserAction);
await interaction.editReply({ content: 'Check the console.' });
break;
}
/*
if (settings === 'verification') {
console.log('a')
if (automodAction === undefined) {
console.log('i')
if (enableOrDisable !== undefined) {
if (guildSettings === null) {
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
verifyEnabled: enableOrDisable
});
await newGuildSettings.save();
} else {
guildSettings.verifyEnabled = enableOrDisable;
await guildSettings.save();
}
} else if (enableOrDisable === undefined) {
if (verifyFailAction === null) {
const newVerifyFailAction = new VerifyFailAction({
guildId: interaction.guild.id,
action: automodAction,
reason: `${automodActionReason.toString()}` || 'No reason provided.',
threshold: automodActionThreshold || 3,
duration: automodActionDuration || 7
});
await newVerifyFailAction.save();
} else if (verifyFailAction !== null) {
verifyFailAction.action = automodAction;
verifyFailAction.reason = `${automodActionReason.toString()}` || 'No reason provided.';
verifyFailAction.threshold = automodActionThreshold || 3;
verifyFailAction.duration = automodActionDuration || 7;
await verifyFailAction.save();
} else if (verifyFailAction?.action !== 'none' && automodAction === 'none') {
await VerifyFailAction.find(filter).deleteMany();
}
}
}
} else if (settings === 'anti-new-user') {
if (enableOrDisable !== undefined && automodAction === undefined) {
if (guildSettings === null) {
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
antiNewUserEnabled: enableOrDisable
});
await newGuildSettings.save();
} else {
guildSettings.antiNewUserEnabled = enableOrDisable;
await guildSettings.save();
}
} else if (enableOrDisable !== undefined && automodAction !== 'none') {
if (guildSettings === null) {
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
antiNewUserEnabled: enableOrDisable
});
await newGuildSettings.save();
} else {
guildSettings.antiNewUserEnabled = enableOrDisable;
await guildSettings.save();
}
if (newUserAction === null) {
const newNewUserAction = new NewUserAction({
guildId: interaction.guild.id,
action: automodAction,
reason: `${automodActionReason}` || 'No reason provided.',
threshold: automodActionThreshold || 3,
duration: automodActionDuration || 0
});
await newNewUserAction.save();
} else {
newUserAction.action = automodAction;
newUserAction.reason = `${automodActionReason}` || 'No reason provided.';
newUserAction.threshold = automodActionThreshold || 3;
newUserAction.duration = automodActionDuration || 0;
await newUserAction.save();
}
} else if (enableOrDisable !== undefined && automodAction === 'none') {
if (guildSettings === null) {
const newGuildSettings = new GuildSettings({
guildId: interaction.guild.id,
antiNewUserEnabled: enableOrDisable
});
await newGuildSettings.save();
} else {
guildSettings.antiNewUserEnabled = enableOrDisable;
await guildSettings.save();
}
if (newUserAction?.action !== 'none') {
await NewUserAction.find(filter).deleteMany();
}
}
}
await interaction.editReply({ content: 'Settings have been updated.' });
} else if (subcommand === 'check') {
const embed = new EmbedBuilder()
.setTitle('Settings')
.addFields({
name: 'Verification',
value: `${guildSettings?.verifyEnabled ? 'Enabled' : 'Disabled'}`,
},
{
name: 'Verification Auto Action',
value: `${verifyFailAction ? `Action: ${verifyFailAction?.action.toUpperCase()}\nReason: ${verifyFailAction?.reason}\nThreshold: ${verifyFailAction?.threshold}\nDuration: ${verifyFailAction?.duration}` : "None"}`,
},
{
name: 'Anti New User',
value: `${guildSettings?.antiNewUserEnabled ? 'Enabled' : 'Disabled'}`,
},
{
name: 'Anti New User Auto Action',
value: `${newUserAction ? `Action: ${newUserAction?.action.toUpperCase()}\nReason: ${newUserAction?.reason}\nThreshold: ${newUserAction?.threshold}\nDuration: ${newUserAction?.duration}` : "None"}`,
})
.setColor("Gold");
await interaction.editReply({ embeds: [embed] });
} else if (subcommand === 'reset') {
await GuildSettings.find(filter).deleteMany();
await NewUserAction.find(filter).deleteMany();
await VerifyFailAction.find(filter).deleteMany();
await interaction.editReply({ content: 'Settings have been reset.' });
}
*/
}
}