Dynamic Prefixes In Discord Bots Using Memjs and Heroku

Small Discord bots often hosted on platforms such as Heroku do not have the flexibility to change their command prefixes.

Command prefixes examples of Discord bots are !, ?, > etc. These command prefixes help the bots understand which bot command is being run.

In this tutorial, you will know how to integrate Heroku with MemCached Cloud using Memjs with a few lines of code.

Integrating With Heroku

The pre-requisite for this is you need to have a Heroku project with a hosted Discord bot in JavaScript. You can sign up here if you do not have one and host your bot there.

Add the Memcached Cloud add-on from here which will give 30MB of free storage.

Integrating With The Discord Bots

In the Discord bot project, install memjs using the following command:

npm i memjs

After installing this, in the message event file of the Discord bot, we will use the following code to load the memjs module:

var memjs = require('memjs');

After loading the module we will use the following code to connect to the Memcached Cloud:

var client = memjs.Client.create(process.env.MEMCACHEDCLOUD_SERVERS, {
  username: process.env.MEMCACHEDCLOUD_USERNAME,
  password: process.env.MEMCACHEDCLOUD_PASSWORD
});

The MEMCACHEDCLOUD_SERVERS, MEMCACHEDCLOUD_USERNAME, and MEMCACHEDCLOUD_PASSWORD are already in the Heroku environment variable when you install the add-on.

Now we will check for the stored prefix in the Memcached Cloud:

client.get(message.guild.id, function (err, value, key) {
    if (value != null) {
        let prefix = value.toString();
        let args = message.content.slice(prefix.length).trim().split(' ');
    }
    else{
        let prefix = '!';
        let args = message.content.slice(prefix.length).trim().split(' ');
    }
});

If no prefix is found in the Memcached Cloud it will default to the ‘!‘ prefix.

Setting The Prefix for the Discord Bots

A new command file for setting the prefix should be made.

To change the prefix of the bot:

var finalprefix = args.join('').trim();
var client = memjs.Client.create(process.env.MEMCACHEDCLOUD_SERVERS, {
  username: process.env.MEMCACHEDCLOUD_USERNAME,
  password: process.env.MEMCACHEDCLOUD_PASSWORD
});
client.set(message.guild.id, finalprefix, function (err, val) {
});
return message.reply(`\nYour prefix has been changed to: **${finalprefix}**`);

In this code, we are using the argument entered by the user and set the prefix of the bot in that guild. In short, we are setting the prefix of the bot with the guild ID where the message is sent.

Conclusion

We have to use Memcached Cloud to set the prefix of the Discord bot using the memjs module. At every message event detected by the bot it will check for the prefix and if the prefix matches the bot will execute the command.

In the command to set the prefix, the code will change/add the prefix for the bot in the guild.

 

You can also read,

How To Minimize Electron App To System Tray

Iterating over JavaScript Array in Different ways

Leave a Reply

Your email address will not be published. Required fields are marked *