setActivity/setPresence not working (undefined)

My Code:
const Discord = require(‘discord.io’, ‘discord.js’);
var logger = require(‘winston’);
var auth = require(’./auth.json’);
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = ‘debug’;
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on(‘ready’, function (evt) {
logger.info(‘Connected’);
logger.info(‘Logged in as: ‘);
logger.info(bot.username + ’ - (’ + bot.id + ‘)’);
console.log(‘Launched Bot Successfully…’);
});
bot.on(‘ready’, () => { – I have tried all sorts of things here, and none work. This is my first bot too so please excuse the mess
bot.user.setPresence({ game: { name: ‘with discord.js’ }, status: ‘idle’ })
.then(console.log)
.catch(console.error);
})
bot.on(‘message’, function (user, userID, channelID, message, evt) {
if (message.substring(0, 1) == ‘!’) {
var args = message.substring(1).split(’ ');
var cmd = args[0];

    args = args.splice(1);
    switch(cmd) {
        case 'launchtest':
            bot.sendMessage({
                to: channelID,
                message: 'Launch successful!'
            });
        break;
     }
 }

});

Error I’m getting:
PS C:\Users\Lenovo E531\Desktop\DiscordTimeBot> node bot.js
{“message”:“Connected”,“level”:“info”}
{“message”:"Logged in as: ",“level”:“info”}
{“message”:“Current Time Bot - (592169374320951322)”,“level”:“info”}
Launched Bot Successfully… --This means its up, status and presence no however.
C:\Users\Lenovo E531\Desktop\DiscordTimeBot\bot.js:20
bot.user.setPresence({ game: { name: ‘with discord.js’ }, status: ‘idle’ })
^

TypeError: Cannot read property ‘setPresence’ of undefined
at DiscordClient.bot.on (C:\Users\Lenovo E531\Desktop\DiscordTimeBot\bot.js:20:14)
at DiscordClient.emit (events.js:203:15)
at Timeout.checkForAllServers [as _onTimeout] (C:\Users\Lenovo E531\node_modules\discord.io\lib\index.js:1729:66)
at ontimeout (timers.js:438:13)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)

This is offtopic. But in your code you define your bot token like this:

var auth = require('./auth.json');
var bot = new Discord.Client({
	token: auth.token,
	autorun: true
});

This is bad because your token is not secure there.

Try using enviroment variables.

Modify your .env file

TOKEN=your-bot-token-here

Then in your bot.js file

var bot = new Discord.Client({
	token: process.env.TOKEN,
	autorun: true
});

Then I tried your function of bot.on('ready', ...) with my bot (using discord.js) and it work.

I don’t know why you have discord.io and discord.js in the same variable. I’ve never saw that. Maybe you should choose one. I’m talking about your first line

Discord.io is not supported. Kindly switch over to discord.js

1 Like