【问题标题】:I am trying to change author nickname by bot in discord using discord.js我正在尝试使用 discord.js 在不和谐中通过机器人更改作者昵称
【发布时间】:2021-10-30 22:37:06
【问题描述】:

我正在尝试使用我在 discord.js 中编码的机器人更改作者昵称。我无法做到这一点,并且在从 airtable 获取数据时出现错误。该机器人具有管理员权限,可以按编码进行更改。我的代码如下

console.log(usernameclan);
console.log("Old user is here");
member.roles.add(rolenumber).catch(console.error);
msg.author.send("Welcome to the clan");
let id = msg.author.id;
client.channels.cache.get(channelnumber).send("Welcome to the clan" + "<@" +msg.author.id+">");
//msg.author.setNickname(usernameclan);
//message.guild.members.get(bot.user.id).setNickname("some nickname");
        

评论部分是我为更改消息作者的昵称所做的 2 次尝试。机器人和用户交互发生在用户的直接消息中。如何更改用户昵称?我也在这里尝试过这段代码,我对任何可行的解决方案持开放态度。我也在我的代码中尝试过这个

let guild = client.guilds.get(serverId);
let student = guild.member(msg.author); 
let nickname = student.setNickname(usernameclan); 

【问题讨论】:

  • 私信不给message.member。您要选择一个公会来将用户转换为成员,然后更改他们的昵称吗?
  • @MrMythical 我愿意接受任何可行的解决方案。我也在我的代码中尝试过这个 let guild = client.guilds.get(serverId);let student = guild.member(msg.author);让昵称 = student.setNickname(usernameclan);

标签: javascript node.js discord.js bots airtable


【解决方案1】:

你不能在直接消息中更改昵称,所以我在这里的确切实现是

member.roles.add(rolenumber).catch(console.error)
<msg>.author.send("Welcome to the clan" + "<@" +msg.author.id+">")
// This
<client>.guilds.cache.get("Guild ID").members.cache.get(<msg>.author.id).setNickname("some nick");

【讨论】:

  • 请注意 已添加,因为您在此处对客户端和消息/消息的定义可能不同,您可以更改它们。
  • 我收到这样的错误消息 E:\Projects\Git\Bot\node_modules\airtable\lib\query.js:118 if (result.offset) { ^ TypeError: Cannot read property ' E:\Projects\Git\Bot\node_modules\airtable\lib\query.js:118:28 在 E:\Projects\Git\Bot\node_modules\airtable\lib\run_action.js:70:17 处未定义的偏移量在 processTicksAndRejections (node:internal/process/task_queues:96:5)。您的解决方案可能是正确的,但在这里不起作用
  • There is an open github issue dedicated to issues such as this,此错误是通过第三方 lib airtable 引发的,它不是 discord.js / js 我建议在其中添加 airtable 标签
  • 感谢您让我注意到这一点,这也在 github 上开放。
【解决方案2】:

DM 不提供.member。您必须先获得公会,然后是那里的成员才能更改他们的昵称。

//msg is an instance of Message (received in DM)
const guild = client.guilds.cache.get("the guild you want to change the member's nickname in")
const member = guild.members.cache.get(msg.author.id)
member.setNickname("new_nickname")

【讨论】:

  • 出现错误 E:\Projects\Git\Bot\node_modules\airtable\lib\query.js:118 if (result.offset) { ^ TypeError: Cannot read property 'offset' of undefined at E :\Projects\Git\Bot\node_modules\airtable\lib\query.js:118:28 在 E:\Projects\Git\Bot\node_modules\airtable\lib\run_action.js:70:17 在 processTicksAndRejections (node:internal /process/task_queues:96:5)
  • 我提供的代码没有返回那个错误。确保它在事件回调中。很有可能是其他代码造成的
  • 感谢您抽出宝贵时间@MrMythical
  • 如果有帮助,请将我的答案标记为正确。
  • 它帮助我更详细地查看文档。虽然还没有收到错误,但感谢您的时间
猜你喜欢
  • 2021-03-10
  • 2021-05-14
  • 2020-08-22
  • 2018-12-09
  • 2018-07-10
  • 2020-07-11
  • 2021-03-29
  • 2019-09-30
  • 1970-01-01
相关资源
最近更新 更多