【问题标题】:how to send a message to specific channel Discord.js如何向特定频道 Discord.js 发送消息
【发布时间】:2021-08-09 08:00:07
【问题描述】:

我需要代码将消息发送到我查看过堆栈溢出的通道,但那里太旧了,并且出现错误

【问题讨论】:

  • 请分享您的代码以展示您的尝试。

标签: javascript node.js discord discord.js


【解决方案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>'))

【讨论】:

  • TypeError: Cannot read property 'send' of undefined at loginconsole (C:\Users\theen\Desktop\c3 SigServer\sigserv.js:30:9) at Object. (C: \Users\theen\Desktop\c3 SigServer\sigserv.js:72:1) 在 Module._compile (internal/modules/cjs/loader.js:1137:30) 在 Object.Module._extensions..js (internal/modules /cjs/loader.js:1157:10) 在 Module.load (internal/modules/cjs/loader.js:985:32) 在 Function.Module._load (internal/modules/cjs/loader.js:878:14 ) 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
  • @DrMeepso 我添加了一个改进的单行版本。您遇到的错误很可能是由于 discord.js 没有足够快地获取频道引起的,但请检查 ID 是否正确
  • @mmoomocow 不是这样。他正在处理这些事件。此外,频道始终被缓存,因此您无需获取它们。
【解决方案2】:

Discord.js sending a message to a specific channel

不确定您是否已经测试过此代码,但看起来这可能会回答您的问题?

我没有对此进行测试,但我链接的线程似乎已经在 2020 年 6 月进行了测试!

【讨论】:

  • TypeError: client.channels.get is not a function at loginconsole (C:\Users\theen\Desktop\c3 SigServer\sigserv.js:28:17) at Object. (C :\Users\theen\Desktop\c3 SigServer\sigserv.js:70:1) 在 Module._compile (internal/modules/cjs/loader.js:1137:30) 在 Object.Module._extensions..js (internal/ modules/cjs/loader.js:1157:10) 在 Module.load (internal/modules/cjs/loader.js:985:32) 在 Function.Module._load (internal/modules/cjs/loader.js:878: 14) 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
  • 要使用此功能,您必须先使用'client.login('INSERT TOKEN HERE')' 登录到 Discord 客户端,其中使用的令牌是您的机器人令牌
【解决方案3】:

首先您需要获取频道 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);

【讨论】:

  • ReferenceError: message is not defined at Object. (C:\Users\theen\Desktop\c3 SigServer\sigserv.js:26:25) ←[90m at Module._compile (internal /modules/cjs/loader.js:1137:30)←[39m ←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)←[39m ←[90m at Module.load (internal/modules/cjs/loader.js:985:32)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)←[39m ←[ 90m 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)←[39m ←[90m at internal/main/run_main_module.js:17:47←[39m
  • 您在哪里使用该代码块。在你的机器人的命令或根目录中
  • 登录控制台调用在哪里请提供更多代码
【解决方案4】:

很快,我将消息发送到特定频道,如下所示。

<client>.channels.cache.get("<channel_id>").send("SEND TEXT");

代码段下面是我自己的用法。
就我而言,我将所有私信保存到我自己的频道。

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!!!!

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 2020-04-04
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2022-01-25
    • 2021-10-19
    相关资源
    最近更新 更多