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

时间:2021-04-13 08:38:23

标签: javascript node.js discord 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.

有谁能帮帮我 我将不胜感激

2 个答案:

答案 0 :(得分:2)

好吧,Bot 又名 Client 在登录之前不存在,因此没有缓存任何频道。这就是频道是 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);

答案 1 :(得分:0)

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

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);