簡體   English   中英

如何向特定提及的頻道發送消息?

[英]How to send a message to a specific mentioned channel?

我正在嘗試創建一個命令,該命令在特定channel中發送提及usermessage 該命令的格式如下:

: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM