繁体   English   中英

类型错误:无法读取未定义的属性“发送”?

[英]TypeError: Cannot read property 'send' of undefined?

我正在尝试让我的 discord javascript discord 机器人使用 id 向我发送 dm,但它一直在说:

类型错误:无法读取未定义的属性“发送”。

任何的想法?

const Discord = require('discord.js');
const{prefix,token} = require('./config.json');
const bot = new Discord.Client();

bot.once('ready', () => {
  console.log(`Ready!`);
});

bot.on('message', message => {
  let e = message.content.split(" ");

  if(message.content.startsWith(`${prefix}help`))
  {
    if (message.channel.type == "dm")
    {
      message.author.send("use !send");

    }

  } 

  if(message.content.startsWith(`${prefix}send`))
  {
    if (message.channel.type == "dm")
    {
      try{
      message.author.send("Message sent");
      message.client.users.get("266928832726xxxxxxx").send("someMessage");
    }catch(e){console.log("[ERROR]",e)}
    }

  } 

})

bot.login(token); 

好的,为了知道send的问题是什么,您必须做的是,首先是console.log(message)并检查您是否获得了名为author的值,然后检查author是否具有名为send的值或方法。

你将你的客户端设置为 bot const bot = new Discord.Client();

并尝试将成员获取为message.client.users.get("266928832726xxxxxxx")消息没有属性client.users所以使用bot.users.get()

bot.on('message', message => {
  let e = message.content.split(" ");

  if(message.content.startsWith(`${prefix}help`)){
    if (message.channel.type == "dm"){
      message.author.send("use !send");
    }
  }

  if(message.content.startsWith(`${prefix}send`)){
    if (message.channel.type == "dm"){
      message.author.send("Message sent");
      bot.users.get("266928832726xxxxxxx").send("someMessage").catch(console.error);
    }
  } 
})

暂无
暂无

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

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