johng3587
johng3587

Reputation: 113

User is undefined when changing discord bot presence

Im trying to change my bot's presence using the following code:

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

But when executing this, I get the error 'user is not defined.' Any ideas?

Upvotes: 1

Views: 238

Answers (1)

HellCatVN
HellCatVN

Reputation: 876

You don't have to redo initial client at each commands since it already attach to your message and you can get it easily with message.client.user

So this line const client = new Discord.Client(); is useless since your bot is initial but it even not login to get the details yet

Upvotes: 2

Related Questions