Skip to content

Commit

Permalink
Multiple enhancements, Added settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
JH220 committed Jul 21, 2024
1 parent ae971cf commit 72c6461
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 115 deletions.
17 changes: 16 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,27 @@
"COMMAND_CLEARALL_MISSING_CATEGORY_PERM": "The bot does not have the necessary permissions to clone this channel.\nMissing permission: **{PERMISSION}**\nPlease make sure this permission is set on **this category**.\nIf you need help, please visit our [Discord support server](https://discord.gg/HW9tA4Mp3b).",
"COMMAND_CLEARALL_MISSING_GUILD_PERM": "The bot does not have the necessary permissions to clone this channel.\nMissing permission: **{PERMISSION}**\nPlease make sure this permission is set on **this server**.\nAlternatively, you can put this channel in a category and give the bot the necessary permissions there.\nIf you need help, please visit our [Discord support server](https://discord.gg/HW9tA4Mp3b).",
"COMMAND_CLEARALL_ERROR": "There was an error while clearing this channel.\nPlease check if the bot has all the neccessary permissions!\nIf you need help, please visit our [Discord support server](https://discord.gg/HW9tA4Mp3b).",
"COMMAND_CLEARALL_DELETE_ERROR": "I can not delete this channel.\nPlease note that I can only delete channels not used for community server purposes (rules and public updates channel).\nIf you need help, please visit our [Discord support server](https://discord.gg/HW9tA4Mp3b).",
"COMMAND_CLEARALL_SUCCESS": "{USER} deleted all messages in this channel.",
"COMMAND_PING": "Pong!\nShard {SHARD} | {PING} ms",
"COMMAND_VOTE_EMBED_TITLE": "ClearChat-Bot Vote Infos ✨",
"COMMAND_VOTE_EMBED": "Hey, you can vote via [this link](https://jh220.de/cc/vote).\nThis will give you access to new cool features.\n\nIf you want to invite the bot to your server, click on [this link](https://discord.com/oauth2/authorize?client_id=787789079227006976&permissions=484432&redirect_uri=https%3A%2F%2Fwww.jh220.de&scope=bot%20applications.commands).\nIf you need help with the bot, please visit our [Discord support server](https://discord.gg/HW9tA4Mp3b).",
"COMMAND_SETTINGS_EMBED_TITLE": "ClearChat-Bot Settings 🛠️",
"COMMAND_SETTINGS_EMBED": "This is the settings page of the ClearChat bot.\nYou can select a setting from the drop-down menu below this message and then enable or disable it.\nThe updated setting will immediately appear updated in the message, so you can make further adjustments.\nTo reset all settings to their default values, click \"Reset all settings\" there.",
"COMMAND_SETTINGS_BUTTON_RESET_ALL": "Reset all settings",
"INTERACTION_SETTINGS_RESET": "All settings have been reset to their default values.",
"INTERACTION_SETTINGS_TOGGLE_CONFIRMATION_MESSAGES": "Switched the setting for confirmation messages to {STATE}.",
"COMMAND_ADMIN_GET_USER_EMBED": "Information about the user.\n",
"COMMAND_ADMIN_GET_SERVER_EMBED_TITLE": "ClearChat-Bot Admin Server Information 📊",
"COMMAND_ADMIN_GET_SERVER_EMBED": "Information about the server.\n",
"COMMAND_ADMIN_STATS_STILL_STARTING": "The statistics are still being collected.\nPlease try again later.",
"COMMAND_ADMIN_STATS_EMBED_TITLE": "ClearChat-Bot Admin Stats 📊",
"INTERACTION_ADMIN_GET_USER_BAN_SUCCESS": "The user {BAN_USER} has been banned.",
"INTERACTION_ADMIN_GET_USER_PARDON_SUCCESS": "The User {BAN_USER} has been pardoned.",
"INTERACTION_ADMIN_GET_USER_BAN_CONFIRM_ERROR_ALREADY_BANNED": "The user {BAN_USER} is already banned.",
"INTERACTION_ADMIN_GET_USER_PARDON_CONFIRM_ERROR_ALREADY_PARDONED": "The user {BAN_USER} is already pardoned.",
"INTERACTION_ADMIN_GET_USER_BAN_HISTORY_EMBED_TITLE": "ClearChat-Bot Admin User Ban History 📜",
"INTERACTION_ADMIN_GET_OVERVIEW_EMBED_TITLE": "ClearChat-Bot Admin User Information 📊",
"ERROR_EXECUTE": "There was an error while executing this command!\nPlease report it to: https://github.com/JH220/discord-clearchatbot/issues/new?title=Interaction-ID%3A+{INTERACTION_ID}&template=command_error.md\nInteraction-ID: **{INTERACTION_ID}**\nFor more information, visit our [Discord support server](https://discord.gg/HW9tA4Mp3b).",
"SETTINGS_EMBED": "This is the settings page of the ClearChat bot.\nYou can select a setting from the drop-down menu below this message and then enable or disable it.\nThe updated setting will immediately appear updated in the message, so you can make further adjustments.\nTo reset all settings to their default values, click \"Reset all settings\" there."
"SETTINGS_EMBED": "This is the server-side settings page of the ClearChat bot.\nYou can select a setting from the drop-down menu below this message and then enable or disable it.\nTo reset all settings to their default values, click \"Reset all settings\" there."
}
3 changes: 2 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CustomClient extends Client implements Logger {
}

