已解决:DM ID的特定用户

时间:2020-06-06 16:57:29

标签: javascript discord.js

我遇到了其他问题,试图找到答案,并找到了以下代码:

let Prayer = message.member.user.tag;
bot.users.cache.get("my_id").send("My goddess, " + Prayer + " has sent you a prayer!");

但是,出现此错误:

TypeError: Cannot read property 'send' of undefined

我不知道为什么这么说,因为它也适用于其他人。有人可以帮我吗?

编辑:

现在有了新代码

const Discord = require("discord.js");
const bot = new Discord.Client();
let Prayer = message.member.user.tag;
async function SendingPray() {
      message.reply("Test3");
      const Yuuki = await bot.users.fetch('715912580127785060');
      message.reply("Test4");
      console.log(Yuuki);
      message.reply("Test5");
      Yuuki.send("Testing");
      message.reply("Test6");
};
SendingPray();
message.reply("your pray has been sent. The goddess will read your pray and decide your fate!");

它到达了Test3,但之后Test 4将不会出现,Yuuki也不会被记录。

结果: 特别感谢@Karizma与我一起解决了这个问题!

在index.js的结尾必须说

module.exports = bot;

pray.js(使用命令处理程序)具有以下代码:

module.exports = {
    name: 'pray',
    description: 'Pray to the goddess!',
    usage: '[command name]',
    execute(message, args){
        const Discord = require("discord.js");
        const { Client } = require('discord.js');
        const bot = require("path_to_index.js")
        const token = require("path_to_your_token");
        bot.login(token.login_token);
        //token_login is how I stored my token to login the bot
        let Prayer = message.member.user.tag;
            async function SendingPray() {
                const Yuuki = await bot.users.fetch('ID_wanted').catch(err => {
                    console.error(err);
                    //means the user id is invalid
                });
                Yuuki.send("My goddess, " + Prayer + " has sent you a pray!").catch(console.error);
            };
            SendingPray();
        message.reply("your pray has been sent. The goddess will read your pray and decide your fate!");
    }
}

问题是无法通过在7-9中添加代码并直接返回index.js来通过新文件重新定义机器人,而无法获取ID。

1 个答案:

答案 0 :(得分:1)

由于要获取用户,因此必须在方法上调用.catch,以防失败

async function SendingPray() {
      message.reply("Test3");
      const Yuuki = await bot.users.fetch('715912580127785060').catch(err => {
          console.error(err);
          //means the user id is invalid
      });
      message.reply("Test4");
      console.log(Yuuki);
      message.reply("Test5");
      Yuuki.send("Testing");
      message.reply("Test6");
};

我已经测试了不和谐的ID,看来它是无效的。