[英]How to send a message to a specific mentioned channel?
我正在尝试创建一个命令,该命令在特定channel
中发送提及user
的message
。 该命令的格式如下:
:send @user #channel
这是我的代码:
let user = message.mentions.users.first().id;
let channell = message.mentions.channels.first()
channel.cache.get(`${channell}`).send(`<@${user}>`);
谢谢!
您要做的第一件事是获取command args
中提到的channel
和提到的user
:
const channelID = args[1];
const mentionedUser = message.mentions.members.first();
if(!channelID) return message.reply('You need to provide a channelID!');
if(!mentionedUser) return message.reply(`Please use a proper member mention!`);
const targetChannel = await message.client.channels.fetch(channelID);
要获取
channel
ID
,您必须在 Discord 中激活developer mode
,然后您可以右键单击target channel
并点击Copy channel ID
或者,您应该能够通过其名称获取channel
:
const targetChannel = guild.channels.cache.find(channel => channel.name === "Name of the channel");
完成后,您现在可以将消息发送到目标频道:
targetChannel.send(`Hello ${mentionedUser.toString()}`);
我使用.toString()
的原因是message.mentions.members.first()
返回GuildMember
。 如果您查看文档,您会注意到.toString()
自动返回用户的mention
而不是GuildMember
object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.