【问题标题】:Cannot read property 'send' of undefined at Client无法在客户端读取未定义的属性“发送”
【发布时间】:2022-01-09 04:46:54
【问题描述】:
console.log("If This Take Too long make sure u have add right token!")
const fs = require('fs')
const yaml = require("js-yaml");
const { mainprefix , token, messagesfetchlimts, status, botlog } = yaml.load(fs.readFileSync("./config.yml"));
const Discord = require('discord.js')
const client = new Discord.Client();
const db = require('quick.db')
const { join } = require('path');
const { readdirSync } = require('fs');
client.commands= new Discord.Collection();
client.login(process.env.token);

  
client.on('ready', () => {
    client.channels.cache.get(botlog).send(`** Bot Is Running**\n Servers: ${client.guilds.cache.size} And ${client.users.cache.size} Users`)
    client.user.setActivity(status, { type: 'PLAYING' });
    console.clear();
    console.log('\x1b[36m%s\x1b[0m', `
    ▓█████▄  ▄▄▄       ██▀███   ██ ▄█▀  
  ▒██▀ ██▌▒████▄    ▓██ ▒ ██▒ ██▄█▒   
  ░██   █▌▒██  ▀█▄  ▓██ ░▄█ ▒▓███▄░   
  ░▓█▄   ▌░██▄▄▄▄██ ▒██▀▀█▄  ▓██ █▄   
  ░▒████▓  ▓█   ▓██▒░██▓ ▒██▒▒██▒ █▄  
   ▒▒▓  ▒  ▒▒   ▓▒█░░ ▒▓ ░▒▓░▒ ▒▒ ▓▒   
   ░ ▒  ▒   ▒   ▒▒ ░  ░▒ ░ ▒░░ ░▒ ▒░     
   ░ ░  ░   ░   ▒     ░░   ░ ░ ░░ ░      
     ░          ░  ░   ░     ░  ░       
   ░                                                                                 `);
  console.log('\n\x1b[32m%s\x1b[0m', `          $[INFO]: Logged on ${client.user.tag}`);  
  console.log('\x1b[32m%s\x1b[0m', `           $[INFO]: PREFIX ${mainprefix}`);  
  console.log('\x1b[32m%s\x1b[0m', `           $[EXTRA]: EZBACKUP`);  

  console.log('\n\x1b[31m%s\x1b[0m', `           $[HOW]: To Check Commands List Type ${mainprefix}help`);  
  console.log('\x1b[31m%s\x1b[0m', `            $[CREDITS]: Made By DarkBoy/DarkMisehl YT`);  
  console.log('\x1b[31m%s\x1b[0m', `            $[SUPPORT]: DarkDevs: https://discord.gg/rSvmYx8 `);  

 });

 const commandFiles = readdirSync(join(__dirname, "commands")).filter(file => file.endsWith(".js"));

 for (const file of commandFiles) {
     const command = require(join(__dirname, "commands", `${file}`));
     client.commands.set(command.name , command);
 }
 
 client.on("message", async message => {
    let prefix = await db.get(`prefix_${message.guild.id}`);
   if(prefix === null) prefix = mainprefix;
       if(message.author.bot) return;
       if(message.channel.type === 'dm') return;
   
       if(message.content.startsWith(prefix)) {
           const args = message.content.slice(prefix.length).trim().split(/ +/);
   
           const command = args.shift().toLowerCase();
   
           if(!client.commands.has(command)) return;
   
   
           try {
               client.commands.get(command).run(client, message, args);
               if (!command) command = client.commands.get(client.aliases.get(command));

           } catch (error){
               console.error(error);
           }
        }
   })
   client.on("guildCreate", guild => {
    client.channels.cache.get(botlog).send(`** NEW GUILD **\n Server: ${guild.name}\n Server ID: ${guild.id}`)
  });
  client.on("guildRemove", guild => {
    client.channels.cache.get(botlog).send(`** GUILD REMOVED **\n Server: ${guild.name}\n Server ID: ${guild.id}`)
  });

我遇到的错误

If This Take Too long make sure u have add right token!
(node:1603) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at Client.<anonymous> (/home/runner/EzBackup-Bot/ezbackup.js:16:38)
    at Client.emit (events.js:314:20)
    at WebSocketManager.triggerClientReady (/home/runner/EzBackup-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:431:17)
    at WebSocketManager.checkShardsReady (/home/runner/EzBackup-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:415:10)
    at WebSocketShard.<anonymous> (/home/runner/EzBackup-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:197:14)
    at WebSocketShard.emit (events.js:314:20)
    at WebSocketShard.checkReady (/home/runner/EzBackup-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:475:12)
    at WebSocketShard.onPacket (/home/runner/EzBackup-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:447:16)
    at WebSocketShard.onMessage (/home/runner/EzBackup-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/EzBackup-Bot/node_modules/ws/lib/event-target.js:125:16)
(node:1603) 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

【问题讨论】:

  • botlog 是无效 ID。 botlog 的值是多少?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: node.js discord.js


【解决方案1】:

正如属性所说,它是 cache,机器人的缓存在每次机器人重启时都会重新设置,所以当 ready 事件触发时,缓存还没有形成良好的格式

我建议你在这种情况下使用fetch()方法

例子:

var logchannel = await client.channels.fetch(botlog); // We use await because fetch returns a promise

Documentation

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2021-11-21
    相关资源
    最近更新 更多