chore: improve safety of commands

This commit is contained in:
Ahmad 2025-04-17 01:05:10 -04:00
parent 83bbf7b098
commit 7c2a99daf5
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
28 changed files with 329 additions and 235 deletions

View file

@ -14,12 +14,15 @@ const command: Command = {
.setDescription('(Admin Only) Display the current configuration')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
execute: async (interaction) => {
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply({ flags: ['Ephemeral'] });
if (
!interaction.memberPermissions?.has(PermissionFlagsBits.Administrator)
) {
await interaction.reply({
await interaction.editReply({
content: 'You do not have permission to use this command.',
flags: ['Ephemeral'],
});
return;
}
@ -180,10 +183,9 @@ const command: Command = {
? [createPaginationButtons(pages.length, currentPage)]
: [];
const reply = await interaction.reply({
const reply = await interaction.editReply({
embeds: [pages[currentPage]],
components,
flags: ['Ephemeral'],
});
if (pages.length <= 1) return;

View file

@ -26,9 +26,11 @@ const command: OptionsCommand = {
),
execute: async (interaction) => {
try {
await interaction.deferReply();
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply();
try {
const client = interaction.client as ExtendedClient;
const commandName = interaction.options.getString('command');
@ -178,7 +180,7 @@ async function handleSpecificCommand(
const cmd = client.commands.get(commandName);
if (!cmd) {
return interaction.reply({
return interaction.editReply({
content: `Command \`${commandName}\` not found.`,
ephemeral: true,
});
@ -227,7 +229,7 @@ async function handleSpecificCommand(
inline: false,
});
return interaction.reply({ embeds: [embed] });
return interaction.editReply({ embeds: [embed] });
}
/**

View file

@ -16,6 +16,10 @@ const command: Command = {
.setName('members')
.setDescription('Lists all non-bot members of the server'),
execute: async (interaction) => {
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply();
let members = await getAllMembers();
members = members.sort((a, b) =>
(a.discordUsername ?? '').localeCompare(b.discordUsername ?? ''),
@ -63,7 +67,7 @@ const command: Command = {
const components =
pages.length > 1 ? [getButtonActionRow(), getSelectMenuRow()] : [];
await interaction.reply({
await interaction.editReply({
embeds: [pages[currentPage]],
components,
});

View file

@ -8,7 +8,7 @@ const command: Command = {
.setDescription('Check the latency from you to the bot'),
execute: async (interaction) => {
await interaction.reply(
`Pong! Latency: ${Date.now() - interaction.createdTimestamp}ms`,
`🏓 Pong! Latency: ${Date.now() - interaction.createdTimestamp}ms`,
);
},
};

View file

@ -8,21 +8,22 @@ const command: Command = {
.setName('recalculatelevels')
.setDescription('(Admin Only) Recalculate all user levels'),
execute: async (interaction) => {
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply({ flags: ['Ephemeral'] });
await interaction.editReply('Recalculating levels...');
if (
!interaction.memberPermissions?.has(
PermissionsBitField.Flags.Administrator,
)
) {
await interaction.reply({
await interaction.editReply({
content: 'You do not have permission to use this command.',
flags: ['Ephemeral'],
});
return;
}
await interaction.deferReply();
await interaction.editReply('Recalculating levels...');
try {
await recalculateUserLevels();
await interaction.editReply('Levels recalculated successfully!');

View file

@ -36,7 +36,9 @@ const command: SubcommandCommand = {
),
execute: async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply({ flags: ['Ephemeral'] });
const config = loadConfig();
const managerRoleId = config.roles.staffRoles.find(
@ -52,18 +54,15 @@ const command: SubcommandCommand = {
PermissionsBitField.Flags.Administrator,
)
) {
await interaction.reply({
await interaction.editReply({
content:
'You do not have permission to use this command. This command is restricted to users with the Manager role.',
flags: ['Ephemeral'],
});
return;
}
const subcommand = interaction.options.getSubcommand();
await interaction.deferReply({ flags: ['Ephemeral'] });
try {
if (subcommand === 'database') {
await handleDatabaseReconnect(interaction);

View file

@ -18,6 +18,10 @@ const command: Command = {
.setName('restart')
.setDescription('(Manager Only) Restart the bot'),
execute: async (interaction) => {
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply({ flags: ['Ephemeral'] });
const config = loadConfig();
const managerRoleId = config.roles.staffRoles.find(
(role) => role.name === 'Manager',
@ -32,17 +36,15 @@ const command: Command = {
PermissionsBitField.Flags.Administrator,
)
) {
await interaction.reply({
await interaction.editReply({
content:
'You do not have permission to restart the bot. This command is restricted to users with the Manager role.',
flags: ['Ephemeral'],
});
return;
}
await interaction.reply({
await interaction.editReply({
content: 'Restarting the bot... This may take a few moments.',
flags: ['Ephemeral'],
});
const dbConnected = await ensureDatabaseConnection();

View file

@ -7,8 +7,10 @@ const command: Command = {
.setName('server')
.setDescription('Provides information about the server.'),
execute: async (interaction) => {
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.reply(
`The server **${interaction!.guild!.name}** has **${interaction!.guild!.memberCount}** members and was created on **${interaction!.guild!.createdAt}**. It is **${new Date().getFullYear() - interaction!.guild!.createdAt.getFullYear()!}** years old.`,
`The server **${interaction.guild.name}** has **${interaction.guild.memberCount}** members and was created on **${interaction.guild.createdAt}**. It is **${new Date().getFullYear() - interaction.guild.createdAt.getFullYear()}** years old.`,
);
},
};

View file

@ -19,13 +19,17 @@ const command: OptionsCommand = {
.setRequired(true),
),
execute: async (interaction) => {
if (!interaction.isChatInputCommand() || !interaction.guild) return;
await interaction.deferReply();
const userOption = interaction.options.get(
'user',
) as unknown as GuildMember;
const user = userOption.user;
if (!userOption || !user) {
await interaction.reply('User not found');
await interaction.editReply('User not found');
return;
}
if (
@ -33,7 +37,7 @@ const command: OptionsCommand = {
PermissionsBitField.Flags.ModerateMembers,
)
) {
await interaction.reply(
await interaction.editReply(
'You do not have permission to view member information.',
);
return;
@ -140,7 +144,7 @@ const command: OptionsCommand = {
iconURL: interaction.user.displayAvatarURL(),
});
await interaction.reply({ embeds: [embed] });
await interaction.editReply({ embeds: [embed] });
},
};

View file

@ -71,12 +71,16 @@ const command: SubcommandCommand = {
),
),
execute: async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (!interaction.isChatInputCommand() || !interaction.guild) return;
const commandUser = interaction.guild?.members.cache.get(
const commandUser = interaction.guild.members.cache.get(
interaction.user.id,
);
await interaction.deferReply({
flags: ['Ephemeral'],
});
const config = loadConfig();
const managerRoleId = config.roles.staffRoles.find(
(role) => role.name === 'Manager',
@ -87,18 +91,12 @@ const command: SubcommandCommand = {
!managerRoleId ||
commandUser.roles.highest.comparePositionTo(managerRoleId) < 0
) {
await interaction.reply({
await interaction.editReply({
content: 'You do not have permission to use this command',
flags: ['Ephemeral'],
});
return;
}
await interaction.deferReply({
flags: ['Ephemeral'],
});
await interaction.editReply('Processing...');
const subcommand = interaction.options.getSubcommand();
const user = interaction.options.getUser('user', true);
const amount = interaction.options.getInteger('amount', false);