我正在尝试为我的不和谐机器人创建更详细的控制台日志,并且我想记录机器人可以看到的所有消息。到目前为止我已经这样做了:
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.author
、message.channel
、message.guild
)。但是,如果您想将它们记录在控制台上或保存到文件中,则需要使用 author.tag
、channel.name
和 guild.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}`,
);
});
关于javascript - 如何将 channel /服务器 ID 转换为其名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65959618/