简体   繁体   中英

How to log into the ban section of discord using discord.js v12?

I'm working on my ban command and it's fully functional, but I was wondering, how I would log the ban reason into the ban section of discord (example in the attachment)? My code looks like this:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');

module.exports = {
    name: 'ban',
    description: "Mentioned user will be banned",
    execute(message, args){
        if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
        let User = message.guild.member(message.mentions.members.first()) || message.guild.members.cache.get(args[0])
        if (!User) return message.channel.send("Invalid User")
        if (User.hasPermission("BAN_MEMBERS")) return message.reply("Invalid Permissions a")
        let banReason = args.join(" ").slice(22);
        if (!banReason) {
            banReason = "None"
        }
        console.log(`USER = ${User}`)
        User.ban
        console.log(`Ban reason = ${banReason}`)
        var UserID = User.id
        console.log(`USER ID = ${UserID}`)
    }
}

Any help? Thanks :)

在此处输入图片说明

It's actually quite easy, just add ({reason: banReason}) after the User.ban part, making it:

User.ban({reason: banReason})

That should log it there properly :)

You can use the reason property of the options parameter in the GuildMember.ban() method:

User.ban({ reason: 'real life danger' })
  .then(member => console.log(`${member.name} was banned.`))
  .catch(console.error)

Also, in your code, there's no reason for using the Guild.member() method when defining User . Guild.member(user) is used to turn a User object into a GuildMember object.

However,MessageMentions.members.first() already returns a GuildMember object, making it obsolete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM