Skip to content

Commit

Permalink
Improve Kick/Ban Commands permissions check
Browse files Browse the repository at this point in the history
  • Loading branch information
JonatanMGit committed Feb 13, 2023
1 parent 13f09a3 commit 96af6d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
31 changes: 19 additions & 12 deletions bot/src/commands/ban.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { GuildMember, User } from 'discord.js';

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -12,20 +13,26 @@ module.exports = {
.setDescription('The reason for the ban')
.setRequired(false))
.setDescription('Bans a user'),
async execute(interaction) {
const user = interaction.options.getUser('user');
const reason = interaction.options.getString('reason');
if (user) {
// chck perms
if (interaction.member.permissions.has('BAN_MEMBERS')) {
// ban user
await interaction.guild.members.ban(user, { reason: reason });
await interaction.reply(`Banned ${user}`);
} else {
await interaction.reply({ content: 'You do not have permission to ban members', ephemeral: true });
async execute(interaction) {
const user = interaction.options.getUser('user') as GuildMember;
const reason = interaction.options.getString('reason');
if (user) {
// chck permissions and if user is bannable
if (interaction.member.permissions.has('BAN_MEMBERS') && user.bannable) {
// send dm to user
let message = `You have been banned from ${interaction.guild.name}`;
if (reason) {
message += ` for ${reason}`;
}
await user.send(message);
// ban user
await interaction.guild.members.ban(user, { reason: reason });
await interaction.reply(`Banned ${user}`);
} else {
await interaction.reply({ content: 'You did not provide a user to ban', ephemeral: true });
await interaction.reply({ content: 'You do not have permission to ban members', ephemeral: true });
}
} else {
await interaction.reply({ content: 'You did not provide a user to ban', ephemeral: true });
}
},
};
19 changes: 17 additions & 2 deletions bot/src/commands/kick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GuildMember } from "discord.js";

const { SlashCommandBuilder } = require("@discordjs/builders");
const { registerCommands, loadCommands } = require("../commandUtils");

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -19,9 +20,23 @@ module.exports = {
return;
}
// get the user to kick
const user = interaction.options.getUser("user");
const user = interaction.options.getUser("user") as GuildMember;

// check if the user is kickable
if (!user.kickable) {
await interaction.reply({ content: "I can't kick this user", ephemeral: true });
return;
}

const reason = interaction.options.getString("reason");

// send dm to user
let message = `You have been kicked from ${interaction.guild.name}`;
if (reason) {
message += ` for ${reason}`;
}
await user.send(message);

// kick the user
await interaction.guild.members.kick(user, { reason: reason });
await interaction.reply(`Kicked ${user}`);
Expand Down

0 comments on commit 96af6d2

Please sign in to comment.