javascript JS Discord Bot 获取角色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48300358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS Discord Bot Get Role
提问by Hell Protection
I'm trying to get a role without using messages, like:
我试图在不使用消息的情况下获得一个角色,例如:
const Discord = require('discord.js');
const Bot = new Discord.Client();
const Role = Discord.roles.find('name,'Exemple')
Role.delete()
Is this possible to do that?
这有可能做到吗?
回答by Wright
Yes you can but you will need to have the guild id you want to get the role from. Also, you should put that part in the readyevent.
是的,您可以,但您需要拥有要从中获取角色的公会 ID。此外,您应该将该部分放在ready事件中。
const discord = require("discord.js");
const client = new discord.Client();
client.on("ready", () => {
console.log("Bot online!");
const guild = client.guilds.get("The_server_id");
const role = guild.roles.find("name", "Your_role_name");
console.log(`Found the role ${role.name}`);
})
回答by StanB
Using that way of Collection#find("key", "value")in Discord.JS is deprecated by the way,
You should use Collection#find(Obj => Obj.key == "value")instead.
顺便说一下,Collection#find("key", "value")不推荐使用 Discord.JS 中的这种方式,您应该Collection#find(Obj => Obj.key == "value")改用。
回答by Casey Shore
Guild doesn't work unless you have it set up on the server. People keep suggesting this but you have to already have infrastructure set up in your discord server.
除非您在服务器上设置了公会,否则公会不起作用。人们一直在建议这一点,但您必须已经在 Discord 服务器中设置了基础设施。

