问题描述
我正在使用 discord.js 12,我尝试了以下方法:
I'm using discord.js 12, and I've tried the following:
client.channels.cache.find(x => x.id == "id").send('message')
client.channels.cache.find(x => x.name == "channel-name").send('message')
...and...
client.channels.cache.get('id').send('message')
并且它们都返回一个错误说'无法读取未定义的属性'发送'.
and all of them return an error saying 'cannot read property 'send' of undefined.
现在,我知道这通常意味着频道的 ID 错误.但是,我已经将它复制并重新粘贴了五次.我向你保证,这是正确的 ID.
Now, I know that usually means that the ID of the channel is wrong. However, I've copied it and repasted it like five times. It's the right ID, I assure you.
我已经使用这个系统几个月了,现在它不能工作了.
I've been using this system for months, and now it won't work.
注意:我在文件中定义了以下内容:
Note: I have the following defined in the file:
const Discord = require('discord.js')
const client = new Discord.Client()
请帮帮我!
推荐答案
你要么没有缓存通道,要么它不存在.如果它不存在,我无法帮助你.它返回未定义,因为它在缓存中看不到它.您可以使用简单的 fetch()
方法轻松缓存频道.
You are either not caching the channel, or it doesn’t exist. I can’t help you if it doesn’t exist. It is returning undefined because it doesn’t see it in the cache. You can easily cache the channels by using a simple fetch()
method.
await client.channels.fetch();
client.channels.cache.get(…).send(…)
您也可以使用 API 直接获取频道
You can also directly fetch the channel using the API
(await client.channels.fetch('id')).send(…)
如果您收到一条错误提示 await 仅在异步函数中有效,请将侦听器更改为如下所示:
If you are getting an error that says await is only valid in async functions, change the listener to look like this:
client.on(event, async (variable) => {
//code here
//notice it says 'async' before 'variable'
})
//'event' can be any event, variable is the object provided (message events provide message objects)
这篇关于在特定频道中发送消息 - 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!