javascript - 如何将 channel /服务器 ID 转换为其名称?

标签 javascript node.js discord discord.js

我正在尝试为我的不和谐机器人创建更详细的控制台日志,并且我想记录机器人可以看到的所有消息。到目前为止我已经这样做了:

client.on('message', message => {
    const User = client.users.cache.get(message.author.id); // Getting the user by ID.
    const Channel = client.channels.cache.get(message.channel.id); //getting the channel ID
    console.log(User.tag + " in " + message.channel + " of " + message.guild + " said: " + message.content);

    // rest of my code
});

client.login(token);

我能够弄清楚如何将用户 ID 转换为用户名,但我无法对 channel ID 和服务器 ID 执行相同的操作。我通过使用相同的 channel 用户名代码尝试了类似的 channel ID 方法,但它仍然给了我数字。

在控制台中,显示如下:

763786268181397524 的 763786268181397527 中的 Kingamezz#0218 表示:消息文本

我的目标是尝试将 ID 转换为正确的名称,因此我得到的结果如下:

测试服务器#general中的Kingamezz#0218说:消息文本

最佳答案

如果您想发送可点击的链接作为响应,您可以使用对象本身(message.authormessage.channelmessage.guild )。但是,如果您想将它们记录在控制台上或保存到文件中,则需要使用 author.tagchannel.nameguild.name .

以下应该有效:

client.on('message', (message) => {
  if (message.author.bot) return;

  message.channel.send(
    `${message.author} in ${message.channel} of ${message.guild} said _${message.content}_`,
  );
  
  console.log(
    `${message.author.tag} in #${message.channel.name} of ${message.guild.name} said ${message.content}`,
  );
});

enter image description here

关于javascript - 如何将 channel /服务器 ID 转换为其名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65959618/

相关文章:

javascript - 如何在 polymer 元素中调用函数

javascript - 尝试在 html 中链接两个或多个页面,但加载页面时出错

javascript - jquery复制json对象

node.js - 自动向 Express 部分 View 提供数据

python - 我如何在 Python 中为 discord 机器人创建重新加载命令?

python - 从不和谐消息中选择单词不起作用

javascript - 在交互式图表中动态缩放数据,其中数据会发生变化?

node.js - cheerio 选择具有多个类的元素,用空格分隔

node.js - Node.js 中的本地模块和全局模块有什么区别?何时使用本地和全局模块?

python - Discord.py 试图找出所有 "bot.wait_for"类型 str,如 "message"和 "button_click"