问题描述
我希望创建一个说"命令,允许我通过机器人直接向特定(或任何,也许)频道发送消息.对于初学者和一般测试目的,我的目标是能够使用标准 if(commandIs("command", message)
直接向我的测试服务器中的通道发送消息,最终演变为所有通道在服务器上.
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,这正是我想要做的基础,因为我可以将 .get("name", "My server")
换成实际的频道 ID,但是通过类似say的命令执行此操作将 var
设置为我的代码中的 Channel 类,它不支持 .sendMessage()
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));
-但这会在 .sendMessage()
上产生一个 undefined
错误,我认为会这样.我已经尝试了 message.chat.sendMessage()
和所有其他可能的变体,甚至在 on.ready()
处仅使用两行代码进行测试,但这继续给我同样的错误.我一直在寻找一种方法来绕过找到 ID 后创建的 Channel
类,这导致我使用了 TextChannel
和 GuildChannel
扩展,但是考虑到所有代码(甚至这里有几个示例)不包含所有额外信息,我很确定有一种更简单的方法.我觉得我正在查看某些内容,或者可能使代码复杂化了超出需要的程度,但我不确定.
-but this brings up an undefined
error 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.
看来我是对的,我查看了几个关键点,特别是 channelID 周围没有引号是一个字符串.单独尝试了这个命令,一切都很顺利;调整了主要代码,一切顺利.
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.
推荐答案
首先,我只需要概述一些在 Discord.JS 库中不再起作用的东西(感谢单挑 Ty Q.!)
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.!)
首先,.sendMessage()
已被弃用,并且很可能不再/很快不再工作..send()
的工作等效项 - 它更简单,并且很容易进行更改.
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.
第二:不幸的是,.defaultChannel
不再是一个东西.当我们想要发送消息时,我们必须更加具体.
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.
好吧,让我们这样设置你的命令:a~speak [ChannelID] <Message>
Alright, let's let your command be set out like this:a~speak [ChannelID] <Message>
假设我们使用这个命令:a~speak 12345689 I have a bucket
Let's say we use this command: a~speak 12345689 I has a bucket
我们要做的是找出我们是否可以找到具有ID的频道,如果可以,将消息发送给它.
What 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.
为此,我认为我们应该看看我们是否可以使用 client.channels.find("id", ID)
函数.
To do that, I think we should see if we can use the client.channels.find("id", ID)
function.
if(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(" "));
}
}
让我们按照这个命令来
如果我们输入 a~speak 12345689 I have a bucket
,我们的机器人将查找 ID 为123456789"的可以访问的频道.
Let's Follow That Command
If 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".
chan.send(args.shift().join(" "));
我们之前定义了 chan = args[0]
- 这将是123456789",我们不想在消息中发送它,所以我们使用 args.shift()
- 这会从其余参数中删除第一个参数,留下 ["I", "has", "a", "bucket"]
.
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"]
.
之后,我们使用 .join(" ")
- 这会将 ["I", "has", "a", "bucket"]
与空格连接起来形成我有一个桶
.
After that, we use .join(" ")
- this connects ["I", "has", "a", "bucket"]
with spaces to form I has a bucket
.
然后我们只需使用 chan.send()
将该消息发送到我们定义的通道.
We then simply send that message to the channel we defined, with chan.send()
.
message.channel.send(args.join(" "));
这在某些方面有点不同,首先,我们忘记了 chan
变量,因为我们找不到那个频道.如果我们知道这一点,我们可以推断出用户可能会说诸如 a~speak I have a bucket
之类的东西 - 完全没有 ID.
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.
知道,我们(与以前不同),不想要args.shift()
- 我们会删除I"位!相反,我们只想用 args.join(" ")
再次将单词连接成一个句子,然后用 message.channel.send()
发送出去- 它只是将它发送到它从中获取消息的服务器中.
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.
官方 Discord.JS 服务器是在这类主题 (Discord.JS) 上寻求帮助的好地方 和 AnIdiotsGuide.两者都能解决你的问题.
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 跨渠道发送消息会产生“未定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!