我正在尝试为不和谐的bot创建一个更详细的控制台日志,并且希望记录该bot可以看到的所有消息。到目前为止,我做到了:
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转换为用户名,但是对于通道ID和服务器ID却无法做到这一点。我通过使用相同的代码为通道的用户名尝试使用通道ID的类似方法,但是它仍然为我提供了编号。
在控制台中,将显示以下内容:
Kingamezz#0218 in 763786268181397527 of 763786268181397524 said: message text
我的目标是尝试将ID转换为正确的名称,因此得到的结果如下:
Kingamezz#0218 in #general of Testing Server said: message text
如果您要发送可点击的链接作为响应,你可以使用对象本身(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}`,
);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句