我正在执行使用户静音的命令。例如,以下命令将使用户静音 @anon
5秒:
!mute @anon 5
我的程序监听
message
事件,使用户静音并发送如下确认消息:@anon#1234 has now been muted for 5 s
不幸的是,Discord 没有将此消息中的用户名识别为提及。如何使用
msg.channel.send
提及特定用户功能?此示例包含发送确认消息的代码:bot.on("message", msg => {
let args = msg.content.substring(PREFIX.length).split(" ")
let time = args[2]
let person = msg.guild.member(msg.mentions.users.first() || msg.guild.members.fetch(args[1]))
// muting the user here and sending confirmation message
msg.channel.send(`@${person.user.tag} has now been muted for ${time} s`)
setTimeout(() => {
// unmuting the user after specified time and
// sending confirmation message
msg.channel.send(`@${person.user.tag} has been unmuted.`)
}, time * 1000);
})
此示例中不包含静音,它可以工作。消息已正确发送,但未提及用户,这意味着用户名不可点击且未突出显示。
最佳答案
文档推荐 this way提到一个用户:
const message = `${user} has been muted`;
上面的示例使用模板字符串,因此 toString
User
的方法对象被自动调用。虽然可以手动调用它:const message = user.toString() + "has been muted";
该文档指出:When concatenated with a string, this [the user object] automatically returns the user's mention instead of the User object.
这意味着每次
toString
通过模板、字符串连接或手动调用 toString
调用,用户对象将被转换为提及。 Discord 将正确解释输出,突出显示并使其可点击。在您的情况下,您将像这样使用上面的示例:
msg.channel.send(`${person.user} has now been muted for ${time} s`)
setTimeout(() => {
...
msg.channel.send(`${person.user} has been unmuted.`)
}, time * 1000)
关于javascript - 如何使用 discord.js 在消息中提及用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61910304/