【问题标题】:User is undefined when changing discord bot presence更改不和谐机器人存在时用户未定义
【发布时间】:2021-06-27 13:31:42
【问题描述】:

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

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`)
        }
    },
};

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

【问题讨论】:

  • 我认为您应该使用主文件中的客户端。使用&lt;message&gt;.client 获取。

标签: node.js discord.js


【解决方案1】:

您不必在每个命令中重做初始客户端,因为它已经附加到您的消息中,您可以通过 message.client.user 轻松获取它

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

【讨论】:

  • 好的,但是现在bot状态不会改变,但是命令成功了。编辑:没关系,更新只需要一分钟。
猜你喜欢
  • 2017-04-26
  • 2021-08-08
  • 2021-09-04
  • 1970-01-01
  • 2021-03-10
  • 2021-06-19
  • 2018-07-10
  • 2018-02-04
  • 2017-09-13
相关资源
最近更新 更多