【问题标题】:discord.js - Issues with sending a messagediscord.js - 发送消息的问题
【发布时间】:2021-06-05 14:58:47
【问题描述】:

我有一个命令处理程序,它是模块并像这样构造。

module.exports ={
    name: "test",
execute(message, args) {
var yes = client.channels.cache.get('818107649912209438')
yes.send('some message')
}

每当我尝试向特定频道发送消息时,机器人都会返回一个错误:

  var yes = client.channels.cache.get("818107649912209438");
                     ^

TypeError: Cannot read property 'channels' of undefined

我一直在尝试解决这个问题,但没有运气。如果有人知道它为什么不起作用并愿意提供帮助,我将不胜感激。

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    您的错误'of undefined' 表示您正在通过某些东西访问通道属性,但没有定义某些东西。在此命令处理程序中,您没有定义 client。您必须将client 对象作为您在主文件中定义的参数传递,例如execute(message,args,client),然后访问客户端的属性或方法。

    【讨论】:

      【解决方案2】:

      你的问题不清楚,所以我将给你一个如何处理的例子,我稍后会显示代码和解释。

      格式:

      • 文件夹
      1. 文件

      • 事件
      1. message.js
      // I used message as msg to shorten my code.
      const all_requires = require("../settings/settings"); // Exported all requires
      const { config, Discord, client } = all_requires; // Imported what I need
      module.exports = (client, msg) => {
      
          if(msg.author.bot || !msg.content.startsWith(config.prefix)) return;
          const args = msg.content.split(" ").slice(1);
          const command = msg.content.split(' ')[0].slice(config.prefix.length);
          const cmd = client.commands.get(command); //enmap handler
          console.log(cmd);
          if(!cmd) return;
      
          cmd.run(msg, args);
      }
      
      • 命令
      1. ping.js
      module.exports.run = (msg) => {
          msg.channel.send("Pong!");
      }
      
      • 配置
      1. config.json
      {
          "token": "TOKEN-HERE",
          "prefix": "+"
      }
      
      • 设置
      1. settings.js
      const Discord = require("discord.js");
      const fs = require("fs");
      const enmap = require("enmap");
      const config = require("../config/config.json");
      const client = new Discord.Client();
      
      const all_requires = { Discord, fs, enmap, config, client }
      module.exports = all_requires;
      

      index.js

      const all_requires = require("./settings/settings");
      const { Discord, fs, enmap, config, client } = all_requires;
      client.commands = new enmap;
      
      client.once("ready", () => {
          console.log(`${client.user.tag} is Connected.`);
      })
      
      fs.readdir("./events/", (err, files) => {
          if(err) return console.error(err);
          files.forEach(file => {
              const event = require(`./events/${file}`);
              let eventName = file.split(".")[0];
              client.on(eventName, event.bind(null, client));
          });
          console.log(`${files.length} Events Loaded.`)
      });
      
      fs.readdir("./commands/", (err, files) => {
          if(err) return console.error(err);
          files.forEach(file => {
              let props = require(`./commands/${file}`);
              let commandName = file.split(".")[0];
              client.commands.set(commandName, props);
          });
          console.log(`Loaded ${files.length} Commands.`);
      });
      
      process.on('unhandledRejection', err => console.log(err));
      client.login(config.token);
      

      Files Structure

      作为处理程序,您需要向我展示整个代码以了解您正在做什么来处理命令,您可以使用 enmap link 就像在该基本代码的示例中一样,或者使用 discord.js @ 987654323@ 执行命令的处理程序,查看message.js Line:8 client.commands.get() 这里我们定义了 enmap 处理程序,而在 Line:13cmd.run() 我们运行命令,只使用module.exports.run 并且我们在index.js Line:3 中定义了命令处理程序,我们在fs 内的Line:24 中设置了命令strong>for 循环。

      -享受编码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-17
        • 1970-01-01
        • 2021-04-01
        • 2018-08-25
        • 2020-08-04
        • 2021-02-05
        • 2020-10-08
        相关资源
        最近更新 更多