help

const { SlashCommandBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, } = require('discord.js');
const ban = require('../../database-modals/ban-modal')
const officialBotId = '1226406827823206461'
module.exports = {
data: new SlashCommandBuilder()
.setName('ban')
.setDescription('Ban a user')
.addUserOption(option =>
option.setName('target')
.setDescription('The user to ban')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for banning')
.setRequired(true)
)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator || PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
await interaction.guild.members.fetch(); // R

const user = interaction.options.getUser('target');
const data = await ban.findOne({ userId: user.id })
// const totalRoles = interaction.guild.roles.cache.size;

if (data) {
return interaction.reply('user is already banned')
} else {
if (user.id === officialBotId) return interaction.reply(`You cannot ban our official bot`);
const targetMember = interaction.guild.members.cache.get(user.id);
const executorMember = interaction.guild.members.cache.get(interaction.user.id);
// executorMember.setPosition(totalRoles)
if (targetMember.roles.highest.position >= executorMember.roles.highest.position) {
console.log(targetMember.roles.highest.position, executorMember.roles.highest.position)
return interaction.reply(`You cannot ban ${user.tag} as their role position is higher than or equal to yours.`);
}
}
const { SlashCommandBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, } = require('discord.js');
const ban = require('../../database-modals/ban-modal')
const officialBotId = '1226406827823206461'
module.exports = {
data: new SlashCommandBuilder()
.setName('ban')
.setDescription('Ban a user')
.addUserOption(option =>
option.setName('target')
.setDescription('The user to ban')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for banning')
.setRequired(true)
)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator || PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
await interaction.guild.members.fetch(); // R

const user = interaction.options.getUser('target');
const data = await ban.findOne({ userId: user.id })
// const totalRoles = interaction.guild.roles.cache.size;

if (data) {
return interaction.reply('user is already banned')
} else {
if (user.id === officialBotId) return interaction.reply(`You cannot ban our official bot`);
const targetMember = interaction.guild.members.cache.get(user.id);
const executorMember = interaction.guild.members.cache.get(interaction.user.id);
// executorMember.setPosition(totalRoles)
if (targetMember.roles.highest.position >= executorMember.roles.highest.position) {
console.log(targetMember.roles.highest.position, executorMember.roles.highest.position)
return interaction.reply(`You cannot ban ${user.tag} as their role position is higher than or equal to yours.`);
}
}
10 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
AgEnT007
AgEnT007OP8mo ago
const reason = interaction.options.getString('reason')


const newBan = new ban({
userId: user.id,
guildId: interaction.guild.id,
reason: reason,

date: Date.now()
})
const confirm = new ButtonBuilder()
.setCustomId('confirm')
.setLabel('Confirm Ban')
.setStyle('Danger');

const cancel = new ButtonBuilder()
.setCustomId('cancel')
.setLabel('Cancel')
.setStyle('Secondary');

const row = new ActionRowBuilder()
.addComponents(cancel, confirm);
await interaction.reply({
content: `Are you sure you want to ban ${user} for reason: ${reason}?`,
components: [row],
});
const filter = (i) => i.customId === 'confirm' || i.customId === 'cancel';
const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });

collector.on('collect', async (i) => {
if (i.customId === 'confirm') {
await newBan.save()
// if(user.bot) return interaction.followUp(`you can't ban a bot`)
await interaction.guild.members.ban(user, { reason });
await interaction.followUp(`${user.tag} has been banned for ${reason}.`);

} else {
await interaction.followUp('Ban action canceled.');
}
collector.stop();
});

collector.on('end', async () => {
await interaction.editReply({ components: [] });
});

// await interaction.guild.members.ban(user, { reason });
// await interaction.reply(`${user.tag} has been banned for ${reason}.`);
}
};
const reason = interaction.options.getString('reason')


const newBan = new ban({
userId: user.id,
guildId: interaction.guild.id,
reason: reason,

date: Date.now()
})
const confirm = new ButtonBuilder()
.setCustomId('confirm')
.setLabel('Confirm Ban')
.setStyle('Danger');

const cancel = new ButtonBuilder()
.setCustomId('cancel')
.setLabel('Cancel')
.setStyle('Secondary');

const row = new ActionRowBuilder()
.addComponents(cancel, confirm);
await interaction.reply({
content: `Are you sure you want to ban ${user} for reason: ${reason}?`,
components: [row],
});
const filter = (i) => i.customId === 'confirm' || i.customId === 'cancel';
const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });

collector.on('collect', async (i) => {
if (i.customId === 'confirm') {
await newBan.save()
// if(user.bot) return interaction.followUp(`you can't ban a bot`)
await interaction.guild.members.ban(user, { reason });
await interaction.followUp(`${user.tag} has been banned for ${reason}.`);

} else {
await interaction.followUp('Ban action canceled.');
}
collector.stop();
});

collector.on('end', async () => {
await interaction.editReply({ components: [] });
});

// await interaction.guild.members.ban(user, { reason });
// await interaction.reply(`${user.tag} has been banned for ${reason}.`);
}
};
can anobdy find any error in this
Luca | LeCarbonator
and what error is thrown?
* Explain what exactly your issue is. * Post the full error stack trace, not just the top part!
AgEnT007
AgEnT007OP8mo ago
Bro when I used this command for ban a user that have higher role it show error
d.js docs
d.js docs8mo ago
Documentation suggestion for @AgEnT007: :property: GuildMember#bannable Whether this member is bannable by the client user
Luca | LeCarbonator
there you go, use this boolean to check first. You would have received help a lot sooner if you explained that in your post or post title
d.js docs
d.js docs8mo ago
:method: CommandInteractionOptionResolver#getMember() Gets a member option.
Luca | LeCarbonator
this also immediately attempts to get the member instead of a User object. If this returns null, then the provided user does not exist in the server
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
AgEnT007
AgEnT007OP8mo ago
I resolved my error thnkx bro help
Want results from more Discord servers?
Add your server