I am trying to create a command that sends a message
mentioning a user
in a specific channel
. The command is formatted like this:
:send @user #channel
And this is the code I have:
let user = message.mentions.users.first().id;
let channell = message.mentions.channels.first()
channel.cache.get(`${channell}`).send(`<@${user}>`);
Thanks!
The first thing you want to do is getting the channel
mentioned in the command args
and the mentioned 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);
In order to get the
ID
of achannel
you have to activatedeveloper mode
in your Discord, then you can right click thetarget channel
and hitCopy channel ID
Alternatively you should be able to get a channel
by it's name:
const targetChannel = guild.channels.cache.find(channel => channel.name === "Name of the channel");
After that is done you can now send your message to the destinated channel:
targetChannel.send(`Hello ${mentionedUser.toString()}`);
The reason why I'm using .toString()
is because message.mentions.members.first()
returns a GuildMember
. If you look at the docs you'll notice that .toString()
automatically returns the user's mention
instead of the GuildMember
object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.