MJTbot Discord bot
A Node.js bot created for use in game jam events to help manage creative workshops (at MalmoJamsToo). In this post I will focus on three particular commands I created for the bot which were created and pieced together by myself.
The three commands are roll, jamlist and unjam.
The roll command
I was really happy with what I created with the roll command. The idea is to roll any amount of dice that also denote how many faces, and to present the rolls as concisely as possible. The code separates each of the values, understanding that there's an amount of dice to roll followed by how many sides they have (notated as <amount>d<sides>, so 2d6 would be two dice each with six sides). While there are available arguments, the calculations continue until there are none left (lines 41-45).
The roll code is as follows.
const Discord = require("discord.js");
module.exports = {
name: 'roll',
description: 'Rolls dice.',
usage: 'roll <amount>d<sides> ...',
execute(message, args) {
if (message.deletable) message.delete();
let show = [];
let answer = "";
let val = 0;
let result = "";
function rolldice() {
result = "";
if (args[val]) {
const x = parseInt(args[val].split('d').slice(0));
const y = parseInt(args[val].split('d').slice(1));
if (isNaN(x) || isNaN(y)) {
return;
}
let array = [];
for (let i = 0; i < x; i++) {
array.push(Math.floor(Math.random() * y)+1);
};
result = array[0];
for (let i = 1; i < array.length; i++) {
result = result +", "+ array[i];
};
result = x+"d"+y+": **"+result+"**";
};
show.push(result);
}
while (args[val+1]) {
rolldice();
val = val + 1;
}
rolldice();
answer = show[0];
for (let i = 1; i < show.length; i++) {
answer = answer +"\n"+ show[i];
};
if (answer == undefined) {
return;
};
//answer = "Roll results as follows:\n" + answer;
const embed = new Discord.MessageEmbed()
.setColor("#ff9900")
.setTitle("🎲 Roll results as follows 🎲")
.addField("Results", answer)
.setFooter(message.author.username, message.author.avatarURL())
.setTimestamp();
message.channel.send(embed);
}
}
The jamlist and unjam commands are much more practical. Using the library for DiscordJS, they help manage users on the Discord server for the events/workshops that we organize.
The jamlist command
jamlist downloads a full list of all server members into a text file with a timestamp. This is used for online check-in purposes. The list is loaded into a spreadsheet which checks which users have registered (and written down their Discord username) on our event registration forms.
The unjam command
unjam removes a specified role from all users on the server. Usually during our jams we assign our users roles that confirm their participation and that they've checked in. Users also may assign themselves a Looking-for-group role (which allows others to ping/at them) which should also be removed at the end of the event. As our events usually have over 30 users, it's a lot less work to run this command to completely remove multiple inactive roles.
I have decided not to show the source for the above commands, but I'm very willing to show them off in a code review. It does not really follow the most optimal variable naming conventions, but it's certainly also not the worst.
The event bot is currently not in use as we have suspended events temporarily, and it also requires an update to accommodate the slash command integration Discord announced and pushes for.