【问题标题】:Add roles to user from DMs in new discordjs在新的 discordjs 中从 DM 向用户添加角色
【发布时间】:2022-01-18 21:18:38
【问题描述】:

我想知道如何在 DM 中为特定用户添加角色。 随着新的 Discord.js 更新,它很棘手,并且找不到解决方法。 谢谢。

我的尝试:

var guild = client.guilds.cache.get("[GUILDID]")
var buyerRole = guild.roles.cache.get("[ROLE ID]")
console.log(message.author.id) // Works
var guildMember = guild.members.fetch(message.author.id)
console.log(guildMember.displayName) // Returns 'undefined'
guildMember.setNickname(guildMember.username+" | Buyer") // Error
console.log(buyerRole.color) // Works

输出:guildMember.setNickname is not a function

【问题讨论】:

  • 你能展示你的尝试吗,你卡在哪一部分?
  • 好的,我编辑它。
  • 请将您的代码显示为代码块而不是图像。代码块可以复制,图片不能复制

标签: node.js discord.js


【解决方案1】:

那是因为您获取成员但从不等待它。 .fetch 返回一个承诺,所以获取guildMember 的正确方法是这样的:

const guildMember = await guild.members.fetch(message.author.id)

【讨论】:

  • 谢谢,但是当我尝试时,我得到“SyntaxError: await is only valid in async functions and the top level body of modules”
  • 确保回调如下所示:async (message) => {...}
  • 我还是明白了。异步与client.on('messageCreate',async (message)=>{ 是吗?
  • 哦,还有,所有这些都包含在一个 try 方法中。
  • 哦,原来我没有使 fs.readfile 异步。谢谢!
猜你喜欢
  • 2021-11-29
  • 2018-09-10
  • 1970-01-01
  • 2021-12-21
  • 2021-06-17
  • 2021-03-01
  • 2015-03-27
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多