我试图在服务器上进行角色计数,但我不知道如何获取角色计数

时间:2019-11-29 14:43:40

标签: discord.js

对此是新手,我尝试进行角色计数: 我试过了:

const RolesChannel = client.channels.get("649537269988065280")
  RolesChannel.setName(RolesChannel.guild.Roles + ' Roles!')
  console.log('updated ' + RolesChannel.guild.name+ '\'s Roles channel!')

它将为频道命名:未定义的角色!

然后我尝试:

const RolesChannel = client.channels.get("649537269988065280")
  RolesChannel.setName(RolesChannel.guild.Roles.size + ' Roles!')
  console.log('updated ' + RolesChannel.guild.name+ '\'s Roles channel!')

它只会崩溃并显示以下错误消息:

TypeError: Cannot read property 'size' of undefined
at SEES (C:\Users\enfri\Documents\discord bot test\!testo.js:845:53)
at processCommand (C:\Users\enfri\Documents\discord bot test\!testo.js:342:10)
at Client.<anonymous> (C:\Users\enfri\Documents\discord bot test\!testo.js:148:10)
at Client.emit (events.js:210:5)
at MessageCreateHandler.handle (C:\Users\enfri\Documents\discord bot test\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\enfri\Documents\discord bot test\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
at WebSocketConnection.onPacket (C:\Users\enfri\Documents\discord bot test\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\enfri\Documents\discord bot test\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\enfri\Documents\discord bot test\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:210:5)

sceenshot of the channel

1 个答案:

答案 0 :(得分:0)

我不明白为什么当角色是公会的属性时,为什么要尝试从通道中获取角色的数量。 如果需要的话,只需获取公会ID并获取该公会中角色的大小即可。

let guild = clients.guilds.get("Guild id");
Console.log(guild.roles.size);