简体   繁体   English

如何让我的 Discord 机器人给用户一个角色?

[英]How can I make my Discord bot give a user a role?

How can I make my Discord bot give a user a role?如何让我的 Discord 机器人给用户一个角色? It has the "Administrator" permission, and I'm using the nodejs library.它具有“管理员”权限,我正在使用 nodejs 库。 Here's my code so far (this initializes the bot):到目前为止,这是我的代码(这会初始化机器人):

var auth = require("./auth.json");
var fs = require("fs");
var bot = new discord.Client();
bot.login("TOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKEN")

bot.on("ready", function(event) {
        console.log("Initialized");
});

You must use the GuildMember.addRole() function.您必须使用GuildMember.addRole() function。 (to get a guild member, use Guild#members.get() ). (要获得公会成员,请使用Guild#members.get() )。 Read the Discord.js.org documentation for more informations.阅读 Discord.js.org 文档以获取更多信息。

You can use:您可以使用:

bot.on("message", (message) => {
    if(!message.member) return
    // Find the role by name
    var role = message.guild.roles.find((r) => r.name === "Name of your role");
   // Assign the role to the message author
   message.member.roles.add(role);
});

You can try this你可以试试这个

client.on('guildMemberAdd', member => {

    //Search and add the role to the new user
    member.addRole(member.guild.roles.find(nm => nm.name === "Your role name"));

    // Send the message to a designated channel on a server:
    const channel = member.guild.channels.find(ch => ch.name === 'general');

    // Do nothing if the channel wasn't found on this server
    if (!channel) return;

    // Send the message, mentioning the member
    channel.send(`Welcome to the server, ${member}`);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使我的机器人扮演角色并将其给予我? - How Do I Make My Bot Make a Role and Give It To Me? 如何做到让我的机器人在用户重复一组数字时为其赋予角色? - How can I make it so that my bot gives a role to a user when they repeat a set of numbers? 如何让我的 discord 机器人仅在用户开始输入特定频道时发送消息? - How can I make my discord bot only send a message if a user starts typing in a specific channel? 如何让我的 Discord 机器人(Javascript)回答用户提出的特定问题的随机数? - How can I make my Discord bot (Javascript) answer a random number to a specific question an user asks? 如何让我的 discord 机器人只对某个用户 ID 做出反应? - How can I make my discord bot only react on a certain user id? 当被问到某个问题时,如何让我的 Discord Bot (Javascript) 在标签中回复用户? - How can i make my Discord Bot (Javascript) reply to the user in tag, when a certain question is asked? Discord 机器人选择角色不会给用户角色 - Discord Bot selection role won't give the role to the user 当提到它时,如何让我的 Discord 机器人识别它? - How can I make my Discord bot recognize it when it is mentioned? 如何让 discord 机器人提及角色和用户? - How do I get a discord bot to mention a role and a user? 我的 Discord 机器人如何为新用户分配角色? - How can my Discord Bot assign a Role to new Users?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM