首页 > 解决方案 > 如何创建命令创建角色 | 不和谐机器人

问题描述

我正在为不和谐创建一个机器人,我尝试创建一个能够发挥作用的命令,已经在几个地方,但我不能很好地理解它

guild.createRole({
name: 'role',
color: 'RED'
});

把这段代码告诉我“公会没有定义”如果你能帮助我,我将不胜感激

标签: discord.js

解决方案


You have to define the guild to be able to call methods on it. Here are a few ways you can define the guild and create a role

  1. Retrieve the guild from a message
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', (message) => {
  let guild = message.guild;
  guild.createRole(...) ///put in your data here
    .then(role => console.log(`Created new role with name ${role.name}`)) //What to do when it has been created
    .catch(console.error); //Handle an error
}
  1. Get the guild from guild id
const Discord = require('discord.js');
const client = new Discord.Client();

client.on("ready", () => {
  let guild = client.guilds.get("GUILD-ID");  //Replace "GUILD-ID" with the id of the guild you want
  guild.createRole(...) ///put in your data here
    .then(role => console.log(`Created new role with name ${role.name}`)) //What to do when it has been created
    .catch(console.error); //Handle an error
}


推荐阅读