簡體   English   中英

如何讓我的 discord 機器人為用戶分配角色(不和諧 js)

[英]How can I make my discord bot assign roles to users (discord js)

這是我的代碼:

如何使用我的 discord js 機器人為用戶分配角色?

如何讓我的 discord 機器人為用戶分配角色(不和諧 js)

我想要這樣當我輸入 !addrole 時添加一個角色

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on('ready', () => {
  console.log('Bot Online');
});

var prefix = "!"

client.on(`message`, message => {
  if(message.content.startsWith(prefix + "addrole"))
  {
    //ADD ROLE HERE
  }

});

client.on('ready', () => {
  client.user.setActivity(`!help`, { type: "PLAYING" });
});

client.login('TOKEN');

首先,您需要找到要添加的角色:

const guild = message.guild;
let role = message.mentions.roles.first() || guild.roles.cache.find(role => role.id === message.content.substring(1))

然后使用以下命令添加角色:

const member = message.member
member.roles.add(role);

我不知道您正在使用的版本,但看到您正在使用縮進,您應該嘗試一下。

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on('ready', () => {
  console.log('Bot Online');
});

var prefix = "!"

client.on(`message`, message => {
  if(message.content.startsWith(prefix + "addrole"))
  {
    const roleId = 'add your role id here'
    message.member.addRole(message.guild.roles.cache.find(roleId))
  }

});

client.on('ready', () => {
  client.user.setActivity(`!help`, { type: "PLAYING" });
});

client.login('TOKEN');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM