How do I make a command that makes the status change in discord.js?

Viewed 506

I'm trying to make a command for my bot in discord.js. What it's supposed to do it, whenever the admin of the server uses it, It'll change the status. I tried making it, but it doesn't work. Here's my code:

client.on('message', message => {
  if (message.content.startsWith('!status')) {
   const prefix = '!';
    const args = message.content.slice(prefix.length).trim().split(' ');
    if (member.hasPermission('ADMINISTRATOR')) {
    const activity = args.splice(2);
    client.user.setActivity(activity);
    }
    else {
      message.channel.send('You\'re not the admin of the server or bot!')
    }
  }
});
2 Answers

The first problem is member is not defined, if you want ti to be the message author, you can use message.member instead.

The second problem is activity is an array, it needs to be a string or an ActivityOptions object. You either want to use the first element of args as the status, or if you want to accept more than one words, join them by space instead.

Check the code below, I've added a few comments too:

const prefix = '!';

client.on('message', (message) => {
  // if the message doesn't start with the prefix or the author is a bot, exit early
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  // create an args variable that slices off the prefix and splits it into an array
  const args = message.content.slice(prefix.length).split(/ +/);
  // create a command variable by taking the first element in the array
  // and removing it from args
  const command = args.shift().toLowerCase();

  if (command === 'status') {
    // if the message author has no permission, send a message and exit
    if (!message.member.hasPermission('ADMINISTRATOR'))
      return message.channel.send('Your not the Admin of the server or bot!');

    // if there are more than one words as the status, join them again
    const activity = args.join(' ');
    client.user.setActivity(activity);
  }
});

There are two things you had to fix up.

First, like what Lioness100 said, the setActivity method expects a string for an argument, not an array. If you wanted to simply remove the command, then you can simply do something like const activity = message.content.slice(7);

Second, you cannot simply grab the member since the program will have no idea which member you're expecting. You'll have to grab the member through message. Use message.member instead.


Code:

client.on('message', message => {
    if (message.content.startsWith('!status')) {
        const prefix = '!';
        const args = message.content.slice(prefix.length).trim().split(' ');
        if (message.member.hasPermission('ADMINISTRATOR')) {
            //args[0] is the command itself
            const activity = args[1];
            client.user.setActivity(activity);
        }
        else {
            message.channel.send('Your not the Admin of the server or bot!')
        }
    }
});
Related