Discordjs API error whilst trying to banning a user

Viewed 60

I am attempting to program a ban command to be used by an administrator at a user who has broken the rules or has been naughty enough to be banned. When I ran the following code:

const Discord = require("discord.js");
const config = require("./config.json");
const { MessageAttachment, MessageEmbed } = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS","GUILD_MEMBERS", "GUILD_MESSAGES", "DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS", "DIRECT_MESSAGE_TYPING"], partials: ['CHANNEL',] })
const RichEmbed = require("discord.js");
const prefix = "!";



client.on("messageCreate", function(message) {
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;

  const commandBody = message.content.slice(prefix.length);
  const args = commandBody.split(' ');
  const command = args.shift().toLowerCase();
  
  if (command === "news") {
    if (message.channel.type == "DM") {
      message.author.send(" ");
    }

  }
  if (command === "help") {
    message.author.send("The help desk is avaliable at this website: https://example.com/");
  }
});

client.on("messageCreate", function(message) {
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;

  const commandBody = message.content.slice(prefix.length);
  const args = commandBody.split(' ');
  const command = args.shift().toLowerCase();

  if (command === "ping") {
    const timeTaken = Date.now() - message.createdTimestamp;
    message.channel.send(`Pong! ${timeTaken}ms.`);
  }
  if (command === "delete all messages") {
    const timeTaken = Date.now() - message.createdTimestamp;
    const code = Math.random(1,1000)
    message.channel.send(`Executing command. Verification Required.`);
    message.author.send(`Your code is the number ${code}.`)
    message.channel.send(`Please confirm your code. The code has been sent to your dms. Confirm it in here.`)
  }
  if (command === "ban") {
    const user = message.mentions.users.first().id
    message.guild.members.ban(user).then(user => {  
    message.channel.send(`Banned ${user.id}`);

}).catch(console.error);
}
});
client.login(config.BOT_TOKEN)

I ran into the following error whilst trying to test the command on a dummy acc.

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (/home/runner/gwehuirw/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/runner/gwehuirw/node_modules/discord.js/src/rest/RequestHandler.js:51:14) {
  method: 'put',
  path: '/guilds/891083670314680370/bans/943680142004322326',
  code: 50013,
  httpStatus: 403,
  requestData: { json: { delete_message_days: 0 }, files: [] }

Could someone please help me with this? Thanks.

PS: I already had the necessary permissions for the bot to ban users. I also added the necessary intents for it to work. But I still am stuck.

2 Answers

Try removing the .id when you are trying to get the user. Getting the id would be making you ban the string that is the id, not the user.

I realized that the user who was trying to ban a user needed to have certain permissions. I have finished working on the command. Thanks to everyone who had helped me through this mess!

Related