Skip to content

Instantly share code, notes, and snippets.

@Restioson
Last active May 7, 2017 08:53
Show Gist options
  • Save Restioson/a674cccd7ad5b96265caae0fd522d9c3 to your computer and use it in GitHub Desktop.
Save Restioson/a674cccd7ad5b96265caae0fd522d9c3 to your computer and use it in GitHub Desktop.
//Constants
const Discord = require("discord.js");
const Fs = require("fs")
const client = new Discord.Client();
//Variables
var issues = new Array();
var users = new Array();
//Classes
function FixxitUser (user) {
this.user = user;
this.exp = 0;
this.issuesLogged = 0;
this.issuesClosed = 0;
this.isAdmin = false;
this.isBlacklisted = false;
}
//Functions
function saveBot() {
//Write issues array to file
Fs.writeFileSync("./issues.json", JSON.stringify(issues, null, 4))
//Write users array to file
Fs.writeFileSync("./users.json", JSON.stringify(users, null, 4))
}
//Commands
//Add new issue
function addIssue(message) {
//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.username + "#" + message.author.discriminator, message.author.id]);
}
}
//Lists Fixxit commands
function list(message) {
message.channel.send(
" \n"
+ "@fixxit [issue] - Logs issue [issue]\n"
+ "!fixxit - Displays this help page\n"
+ "!fixxit list - Lists issues\n"
+ "!fixxit remove [index] - Removes issue at index [index]\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"
);
}
function removeIssue(message) {
//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 - 1 <= issues.length) {
issueFixed = issues[message.content - 1];
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 {
console.log(message.content.length > 0)
console.log(!isNaN(message.content))
console.log(!/\+\-\./.test(message.content))
console.log(message.content)
message.reply("message must contain a list index!");
}
}
//Logs issue as fixed
function fixIssue(message) {
//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");
}
//Index out of range
else {
message.reply("list index is out of range");
}
}
//Invalid index
else {
message.reply("message must contain a list index!");
}
}
//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;
}
// Save bot
saveBot();
//Add new issue
if (message.cleanContent.includes("@fixxit"))
addIssue(message);
//Send list of commands
else if (message.content == "!fixxit")
list(message);
//Fix nth issue
else if (message.content.startsWith("!fixxit fix"))
fixIssue(message);
//Remove nth issue
else if (message.content.startsWith("!fixxit remove"))
removeIssue(message);
//Remove all issues
else if (message.content == "!fixxit removeall") {
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] +'")\n'
}
message.channel.send(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('MjYzNTkzNzI1MjYxNzc0ODQ4.C0UU2g.xKKG0S8Z0Hj8WQFHipLBdJv4m4U'); // This is an invalid token, btw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment