我需要代码将消息发送到我查看过堆栈溢出的通道,但那里太旧了,并且出现了错误
答案 0 :(得分:1)
There is a guide for this on the discord.js guide.
const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');
改进的版本是:
<client>.channels.fetch('<id>').then(channel => channel.send('<content>'))
答案 1 :(得分:1)
首先您需要获取频道 ID 或频道名称才能执行此操作
/* You handle in command and have message */
// With Channel Name
const ChannelWantSend = message.guild.channels.cache.find(channel => channel.name === 'Channel Name');
// With Channel ID
const ChannelWantSend = message.guild.channels.cache.get(channelId);
ChannelWantSend.send('Your Message');
/* If you start from root of your bot , having client */
// With Channel Name
const ChannelWantSend = client.channels.cache.find(channel => channel.name === 'Channel Name');
// With Channel ID
const ChannelWantSend = client.channels.cache.get(channelId);
ChannelWantSend.send('Your Message');
// In both case If ChannelWantSend is undefined where is a small chance that discord.js not caching channel so you need to fetch it
const ChannelWantSend = client.channels.fetch(channelId);
答案 2 :(得分:0)
Discord.js sending a message to a specific channel
不确定您是否已经测试过此代码,但看起来这可以回答您的问题?
我尚未对此进行测试,但我链接的线程似乎已在 2020 年 6 月对其进行了测试!
答案 3 :(得分:0)
不久,我将消息发送到特定频道,如下所示。
<client>.channels.cache.get("<channel_id>").send("SEND TEXT");
代码段下面是我自己的用法。
就我而言,我将所有 Direct Messages 保存到我自己的频道。
const Discord = require('discord.js');
const client = new Discord.Client();
function saveDMToAdminChannel(message) {
var textDM = `${message.author.username}#${message.author.discriminator} : ${message.content}`;
client.channels.cache.get("0011223344556677").send(textDM);
// "0011223344556677" is just sample.
}
client.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type == 'dm') {
saveDMToAdminChannel(message);
}
});
在我自己的频道中,DM 是这样保存的,
00:00 User1#1234 : Please fix bug
07:30 User2#2345 : Please fix bug!!
10:23 User3#3456 : Please fix bug!!!!