[英]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.