discord.js-发送消息时出现问题

discord.js-发送消息时出现问题,discord,discord.js,Discord,Discord.js,我有一个命令处理程序,它是模块,构造如下 module.exports ={ name: "test", execute(message, args) { var yes = client.channels.cache.get('818107649912209438') yes.send('some message') } 每当我尝试向特定通道发送消息时,bot都会返回一个错误,该错误是: var yes = client.channels.cache.get(

我有一个命令处理程序,它是模块,构造如下

module.exports ={
    name: "test",
execute(message, args) {
var yes = client.channels.cache.get('818107649912209438')
yes.send('some message')
}
每当我尝试向特定通道发送消息时,bot都会返回一个错误,该错误是:

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

TypeError: Cannot read property 'channels' of undefined

我一直在努力解决这个问题,但运气不好。如果有人知道它不起作用的原因并愿意提供帮助,我将不胜感激。

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

您的错误
“of undefined”
意味着您正在通过某个对象访问通道属性,但该对象未定义。在此命令处理程序中,您没有定义
客户机
。您必须将
client
对象作为您在主文件中定义的参数传递,如
execute(message,args,client)
,然后访问客户端的属性或方法。

您的问题不清楚,因此我将向您展示一个如何处理的示例,稍后我将展示代码和解释

格式:

  • 文件夹
  • 文件

    • 事件
  • message.js
  • //我使用消息作为消息来缩短代码。
    const all_requires=require(“../settings/settings”);//导出所有需要的
    const{config,Discord,client}=all_requires;//我需要什么
    module.exports=(客户端,消息)=>{
    if(msg.author.bot | | |!msg.content.startsWith(config.prefix))返回;
    常量args=msg.content.split(“”).slice(1);
    const command=msg.content.split(“”)[0].slice(config.prefix.length);
    const cmd=client.commands.get(command);//enmap处理程序
    console.log(cmd);
    如果(!cmd)返回;
    cmd.run(msg,args);
    }
    
    • 命令
  • ping.js
  • module.exports.run=(msg)=>{
    msg.channel.send(“Pong!”);
    }
    
    • 配置
  • config.json
  • {
    “token”:“token-HERE”,
    “前缀”:“+”
    }
    
    • 背景
  • settings.js
  • const Discord=require(“Discord.js”);
    常数fs=要求(“fs”);
    常量enmap=需要(“enmap”);
    const config=require(“../config/config.json”);
    const client=new Discord.client();
    const all_requires={Discord,fs,enmap,config,client}
    module.exports=所有_要求;
    

    index.js

    const all_requires=require(“./settings/settings”);
    const{Discord,fs,enmap,config,client}=all_所需;
    client.commands=new enmap;
    client.once(“就绪”,()=>{
    log(`${client.user.tag}已连接。`);
    })
    fs.readdir(“./events/”,(错误,文件)=>{
    if(err)返回控制台。error(err);
    files.forEach(文件=>{
    const event=require(`./events/${file}`);
    让eventName=file.split(“.”[0];
    on(eventName,event.bind(null,client));
    });
    console.log(`${files.length}事件已加载。`)
    });
    fs.readdir(“./commands/”,(错误,文件)=>{
    if(err)返回控制台。error(err);
    files.forEach(文件=>{
    让props=require(`./commands/${file}`);
    让commandName=file.split(“.”[0];
    client.commands.set(commandName,props);
    });
    log(`Loaded${files.length}命令。`);
    });
    on('unhandledRejection',err=>console.log(err));
    client.login(config.token);
    

    作为一个处理程序,您需要向我展示整个代码,以了解您在处理命令时所做的操作,您可以像该基本代码的示例中那样使用
    enmap
    ,或者使用
    discord.js
    处理程序来执行命令,请查看
    message.js
    行:8
    client.commands.get()
    这里我们定义了enmap处理程序,在第13行为
    cmd.run()
    运行命令,只使用
    module.exports.run
    index.js
    第3行中定义了命令处理程序,并在
    fs
    for循环中设置第24行中的命令

    -享受编码


    您的问题不清楚,所以我将向您展示一个如何处理的示例,稍后我将展示代码和解释

    格式:

    • 文件夹
  • 文件

    • 事件
  • message.js
  • //我使用消息作为消息来缩短代码。
    const all_requires=require(“../settings/settings”);//导出所有需要的
    const{config,Discord,client}=all_requires;//我需要什么
    module.exports=(客户端,消息)=>{
    if(msg.author.bot | | |!msg.content.startsWith(config.prefix))返回;
    常量args=msg.content.split(“”).slice(1);
    const command=msg.content.split(“”)[0].slice(config.prefix.length);
    const cmd=client.commands.get(command);//enmap处理程序
    console.log(cmd);
    如果(!cmd)返回;
    cmd.run(msg,args);
    }
    
    • 命令
  • ping.js
  • module.exports.run=(msg)=>{
    msg.channel.send(“Pong!”);
    }
    
    • 配置
  • config.json
  • {
    “token”:“token-HERE”,
    “前缀”:“+”
    }
    
    • 背景
  • settings.js
  • const Discord=require(“Discord.js”);
    常数fs=要求(“fs”);
    常量enmap=需要(“enmap”);
    const config=require(“../config/config.json”);
    const client=new Discord.client();
    const all_requires={Discord,fs,enmap,config,client}
    module.exports=所有_要求;
    

    index.js

    const all_requires=require(“./settings/settings”);
    const{Discord,fs,enmap,config,client}=all_所需;
    client.commands=new enmap;
    client.once(“就绪”,()=>{
    log(`${client.user.tag}已连接。`);
    })
    fs.readdir(“./events/”,(错误,文件)=>{
    if(err)返回控制台。error(err);
    files.forEach(文件=>{