Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向特定频道Discord.js发送消息_Javascript_Node.js_Discord_Discord.js - Fatal编程技术网

Javascript 如何向特定频道Discord.js发送消息

Javascript 如何向特定频道Discord.js发送消息,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我需要代码将消息发送到一个通道,我已经查看了堆栈溢出,但是那里太旧了,出现了一个错误 const channel=.channels.cache.get(“”); 频道发送(“”); 改进的版本将是: .channels.fetch(“”)。然后(channel=>channel.send(“”)) 不确定您是否已经测试过这段代码,但这似乎可以回答您的问题 我还没有测试过这个,但是我链接的线程似乎已经在2020年6月测试过了 首先,您需要获取频道ID或频道名称才能执行此操作 /*您在命令中进

我需要代码将消息发送到一个通道,我已经查看了堆栈溢出,但是那里太旧了,出现了一个错误

const channel=.channels.cache.get(“”);
频道发送(“”);
改进的版本将是:

.channels.fetch(“”)。然后(channel=>channel.send(“”))

不确定您是否已经测试过这段代码,但这似乎可以回答您的问题


我还没有测试过这个,但是我链接的线程似乎已经在2020年6月测试过了

首先,您需要获取频道ID或频道名称才能执行此操作

/*您在命令中进行处理并获得消息*/
//使用频道名称
const ChannelWantSend=message.guild.channels.cache.find(channel=>channel.name==='channel name');
//使用通道ID
const ChannelWantSend=message.guild.channels.cache.get(channelId);
ChannelWantSend.send(“您的消息”);
/*如果你从你的机器人的根开始,有一个客户端*/
//使用频道名称
const ChannelWantSend=client.channels.cache.find(channel=>channel.name==='channel name');
//使用通道ID
const ChannelWantSend=client.channels.cache.get(channelId);
ChannelWantSend.send(“您的消息”);
//在这两种情况下,如果ChannelWantSend未定义,discord.js不缓存通道的可能性很小,所以您需要获取它
const ChannelWantSend=client.channels.fetch(channelId);

很快,我将消息发送到特定的频道,如下面的

.channels.cache.get(“”).send(“发送文本”);
代码块下面是我自己的用法。
在我的例子中,我将所有直接消息保存到自己的频道

const Discord=require('Discord.js');
const client=new Discord.client();
函数saveDMToAdminChannel(消息){
var textDM=`${message.author.username}{message.author.discriminator}:${message.content}`;
client.channels.cache.get(“0011223344556677”).send(textDM);
//“001122334456677”只是一个样本。
}
client.on(“消息”,异步消息=>{
if(message.author.bot)返回;
如果(message.channel.type=='dm'){
saveDMToAdminChannel(消息);
}
});
在我自己的频道中,DM的保存方式如下:

00:00 User1#1234 : Please fix bug
07:30 User2#2345 : Please fix bug!!
10:23 User3#3456 : Please fix bug!!!!

TypeError:无法读取对象处loginconsole(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:30:9)上未定义的属性“send”。(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:72:1)at Module._compile(internal/modules/cjs/loader.js:1137:30)at Object.Module._extensions..js(internal/modules/cjs/loader.js:1157:10)at Module.load(internal/modules/cjs/cjs/loader.js:985:32)at Function/modules/cjs:878:14)在Function.executeUserEntryPoint[as runMain](internal/modules/run_main.js:71:12)@DrMeepso,我添加了一个改进的单行版本。您遇到的错误很可能是由于discord.js获取通道的速度不够快,但请检查ID是否正确correct@mmoomocow不是这样。他正在做这些事情。此外,通道总是缓存的,所以您不需要获取它们。ReferenceError:对象上未定义消息。(C:\Users\theen\Desktop\c3sigserver\sigserv.js:26:25)←[90米模块处。_编译(内部/modules/cjs/loader.js:1137:30)←[39m←[Object.Module._extensions..js处为90m(internal/modules/cjs/loader.js:1157:10)←[39m←[Module.load处90米(内部/modules/cjs/loader.js:985:32)←[39m←[90米功能模块加载(内部/modules/cjs/loader.js:878:14)←[39m←[Function.executeUserEntryPoint[as runMain](internal/modules/run_main.js:71:12)处的[90m]←[39m←[90米在内部/主/运行主模块处。js:17:47←[39M您在哪里使用该代码块。在命令或根目录中,我希望这对登录控制台调用有帮助。请提供更多代码类型错误:client.channels.get不是loginconsole的函数(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:28:17)at Object。(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:70:1)在Module.处编译(internal/modules/cjs/loader.js:1137:30)在Object.Module.处扩展..js(internal/modules/cjs/loader.js:1157:10)在Module.load(internal/modules/cjs/loader.js:985:32)在Function.executeUserEntryPoint.处加载(internal/modules/cjs/cjs/loader.js:878:14)(internal/modules/run_main.js:71:12)要使用此功能,您必须首先使用“client.login('INSERT TOKEN HERE')登录到Discord客户端,其中使用的令牌是您的bot令牌。请共享您的代码以显示您的尝试。