admin管理员组

文章数量:1415484

I'm kinda new to coding discord bots and I have a problem. I want to have a ban mand that would ban the mentioned user in the mand or the user that has an ID that was provided in the mand. For example: &ban @User#0001 would ban User#0001 but if the mand looks like this: &ban 123456789123456789 (let's say that's the ID of User#0001) it would still ban User#0001 (as it is the user's ID).

I have this code, it works if I mention the user, but it doesn't work if I enter the ID.

const Discord = require('discord.js');

module.exports = {
    name: 'testban',
    description: "Executer will ban the mentioned user",
    execute(message, args){
        if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
        let User = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
        if (!User) return message.channel.send("Invalid User")
        if (User.hasPermission("BAN_MEMBERS")) return message.reply("Can't ban that one, he also can ban")
        let banReason = args.join(" ").slice(22);
        if (!banReason) {
            banReason = "None"
        }
        console.log(`USER = ${User}`)
        User.ban({reason: banReason})
        var UserID = User.id
        console.log(`USER ID = ${UserID}`)
    }
}

The error I get when entering the ID is this: message.guild.members.get is not a function

How could I make it ban the person even if I only provide the ID?

I'm kinda new to coding discord bots and I have a problem. I want to have a ban mand that would ban the mentioned user in the mand or the user that has an ID that was provided in the mand. For example: &ban @User#0001 would ban User#0001 but if the mand looks like this: &ban 123456789123456789 (let's say that's the ID of User#0001) it would still ban User#0001 (as it is the user's ID).

I have this code, it works if I mention the user, but it doesn't work if I enter the ID.

const Discord = require('discord.js');

module.exports = {
    name: 'testban',
    description: "Executer will ban the mentioned user",
    execute(message, args){
        if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
        let User = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
        if (!User) return message.channel.send("Invalid User")
        if (User.hasPermission("BAN_MEMBERS")) return message.reply("Can't ban that one, he also can ban")
        let banReason = args.join(" ").slice(22);
        if (!banReason) {
            banReason = "None"
        }
        console.log(`USER = ${User}`)
        User.ban({reason: banReason})
        var UserID = User.id
        console.log(`USER ID = ${UserID}`)
    }
}

The error I get when entering the ID is this: message.guild.members.get is not a function

How could I make it ban the person even if I only provide the ID?

Share Improve this question edited Jul 21, 2021 at 9:40 Pandicon asked Aug 26, 2020 at 13:40 PandiconPandicon 4303 gold badges17 silver badges38 bronze badges 1
  • Vinicius's answer is correct, but also make sure you change message.mentions.users.first() to message.mentions.members.first() so that it returns a GuildMember instead of a User – Lioness100 Commented Aug 26, 2020 at 15:42
Add a ment  | 

1 Answer 1

Reset to default 3

If you're using the newer versions of discord.js you need to use the cache, you just need to change it to this:

message.guild.members.cache.get(args[0])

本文标签: javascriptBanning users using user ID discordjsStack Overflow