更改不和谐机器人存在时用户未定义

时间:2021-06-27 03:36:26

标签: node.js discord.js

我正在尝试使用以下代码更改我的机器人的存在:

const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
    name: 'setstatus', //command name
    description: "Sets the bot's presence", //command description
    args: true, //needs arguments? delete line if no
    usage: `<online|invisible|dnd|idle>`, //usage instructions w/o command name and prefix
    cooldown: 5, //cooldown in seconds, defaults to 3
    ownerOnly: true, //need to be the owner? delete line if no
    aliases: [],
    execute(message, args, prefix, user) { //inside here command stuff
        if (args[0] === 'online') {
            client.user.setPresence({ status: 'online' })
            message.channel.send(`Status set to ${args[0]}`)
        } else if (args[0] === 'idle') {
            client.user.setPresence({ status: 'idle' })
            message.channel.send(`Status set to ${args[0]}`)
        } else if (args[0] === 'invisible') {
            client.user.setPresence({ status: 'invisible' })
            message.channel.send(`Status set to ${args[0]}`)
        } else if (args[0] === 'dnd') {
            client.user.setPresence({ status: 'dnd' })
            message.channel.send(`Status set to ${args[0]}`)
        } else {
            message.channel.send(`Invalid argument: ${args[0]}. Valid arguments are:\nonline, idle, invisible, dnd`)
        }
    },
};

但是在执行此操作时,我收到错误“用户未定义”。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您不必在每个命令上重做初始客户端,因为它已经附加到您的消息中,您可以使用 message.client.user

轻松获取它

所以这行 const client = new Discord.Client(); 没有用,因为您的机器人是初始的,但它甚至还没有登录以获取详细信息