简体   繁体   中英

Discord Bot JS - How do I append a role to a specific discord tag?

Im currently programming a bot for my discord and I want a command that my mods can write that appends a mute role to a specific player. Currently they type /mute with the discord tag straight after (for example /muteDanyboy#8473) and the discord tag is saved in a variable called commandsplit (as "Danyboy#8473"). How do I target that discord tag and append the mute role to that user?

I use discord.js to program. Line 23 is the part im looking to get working, thanks.

const Discord = require("discord.js");
const TOKEN = "MyToken"
const PREFIX = "/timeout24"
var bot = new Discord.Client();
const COMMAND = "/mute"

/*Confirms the bot is active */
bot.on("ready", function(){
    console.log("Bot Active");
});

/*Gives new members the "Member" role */
bot.on("guildMemberAdd", function(member) {
    member.addRole(member.guild.roles.find("name", "Member"))
});

/*The mute command */
bot.on("message", function(message) {
    if(message.author.equals(bot.user)) return;
    if(!message.content.startsWith(COMMAND)) return;
    var commandsplit = message.content.substring(COMMAND.length).split(" ");

    commandsplit.id.addRole(message.guild.roles.find("name", "Muted"))

})
bot.login(TOKEN);

Read the discord.js documentation for guildMember carefully to use .addRole to set the mute role!

Your method is not working properly cause you have a string of username but .addRole uses an Map(kind of object) for the member. So you need to store the member is a variable with the given username. Though you can do that as well but a better method will be to use @ mention and they are parsed as object only.

Firstly make an if statement and check if it's the mute command if(message.startsWith(${prefix}mute); (You need to call the prefix from botconfig.json or define it in the mail file instead first) then inside the if statement use the below code.

let muteuser = message.mentions.members.first();
let muterole = message.guild.roles.find('name',"Muted");
muteuser.addRole(muterole);

Please make sure you understand the code and not just copy it.

Let me know if you need help with anything

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