ierror(interaction : Interaction, error : Error = null, message : string = '') {
this.error(`[Interaction ${interaction.id}] ${message ? message : 'Error while executing interaction'}: ${error}${error?.stack ? `\n${error.stack}` : ''}`);
this.error(`[Interaction ${interaction.id}] ${message ?? 'Error while executing interaction'}: ${error}${error?.stack ? `\n${error.stack}` : ''}`);
}
idebug(interaction : Interaction, message : string) {
this.debug(`[Interaction ${interaction.id}] ${message}`);
Expand All @@ -44,6 +44,7 @@ process.on('message', (message : any) => {
if (message?.type == 'started' && !client.startup) {
client.debug('Received startup message, now in full operation.');
client.startup = new Date();
require('./utils/activity-manager').startActivity(client);
}
});

Expand Down
13 changes: 10 additions & 3 deletions src/commands/main/clearall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { ChannelType, ChatInputCommandInteraction, PermissionFlagsBits, PermissionsBitField } from 'discord.js';
import { CustomClient } from '../../bot';
import { Sequelize } from 'sequelize';

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -26,6 +27,9 @@ module.exports = {
else if (!member.permissions.has(PermissionsBitField.Flags.ManageChannels))
return await database.reply(interaction, 'COMMAND_CLEARALL_MISSING_GUILD_PERM', { 'PERMISSION': 'Manage Channels' });

if (!interaction.channel.deletable)
return await database.reply(interaction, 'COMMAND_CLEARALL_DELETE_ERROR');

var channel;

try {
Expand All @@ -38,11 +42,14 @@ module.exports = {
}

await database.reply(interaction, 'COMMAND_CLEARALL_PENDING', {}, false);
await database.reply(interaction, 'COMMAND_CLEARALL_SUCCESS', { 'CHANNEL': `<#${channel.id}>` }, false);


const settings : any = await (database.connection as Sequelize).models.ServerSetting.findOne({ where: { serverId: interaction.guildId } });
if (!(settings?.showreply ?? true)) return;

try {
const message = await database.getMessage('COMMAND_CLEARALL_SUCCESS', interaction, { 'USER': `<@${interaction.user.id}>` });
channel.send(message);
await database.reply(interaction, 'COMMAND_CLEARALL_SUCCESS', { 'CHANNEL': `<#${channel.id}>` }, false);
channel.send(await database.getMessage('COMMAND_CLEARALL_SUCCESS', interaction));
}
catch (error) {
await (interaction.client as CustomClient).ierror(interaction, error, 'Error while sending message to new channel');
Expand Down
43 changes: 40 additions & 3 deletions src/commands/main/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, ChatInputCommandInteraction, EmbedBuilder, StringSelectMenuBuilder, StringSelectMenuInteraction, StringSelectMenuOptionBuilder } from 'discord.js';
import { CustomClient } from '../../bot';
import { Sequelize } from 'sequelize';

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -11,13 +12,49 @@ module.exports = {
async execute(interaction : ChatInputCommandInteraction) {
const database = new (require('../../utils/database'))();

const select = new StringSelectMenuBuilder()
.setCustomId('settings_select')
.setPlaceholder('Select a setting to view')
.addOptions(
new StringSelectMenuOptionBuilder()
.setLabel('Toggle confirmation messages')
.setDescription('Toggles the confirmation message in the new channel when /clearall is run.')
.setValue('toggle_confirmation_messages'),
);
const resetButton = new ButtonBuilder()
.setCustomId('settings_reset')
.setLabel(await database.getMessage('COMMAND_SETTINGS_BUTTON_RESET_ALL', interaction))
.setStyle(ButtonStyle.Danger)
.setEmoji('🔄');

await interaction.reply({ embeds: [ new EmbedBuilder()
.setColor('#00FFFF')
.setTitle('ClearChat-Bot Settings 🛠️')
.setTitle(await database.getMessage('COMMAND_SETTINGS_EMBED_TITLE', interaction))
.setDescription(await database.getMessage('COMMAND_SETTINGS_EMBED', interaction)),
] });
], components: [new ActionRowBuilder().addComponents(select) as any, new ActionRowBuilder().addComponents(resetButton)] });

await database.reply(interaction, 'COMMAND_SETTINGS_SUCCESS', {}, false);
await (interaction.client as CustomClient).idebug(interaction, 'Replied to settings command');
},
async executeButton(interaction : ButtonInteraction, database : any) {
const customId = interaction.customId.split(';')[0];
if (customId != 'settings_reset') return;

const models = (database.connection as Sequelize).models;
await models.ServerSetting.destroy({ where: { serverId: interaction.guildId } });
database.reply(interaction, 'INTERACTION_SETTINGS_RESET');
},
async executeStringSelectMenu(interaction : StringSelectMenuInteraction, database : any) {
const customId = interaction.customId.split(';')[0];
if (customId != 'settings_select') return;
if (!interaction.values.includes('toggle_confirmation_messages')) return;

const models = (database.connection as Sequelize).models;
const setting : any = await models.ServerSetting.findOne({ where: { serverId: interaction.guildId } });
const showreply = setting ? !setting.showreply : false;
if (setting) setting.update({ showreply: showreply });
else models.ServerSetting.create({ serverId: interaction.guildId, showreply: showreply });

database.reply(interaction, 'INTERACTION_SETTINGS_TOGGLE_CONFIRMATION_MESSAGES', { 'STATE': showreply ? 'Enabled' : 'Disabled' });
},
};
12 changes: 1 addition & 11 deletions src/commands/utility/admin/get/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ module.exports = {
async execute(interaction : ChatInputCommandInteraction, database : any, server : any) {
const models = (database.connection as Sequelize).models;

/*
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('INVALID_KEY')
at new NodeError (node:internal/errors:406:5)
at CustomClient.emit (node:events:503:17)
at emitUnhandledRejectionOrErr (node:events:397:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_UNHANDLED_ERROR',
context: 'INVALID_KEY'
}
*/
const bin = (+server.serverId).toString(2);
const diff = 64 - (+server.serverId).toString(2).length;
const created = parseInt(bin.substring(0, 42 - diff), 2) + 1420070400000;
Expand All @@ -25,7 +15,7 @@ module.exports = {

await interaction.reply({ embeds: [ new EmbedBuilder()
.setColor('#00FFFF')
.setTitle('ClearChat-Bot Admin Server Information 📊')
.setTitle(await database.getMessage('COMMAND_ADMIN_GET_SERVER_EMBED_TITLE', interaction))
.setDescription(await database.getMessage('COMMAND_ADMIN_GET_SERVER_EMBED', interaction))
.setImage(`https://cdn.discordapp.com/icons/${server.serverId}/${server.serverPicture}.webp`)
.addFields(
Expand Down
Loading

0 comments on commit 72c6461

Please sign in to comment.