繁体   English   中英

类型错误:无法在 discord.js 读取未定义错误的属性“发送”

[英]TypeError: Cannot read property 'send' of undefined erorr at discord.js

我一直在为一个问题苦苦挣扎

我写了这段代码

const Discord = require('discord.js');
const botsettings = require('./botsettings.json');
const Bot = new Discord.Client({disableEveryone: true});
const Channel = Bot.channels.cache.get(botsettings.channelid);

Bot.on("ready",async() => {
    Channel.send("Hi, @everyone")
})

Bot.login(botsettings.token);

但是因为我收到了这个错误

TypeError: Cannot read property 'send' of undefined

完整的错误在这里

(node:10792) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at Client.<anonymous> (c:\Users\Acer\Documents\GitHub\helpot\index.js:7:13)
    at Client.emit (events.js:315:20)
    at WebSocketManager.triggerClientReady (c:\Users\Acer\Documents\GitHub\helpot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:431:17)
    at WebSocketManager.checkShardsReady (c:\Users\Acer\Documents\GitHub\helpot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:415:10)
    at WebSocketShard.<anonymous> (c:\Users\Acer\Documents\GitHub\helpot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:197:14)
    at WebSocketShard.emit (events.js:315:20)
    at WebSocketShard.checkReady (c:\Users\Acer\Documents\GitHub\helpot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12)
    at WebSocketShard.onPacket (c:\Users\Acer\Documents\GitHub\helpot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16)
    at WebSocketShard.onMessage (c:\Users\Acer\Documents\GitHub\helpot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (c:\Users\Acer\Documents\GitHub\helpot\node_modules\ws\lib\event-target.js:132:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:10792) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10792) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

有没有人可以帮助我,我将不胜感激

好吧, Bot aka Client在登录之前并不存在,因此没有缓存任何频道。 这就是 Channel 是undefined的原因。
尝试这个

const Discord = require('discord.js');
const botsettings = require('./botsettings.json');
const Bot = new Discord.Client({disableEveryone: true});

Bot.on("ready",async() => {
    //Now the bot is logged in and all channels are ready to be cached
    const Channel = Bot.channels.cache.get(botsettings.channelid);
    Channel.send("Hi, @everyone")
})

Bot.login(botsettings.token);

当机器人启动时,您没有任何缓存频道。 您需要获取它们。

const Discord = require('discord.js');
const botsettings = require('./botsettings.json');
const Bot = new Discord.Client({disableEveryone: true});
const Channel = Bot.channels.fetch(botsettings.channelid); // here

Bot.on("ready",async() => {
    Channel.send("Hi, @everyone")
})

Bot.login(botsettings.token);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM