Recommand · November 24, 2021 0

Discord.io bot stops being able to change server name after reconnecting

I’ve got a discord server that uses a bot using the discord.io library for Node.js. The bot has been running for over a year with no real problems but recently it’s been breaking and I’m having a hard time figuring out why.

One of the things the bot does is periodically change the discord server name. This works fine for a bit, but every couple hours Discord will disconnect the bot and the bot will automatically reconnect, and after this happens the attempts by the bot to rename the server will fail. No error messages occur, and the bot continues to log messages as if it’s renaming the server. Other bot functions still work normally.

The only way I’ve found to fix this issue is to manually go into server settings and change the server name while the bot is running. After doing this, the bot will successfully be able to rename the server until the next disconnect, at which point it will break again.

Simply restarting the bot does not fix the issue, and changing the server name manually while the bot is not running also does not fix the issue (i.e. bot breaks -> force quit bot -> manually rename server -> restart bot – doesn’t fix it).

Here’s some of the relevant parts of the code:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');

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

function renameServer(){
    var myName = generateName();
    bot.editServer({serverID: myserverID, name: myName});
    logString('Server name changed to: ' + myName);
    
    currentName = myName;
}

bot.on('ready', function (evt) {
    logger.info('Connected');
    if (!intervalSet){
        renameInterval = setInterval(renameServer, renameTimer);
        intervalSet = true;
    }
    logger.info('renameTimer set to default of ' + renameTimer + ' milliseconds');
});

bot.on('disconnect', function (errMsg, code) {
    logger.info('Disconnected, attempting to reconnect...');
    bot.connect();
    logger.info(errMsg); //errMsg = "Reconnect requested by Discord"
});

Again, this code was running without issue for a year and only recently started breaking recently. Not really sure how to begin troubleshooting this, and experimentation can be awkward when I have to wait multiple hours for a disconnect each time. Any ideas on what could be causing this?