-4

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!

Toasty
  • 1,850
  • 1
  • 6
  • 21
Ha t
  • 1
  • 2
    Does this answer your question? [Discord.js sending a message to a specific channel](https://stackoverflow.com/questions/52258064/discord-js-sending-a-message-to-a-specific-channel) – Eduards Apr 20 '21 at 07:37
  • Hey, before you ask the next time please read [ask] :) – Toasty Apr 20 '21 at 08:34

1 Answers1

0

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 a channel you have to activate developer mode in your Discord, then you can right click the target channel and hit Copy 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.

Toasty
  • 1,850
  • 1
  • 6
  • 21