【问题标题】:Discord.js — GuildMember.roles.set([]); not workingDiscord.js — GuildMember.roles.set([]);不工作
【发布时间】:2020-08-29 20:47:29
【问题描述】:

我有一个 Discord 机器人,我希望它的一个命令从成员中删除所有角色。不过,我遇到了一个问题。这是我正在使用的代码:

// Get the ID of either the user mentioned (if the person executing the command has the leader role) or the person executing the command:
var user = (args[0] && args[0].substring(0, 3) === "<@!" && args[0][args[0].length - 1] === ">" && message.member.roles.cache.find(role => role.name === "Leader")) ? args[0].substring(3, args[0].length - 1) : message.author.id;
// Remove all of their roles:
message.guild.members.fetch(user).roles.set([]);

控制台吐出这个错误:

(node:23) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'set' of undefined
    at Client.<anonymous> (/app/bot.js:201:48)
    at Client.emit (events.js:311:20)
    at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
    at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:125:16)
    at WebSocket.emit (events.js:311:20)
    at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:800:20)

它说它无法读取 undefined 的属性“set”(这意味着它要么没有找到用户,要么 '.roles' 属性不存在;我知道它应该,因为我从here)。我在代码的其他地方使用了“var user =”行,没有任何问题。

任何帮助将不胜感激。

【问题讨论】:

  • roles 未定义,否则会说Cannot read property 'roles' of undefined 为错误
  • 只是盲读逻辑,我的假设是 fetch 正在执行某种异步逻辑,而 roles 不是返回的承诺的属性
  • 我可能已经修复它...现在测试一些东西
  • @Taplar .fetch() 是异步的,我之前没有意识到这一点。
  • 是的,我以为它是那个方法的名称,:)

标签: javascript node.js discord discord.js


【解决方案1】:

问题是 message.guild.members.fetch(user) 是一个返回 Promise 的异步调用。

来自文档 (https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=fetch):

// Fetch a single member
guild.members.fetch('66564597481480192')
  .then(console.log)
  .catch(console.error);

您需要根据您的应用对其进行定制,但您需要等待 Promise 解决,然后才能设置用户的角色。

异步/等待

// Get the ID of either the user mentioned (if the person executing the command has the leader role) or the person executing the command:
var user = (args[0] && args[0].substring(0, 3) === "<@!" && args[0][args[0].length - 1] === ">" && message.member.roles.cache.find(role => role.name === "Leader")) ? args[0].substring(3, args[0].length - 1) : message.author.id;
// Remove all of their roles:
const discordUser = await message.guild.members.fetch(user);
discordUser.roles.set([]);

然后/抓住

// Get the ID of either the user mentioned (if the person executing the command has the leader role) or the person executing the command:
var user = (args[0] && args[0].substring(0, 3) === "<@!" && args[0][args[0].length - 1] === ">" && message.member.roles.cache.find(role => role.name === "Leader")) ? args[0].substring(3, args[0].length - 1) : message.author.id;
// Remove all of their roles:
message.guild.members.fetch(user).then((discordUser) => {
  discordUser.roles.set([]);
}).catch((err) => console.log(err)})

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2017-09-12
    • 2018-02-05
    • 2021-11-02
    • 1970-01-01
    • 2021-05-01
    • 2021-10-13
    • 2022-12-14
    • 1970-01-01
    相关资源
    最近更新 更多