Skip to content

Instantly share code, notes, and snippets.

@Restioson
Last active December 28, 2016 14:04
Show Gist options
  • Save Restioson/f8cc5f781142436eafb36929800c40ed to your computer and use it in GitHub Desktop.
Save Restioson/f8cc5f781142436eafb36929800c40ed to your computer and use it in GitHub Desktop.
A Discord bot for software development, support and discussion groups which logs issues
//Constants
const Discord = require("discord.js");
const client = new Discord.Client();
//Variables
var issues = new Array();
//Start event
client.on("ready", () => {
console.log("Ready to FIXXIT!"); //Log that bot is ready
client.user.setGame("!fixxit");
});
//Message event
try {
client.on('message', message => {
//Prevent bot from replying to other bots
if (message.author.bot) {
return;
}
//Add new issue
if (message.cleanContent.includes("@fixxit")) {
//Empty issue
if (message.cleanContent.replace("@fixxit ") == "") {
message.reply("issue cannot be empty");
}
//Add issue
else {
message.reply('thank you for reporting this issue');
issue = message.cleanContent.replace("@fixxit ", "")
issues.push([issue, message.author.toString(), message.author.id]);
}
}
//Send list of commands
else if (message.content == "!fixxit") {
message.channel.sendMessage("Commands: \n!fixxit list - Lists issues\n!fixxit remove [index] - Removes issue at index [index]\n@fixxit [issue] - Logs issue [issue]\n!fixxit removeall - Removes all issues from list\n!fixxit fix - (ADMIN ONLY) Logs issue as fixed. Adds XP to user who logged issue and admin who fixed the error");
}
//Remove nth issue
else if (message.content.startsWith("!fixxit fix")) {
//Remove command from message
message.content = message.content.replace("!fixxit fix ", "");
//Make sure the rest of the message exists, is a number, and doesn't have any of the following symbols: +-.
if (message.content.length > 0 && !isNaN(message.content) && !/\+\-\./.test(message.content)) {
//Make sure that the index is in the range of the issue list
if (message.content <= issues.length) {
issueFixed = issues[message.content - 1];
issues.splice(message.content - 1, message.content);
message.reply("issue #" + message.content + " was fixed");
client.fetchUser(issueFixed[2]).resolve().sendMessage("Your issue, (issue #" + message.content + ")" + '"' + issueFixed[0] + '", was removed');
}
//Index out of range
else {
message.reply("list index is out of range");
}
}
//Invalid index
else {
message.reply("Message must contain a list index!");
}
}
//Remove all issues
else if (message.content == "!fixxit removeall") {
for (index in issues) {
client.fetchUser(issues[index][2]).resolve().sendMessage("Your issue, " + '"' + issues[index][0] + '", was removed');
}
issues.splice(0)
message.reply("all issues were removed")
}
//List open issues
else if (message.content === "!fixxit list") {
//List open issues if any are open
if (issues.length > 0) {
message.reply("here are the list of current issues: ");
issuesString = ""
for (index in issues) {
issuesString += "Issue number " + (parseInt(index) + 1) + ': "' + issues[index][0] + ' (created by user "' + issues[index][1].replace("@","") +'")\n'
}
message.channel.sendMessage(issuesString)
}
//No issues are open
else {
message.reply("it seems that there are no issues. Hooray!")
}
}
//Remove nth issue
else if (message.content.startsWith("!fixxit remove")) {
//Remove command from message
message.content = message.content.replace("!fixxit remove ", "");
//Make sure the rest of the message exists, is a number, and doesn't have any of the following symbols: +-.
if (message.content.length > 0 && !isNaN(message.content) && !/\+\-\./.test(message.content)) {
//Make sure that the index is in the range of the issue list
if (message.content <= issues.length) {
issues.splice(message.content - 1, message.content);
message.reply("issue #" + message.content + " was removed");
}
//Index out of range
else {
message.reply("list index is out of range");
}
}
//Invalid index
else {
message.reply("Message must contain a list index!");
}
}
});
}
catch(error) {
//Destroy client
client.destroy();
//Re-throw error
throw(error);
}
//Login
client.login("your super-secret login key goes here")
@Restioson
Copy link
Author

Added some more commands in this revision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment