本文介绍了使用Discord.js在通道之间发送消息会产生“未定义”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在寻求创建一个 say命令,该命令可让我直接通过漫游器向特定(或可能的任何)渠道发送消息。对于初学者和一般测试目的,我的目标是能够使用标准的 if(commandIs( command,message)直接向测试服务器中的频道发送消息,最终演变为服务器上的所有通道。 通过研究,我偶然发现: var channel = client.servers.get( name, My Server)。defaultChannel; client.sendMessage(channel, Hello); 代码,这正是我要作为基础执行的操作,因为我可以换出 .get( name, My服务器)来获取实际的频道ID,但是通过一个类似say的命令来完成此操作,将 var 设置为我的代码中的Channel类不支持 .sendMessage() 我当前的命令代码如下: if(commandIs( speak,message)){ var chat = client.channels.get(18numberID); 如果(args.length === 1){ message.channel.sendMessage( ’您什么都没说。用法:ʻa〜speak [message]`')} else { chat.sendMessage(args.join().substring(8)); -但这会引起 undefined 错误在 .sendMessage()上,我认为是这样。我已经尝试过 message.chat.sendMessage()以及我能做的所有其他可能的变体,甚至只用裸露的两行代码在进行测试on.ready(),但这继续给我同样的错误。我一直在寻找找到ID后创建的 Channel 类的方法,这导致我进入 TextChannel 和 GuildChannel 扩展名,但我可以肯定,考虑到所有代码(即使是此处的几个示例)也不包含所有这些额外信息,有一种更简便的方法。我觉得我正在寻找某些东西,或者可能会使代码变得比所需的复杂,但我不确定。 任何想法或帮助都将不胜感激。 / p> 编辑:看来我是对的,我查看了几个关键点,尤其是channelID周围没有引号的是字符串。单独尝试了该命令,一切进展顺利。 解决方案首先,我只需要概述一些不再需要的内容在 Discord.JS 库中工作(感谢Tys-up Ty Q !!) 首先, .sendMessage()已被弃用,很可能不再/很快将不再起作用。等效的工作是 .send()-更简单,而且很容易更改。 第二:不幸的是, .defaultChannel 不再是一回事。当我们要发送消息时,我们将必须更加具体。 我不太擅长谈论此类问题,因此如果有人愿意选择 好吧,让我们这样设置您的命令: a〜speak [ChannelID]< Message> 假设我们使用以下命令: a〜speak 12345689 I有一个水桶 我们要做的就是找出是否可以找到带有ID的频道,如果可以,发送 为此,我认为我们应该看看是否可以使用 client.channels.find( id,ID )函数。 让我们开始设置命令 if(commandIs( speak,message)){ args = args.shift(); / * [ a〜speak, CHANNELID, I, has, a, bucket] 成为 [ 123456789, I , has, a, bucket] args [0] [1] [2] [3] [4] * / let chan = client.channels.find( id,args [0]); //找到频道ID 123456789 if(chan){//检查该频道是否存在 chan.send(args.shift()。join()); } else { message.channel.send(args.join()); } } 让我们跟随该命令 如果我们键入 a〜speak 12345689我有一个水桶,我们的机器人将寻找可以访问其ID为 123456789的频道。 如果您的漫游器找到ID为 123456789的频道: chan.send(args.shift()。join()); 我们之前定义了 chan = args [0] -就是 123456789 ,并且我们不想在消息中发送该消息,因此我们使用 args.shift()-这会从其余参数中删除第一个参数,从而使我们剩下 [ I, has, a, bucket] 。 之后,我们使用 .join()-这连接了 [ I, has, a, bucket] 带有空格的形式我有一个水桶。 我们然后将该消息发送到我们定义的频道,与 chan.send()。 如果您的漫游器没有找到ID为 12456789的频道 message.channel.send(args.join()); 首先,我们忘记了 chan 变量,因为找不到该频道。如果我们知道这一点,我们可以推断出用户可能说了 a〜speak I have a bucket -完全没有ID之类的话。 知道这一点,我们(与以前不同)不要想要 args.shift()-我们会删除我位!相反,我们只想 args.join()再次将单词组合成一个句子,然后用消息将其发送出去。 channel.send()-仅在收到消息的服务器中发送它。 如果您需要更多帮助,只需用您的Discord名称发表评论,我们将很乐意为您提供一些正确方向的帮助。 来源信息: 在这些主题(Discord.JS)上寻求帮助的一些好地方是官方Discord.JS服务器和 AnIdiotsGuide 。两者都可以解决您的问题。 I'm looking to create a 'say' command that allows me to directly message through the bot to a specific (or any, perhaps) channel. For beginner, and general testing purposes, my goal is to be able to use a standard if(commandIs("command", message) to directly message a channel in my test server, eventually evolving to all channels on a server.Through my research I've stumbled upon the:var channel = client.servers.get("name", "My Server").defaultChannel;client.sendMessage(channel, "Hello");code, which is exactly what I'm looking to do as a base since I can swap out the .get("name", "My server") for the actual channel ID, but doing this through a say-like command sets the var as a Channel class in my code, which doesn't support .sendMessage()My current command code looks like:if(commandIs("speak", message)){ var chat = client.channels.get(18numberID); if(args.length === 1){ message.channel.sendMessage('You did not say anything. Usage: `a~speak [message]`') } else { chat.sendMessage(args.join(" ").substring(8));-but this brings up an undefinederror on the .sendMessage(), which I figured it would. I've tried message.chat.sendMessage() and every other possible variation I could, even going to the bare two lines of code to test at on.ready(), but that continued to give me the same error. I've looked for a way around the Channel class created once the ID is found and that led me to the TextChannel and GuildChannel extensions, but I'm pretty sure there's an easier way around it considering all the code (even a couple examples here) do not contain all that extra information. I feel like I'm looking over something, or possibly complicating the code more than needed, but I'm not sure.Any ideas or help would be appreciated.Edit: It seems I was right and I looked over a few key points, specifically the channelID not having quotes around it to be a string. Tried the command alone and everything went great; tweaked the main code and it all worked out. 解决方案 First of all, I just have to outline a few things that no longer work in the Discord.JS library (Thanks for the heads-up Ty Q.!)First of all, .sendMessage() has been deprecated, and will more than likely not work at all anymore / soon. The working equivalent of this is .send() - which is simpler, and is an easy change to make.Second: .defaultChannel is unfortunately no longer a thing. We will have to be more specific when we want to send a message.I'm not great at talking about these kinds of issues, so if anyone else wants to pick it up, please do.Alright, let's let your command be set out like this:a~speak [ChannelID] <Message>Let's say we use this command: a~speak 12345689 I has a bucketWhat we want to do is find out whether we can find the channel with the ID, and if we can, send the message to it.To do that, I think we should see if we can use the client.channels.find("id", ID) function.Let's Start by Setting Up Our Commandif(commandIs("speak", message)){ args = args.shift(); /* ["a~speak", "CHANNELID", "I", "has", "a", "bucket"] BECOMES ["123456789", "I", "has", "a", "bucket"] args [0] [1] [2] [3] [4]*/ let chan = client.channels.find("id", args[0]); // Find the channel ID "123456789" if(chan) { // Check if that channel exists chan.send(args.shift().join(" ")); } else { message.channel.send(args.join(" ")); }}Let's Follow That CommandIf we type in a~speak 12345689 I has a bucket, our bot will look for a channel that it has access to with the id "123456789".If your bot found the channel with the id "123456789":chan.send(args.shift().join(" "));We defined earlier that chan = args[0] - which would be "123456789", and we don't want to send that in the message, so we use args.shift() - this removes the first argument from the rest, leaving us with ["I", "has", "a", "bucket"].After that, we use .join(" ") - this connects ["I", "has", "a", "bucket"] with spaces to form I has a bucket.We then simply send that message to the channel we defined, with chan.send().If your bot did not find the channel with the id "12456789"message.channel.send(args.join(" "));This one's a bit different in a few ways, first of all, we forget about the chan variable, because we couldn't find that channel. If we know this, we can deduce that the user may have instead said something like a~speak I has a bucket - without the ID altogether.Knowing that, we (unlike before), don't want to args.shift() - we would remove the "I" bit! Instead, we just want to args.join(" ") to once again join the words together into a sentence, then send it away with message.channel.send() - which just sends it in the server it got the message from.If you need any more help, just comment with your Discord name, and I'll be happy to give you a little push in the right direction.Sources of Information:Some good places to look for help on these kinds of topics (Discord.JS), are the Official Discord.JS Server and AnIdiotsGuide. Both of which will be able to solve your problems. 这篇关于使用Discord.js在通道之间发送消息会产生“未定义”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-07 05:58