Fix some bugs

This commit is contained in:
Victor Tran 2017-08-07 12:13:16 +10:00
parent 142aa31f92
commit e5fdb31611
7 changed files with 289 additions and 35 deletions

View file

@ -4,7 +4,12 @@
"runTypes": [ "runTypes": [
{ {
"name": "Start", "name": "Start",
"program": "./bot.js", "program": "./bot.js --nowelcome",
"debug": true
},
{
"name": "Start with Debug messages",
"program": "./bot.js --nowelcome --debug",
"debug": true "debug": true
} }
] ]

249
bot.js
View file

@ -18,7 +18,7 @@
* *
* *************************************/ * *************************************/
const amVersion = "2.0.0"; const amVersion = "2.1.0";
const Discord = require('discord.js'); const Discord = require('discord.js');
const consts = require('./consts.js'); const consts = require('./consts.js');
@ -184,6 +184,10 @@ console.error = function(data, ...args){
}; };
global.log = function(logMessage, type = logType.debug) { global.log = function(logMessage, type = logType.debug) {
if (logMessage == null) {
return;
}
//Log a message to the console //Log a message to the console
if (type == logType.debug) { if (type == logType.debug) {
if (process.argv.indexOf("--debug") == -1) { if (process.argv.indexOf("--debug") == -1) {
@ -294,6 +298,17 @@ textBox.on("submit", function() {
textBox.setValue("> "); textBox.setValue("> ");
textBox.focus(); textBox.focus();
const memberLine = function(member) {
var line = member.id + " " + member.user.tag;
if (member.nickname != null) {
line += " [" + member.nickname + "]";
}
if (member.user.bot) {
line += " \x1b[46m[BOT]";
}
return line;
};
var lLine = line.toLowerCase(); var lLine = line.toLowerCase();
if (lLine == "help") { if (lLine == "help") {
var help = "AstralMod Console Commands:\n" + var help = "AstralMod Console Commands:\n" +
@ -305,7 +320,14 @@ textBox.on("submit", function() {
"broadcast [message] Broadcasts a message to every server AstralMod is connected to\n" + "broadcast [message] Broadcasts a message to every server AstralMod is connected to\n" +
"vacuum Check the AstralMod Configuration File for errors\n" + "vacuum Check the AstralMod Configuration File for errors\n" +
"reconnect Attempts to disconnect and reconnect to Discord\n" + "reconnect Attempts to disconnect and reconnect to Discord\n" +
"exit Exits AstralMod"; "guilds Lists guilds AstralMod knows about\n" +
"ginfo [guildid] - Shows information about a guild\n" +
"findc [channelid] Finds a channel by its ID\n" +
"exit Exits AstralMod\n\n" +
"For any commands denoted with a - modifiers on the end can show more information.\n" +
"m - Show members\n" +
"c - Show channels\n\n" +
"Here's an example: ginfom 123456789012345678";
log(help, logType.info); log(help, logType.info);
} else if (lLine == "exit") { } else if (lLine == "exit") {
shutdown(); shutdown();
@ -380,11 +402,130 @@ textBox.on("submit", function() {
log("Usage: broadcast message", logType.critical); log("Usage: broadcast message", logType.critical);
} else if (lLine == "vacuum") { } else if (lLine == "vacuum") {
vacuumSettings(); vacuumSettings();
} else if (lLine == "throw") {
throw new Error();
} else if (lLine.startsWith("findc ")) {
var channelId = line.substr(6);
var channel = client.channels.get(channelId);
if (channel == null) {
log("Unknown channel.", logType.info);
} else {
log("That channel belongs to " + channel.guild.name + ".", logType.info);
}
} else if (lLine == "findc") {
log("Usage: findc [channelid]", logType.critical);
} else if (lLine == "guilds") {
var response = "Guilds AstralMod is connected to:";
for ([id, guild] of client.guilds) {
response += "\n" + guild.id + " " + guild.name + "";
}
log(response, logType.info);
} else if (lLine.startsWith("ginfo ")) {
var guildLine = line.substr(6);
var guild = client.guilds.get(guildLine);
if (guild == null) {
log("Unknown guild.", logType.info);
} else {
var info = "Information for guild " + guildLine + ":\n" +
"Name: " + guild.name + "\n" +
"Owner: " + memberLine(guild.owner) + "\n" +
"Members: " + parseInt(guild.memberCount) + "\n" +
"Channels: " + parseInt(guild.channels.size);
log(info, logType.info);
}
} else if (lLine.startsWith("ginfom ")) {
var guildLine = line.substr(7);
var guild = client.guilds.get(guildLine);
if (guild == null) {
log("Unknown guild.", logType.info);
} else {
var info = "Information for guild " + guildLine + ":\n" +
"Members: " + parseInt(guild.memberCount);
for ([id, member] of guild.members) {
info += "\n" + memberLine(member);
}
log(info, logType.info);
}
} else if (lLine == "ginfo") {
log("Usage: ginfo [guildid]", logType.critical);
} else { } else {
log("Unknown command. For help, type \"help\" into the console.", logType.critical); log("Unknown command. For help, type \"help\" into the console.", logType.critical);
} }
}); });
textBox.key('backspace', function() {
var line = textBox.getText();
if (!line.startsWith("> ")) {
if (line == ">") {
line = "> ";
} else if (line.startsWith(" ")) {
line = "> " + line.substring(1);
} else {
line = "> " + line;
}
textBox.setValue(line);
}
});
textBox.key('tab', function() {
//Autocomplete!
var line = textBox.getText().substr(2, textBox.getText().length - 6);
textBox.setValue("> " + line);
var lLine = line.toLowerCase();
if (lLine.startsWith("ginfo ")) {
var guildLine = line.substr(6);
var guilds = [];
for ([id, guild] of client.guilds) {
var id = guild.id;
if (id.startsWith(guildLine)) {
guilds.push(guild.id);
}
}
if (guilds.length == 1) {
textBox.setValue("> ginfo " + guilds[0]);
} else if (guilds.length == 0) {
log("No results.", logType.info)
} else {
var acOutput = "";
for (guild of guilds) {
acOutput += guild + " ";
}
log(acOutput, logType.info);
}
} else if (lLine.startsWith("ginfom ") || lLine.startsWith("ginfoc ")) {
var guildLine = line.substr(7);
var guilds = [];
for ([id, guild] of client.guilds) {
var id = guild.id;
if (id.startsWith(guildLine)) {
guilds.push(guild.id);
}
}
if (guilds.length == 1) {
textBox.setValue("> ginfom " + guilds[0]);
} else if (guilds.length == 0) {
log("No results.", logType.info)
} else {
var acOutput = "";
for (guild of guilds) {
acOutput += guild + " ";
}
log(acOutput, logType.info);
}
} else {
log("Command autocompletion coming soon.", logType.info);
//TODO: Command autocompletion
}
});
function shutdown() { function shutdown() {
if (global.settings != null) { if (global.settings != null) {
log("Saving settings..."); log("Saving settings...");
@ -405,7 +546,7 @@ process.on('SIGTERM', shutdown);
log("Welcome to AstralMod!", logType.good); log("Welcome to AstralMod!", logType.good);
function getUserString(user) { global.getUserString = function(user) {
var u = user; var u = user;
if (user.user != null) { if (user.user != null) {
u = user.user; u = user.user;
@ -649,7 +790,12 @@ function processModCommand(message) {
} }
if (isMod(message.member)) { if (isMod(message.member)) {
var command = text.toLowerCase().substr(4); var command;
if (text.startsWith("mod:")) {
command = text.toLowerCase().substr(4);
} else {
command = text.toLowerCase().substr(3);
}
if (command.startsWith("uinfo ")) { if (command.startsWith("uinfo ")) {
var user = command.substr(6); var user = command.substr(6);
user = user.replace("<", "").replace(">", "").replace("@", "").replace("!", ""); user = user.replace("<", "").replace(">", "").replace("@", "").replace("!", "");
@ -691,6 +837,7 @@ function processModCommand(message) {
message.channel.send(':no_entry_sign: ERROR: That didn\'t work. Has 5 minutes passed?'); message.channel.send(':no_entry_sign: ERROR: That didn\'t work. Has 5 minutes passed?');
} }
} }
return true;
} else if (command.startsWith("deal ") || command.startsWith("manage ")) { } else if (command.startsWith("deal ") || command.startsWith("manage ")) {
if (actioningMember[message.guild.id] != null) { if (actioningMember[message.guild.id] != null) {
message.channel.send(':no_entry_sign: ERROR: ' + getUserString(actioningMember[message.guild.id]) + " is already managing another user."); message.channel.send(':no_entry_sign: ERROR: ' + getUserString(actioningMember[message.guild.id]) + " is already managing another user.");
@ -762,10 +909,43 @@ function processModCommand(message) {
}); });
} }
message.delete(); message.delete();
}
} else {
message.channel.send("You ain't a mod...");
return true; return true;
} else if (command == "find") {
message.reply("Usage: `mod:find user`. For more information, `mod:help find`");
return true;
} else if (command.startsWith("find" )) {
var query = command.substr(5);
var searchResults = [];
for (let [snowflake, user] of client.users) {
if (user.username.toLowerCase().indexOf(query.toLowerCase()) != -1) {
searchResults.push({
tag: user.tag,
id: user.id
});
}
}
if (searchResults.length == 0) {
message.reply("No results.");
} else {
var reply = "Here's who I found.```";
var currentNumber = 0;
for (user of searchResults) {
reply += user.tag + ": " + user.id + "\n";
if (currentNumber == 10) {
reply += "\n----- " + parseInt(searchResults.length - currentNumber) + " more. -----\n";
reply += "Please narrow your query.";
break;
}
currentNumber++;
}
reply += "```";
message.channel.send(reply);
}
return true;
}
} }
return false; return false;
} }
@ -847,7 +1027,7 @@ function processAmCommand(message) {
embed.setAuthor("AstralMod Help Contents"); embed.setAuthor("AstralMod Help Contents");
embed.setDescription("Here are some things you can try. For more information, just `am:help [command]`"); embed.setDescription("Here are some things you can try. For more information, just `am:help [command]`");
embed.addField("AstralMod Core Commands", "**config**\n**shoo**\n**declnick**\n**deal**\nping\nuinfo\nnick\nversion\nhelp", true); embed.addField("AstralMod Core Commands", "**config**\n**shoo**\n**declnick**\n**deal**\n**find**\nping\nuinfo\nnick\nversion\nhelp", true);
for (key in plugins) { for (key in plugins) {
var plugin = plugins[key]; var plugin = plugins[key];
@ -890,6 +1070,7 @@ function processAmCommand(message) {
embed.setFooter("AstralMod " + amVersion + ". mod: commands denoted with bold text."); embed.setFooter("AstralMod " + amVersion + ". mod: commands denoted with bold text.");
message.channel.send("", { embed: embed }); message.channel.send("", { embed: embed });
return true;
} else if (command.startsWith("help ")) { //Contextual help } else if (command.startsWith("help ")) { //Contextual help
//Get help for specific command //Get help for specific command
var embed = new Discord.RichEmbed(); var embed = new Discord.RichEmbed();
@ -944,6 +1125,13 @@ function processAmCommand(message) {
help.helpText = "Sets your nickname after staff have a chance to review it"; help.helpText = "Sets your nickname after staff have a chance to review it";
help.param1 = "The nickname you wish to be known as"; help.param1 = "The nickname you wish to be known as";
break; break;
case "find":
help.title = "mod:find";
help.usageText = "mod:find user";
help.helpText = "Finds a user and returns their ID";
help.param1 = "The user you want to find.";
help.remarks = "AstralMod will search for users from all connected servers."
break;
case "help": case "help":
help.title = "am:help"; help.title = "am:help";
help.usageText = "am:help [command]"; help.usageText = "am:help [command]";
@ -1028,8 +1216,9 @@ function processAmCommand(message) {
embed.addField("Remarks", help.remarks); embed.addField("Remarks", help.remarks);
} }
} }
embed.setFooter("AstralMod " + amVersion + "."); embed.setFooter("AstralMod " + amVersion);
message.channel.send("", { embed: embed }); message.channel.send("", { embed: embed });
return true;
} }
} }
return false; return false;
@ -1296,25 +1485,25 @@ function getSingleConfigureWelcomeText(guild) {
string += "1 Staff Roles " + guildSetting.modRoles.length + " roles\n"; string += "1 Staff Roles " + guildSetting.modRoles.length + " roles\n";
if (guildSetting.memberAlerts == null) { if (guild.channels.get(guildSetting.memberAlerts) == null) {
string += "2 Member Alerts Disabled\n"; string += "2 Member Alerts Disabled\n";
} else { } else {
string += "2 Member Alerts #" + guild.channels.get(guildSetting.memberAlerts).name + "\n"; string += "2 Member Alerts #" + guild.channels.get(guildSetting.memberAlerts).name + "\n";
} }
if (guildSetting.chatLogs == null) { if (guild.channels.get(guildSetting.chatLogs) == null) {
string += "3 Chat Logs Disabled\n"; string += "3 Chat Logs Disabled\n";
} else { } else {
string += "3 Chat Logs #" + guild.channels.get(guildSetting.chatLogs).name + "\n"; string += "3 Chat Logs #" + guild.channels.get(guildSetting.chatLogs).name + "\n";
} }
if (guildSetting.botWarnings == null) { if (guild.channels.get(guildSetting.botWarnings) == null) {
string += "4 Bot Warnings Disabled\n"; string += "4 Bot Warnings Disabled\n";
} else { } else {
string += "4 Bot Warnings #" + guild.channels.get(guildSetting.botWarnings).name + "\n"; string += "4 Bot Warnings #" + guild.channels.get(guildSetting.botWarnings).name + "\n";
} }
if (guildSetting.suggestions == null) { if (guild.channels.get(guildSetting.suggestions) == null) {
string += "5 Suggestions Disabled\n"; string += "5 Suggestions Disabled\n";
} else { } else {
string += "5 Suggestions #" + guild.channels.get(guildSetting.suggestions).name + "\n"; string += "5 Suggestions #" + guild.channels.get(guildSetting.suggestions).name + "\n";
@ -1757,11 +1946,15 @@ function processMessage(message) {
} }
//Determine if this is a command //Determine if this is a command
if (text.startsWith("mod:")) { //This is a mod command if (text.startsWith("mod:") || text.startsWith("am")) { //This is a mod command
if (!processModCommand(message)) { if (!processModCommand(message)) {
if (!processAmCommand(message)) { if (!processAmCommand(message)) {
//Pass command onto plugins //Pass command onto plugins
if (text.startsWith("mod:")) {
commandEmitter.emit('processCommand', message, true, text.substr(4).toLowerCase()); commandEmitter.emit('processCommand', message, true, text.substr(4).toLowerCase());
} else {
commandEmitter.emit('processCommand', message, true, text.substr(3).toLowerCase());
}
} }
} }
} else if (text.startsWith("am:")) { } else if (text.startsWith("am:")) {
@ -1963,6 +2156,27 @@ function memberRemove(member) {
} }
} }
function userUpdate(oldUser, newUser) {
if (newUser.username != oldUser.username) {
for (key in settings.guilds) {
var bwChannel = settings.guilds[key].botWarnings;
var guild = client.guilds.get(key);
if (bwChannel != null) {
//Check if member exists
for ([id, member] of guild.members) {
if (member.user.id == newUser.id) {
var channel = client.channels.get(bwChannel); //282513354118004747
if (channel != null) {
channel.send(":ab: " + getUserString(oldUser) + " :arrow_right: " + newUser.username + ".");
}
}
}
}
}
}
}
function loadPlugin(file) { function loadPlugin(file) {
try { try {
if (plugins[file] != null) { if (plugins[file] != null) {
@ -2004,6 +2218,7 @@ function unloadPlugin(file) {
//var module = require.cache[moduleResolve]; //var module = require.cache[moduleResolve];
delete require.cache[moduleResolve]; delete require.cache[moduleResolve];
delete plugins[file]; delete plugins[file];
return true;
} catch (err) { } catch (err) {
log(err.message, logType.critical); log(err.message, logType.critical);
log("Plugin " + file + " is cannot be unloaded.", logType.critical); log("Plugin " + file + " is cannot be unloaded.", logType.critical);
@ -2095,6 +2310,7 @@ function guildMemberUpdate(oldUser, newUser) {
if (guildSetting.botWarnings != null) { if (guildSetting.botWarnings != null) {
if (oldUser.guild != null) { if (oldUser.guild != null) {
channel = oldUser.guild.channels.get(guildSetting.botWarnings); channel = oldUser.guild.channels.get(guildSetting.botWarnings);
if (channel != null) {
if (newUser.nickname == null) { if (newUser.nickname == null) {
channel.send(":abcd: " + getUserString(oldUser) + " :arrow_right: [cleared]"); channel.send(":abcd: " + getUserString(oldUser) + " :arrow_right: [cleared]");
} else { } else {
@ -2103,6 +2319,7 @@ function guildMemberUpdate(oldUser, newUser) {
} }
} }
} }
}
} }
function readyAgain() { function readyAgain() {
@ -2189,6 +2406,7 @@ function readyOnce() {
client.on('guildMemberRemove', memberRemove); client.on('guildMemberRemove', memberRemove);
client.on('guildUnavailable', guildUnavailable); client.on('guildUnavailable', guildUnavailable);
client.on('guildMemberUpdate', guildMemberUpdate); client.on('guildMemberUpdate', guildMemberUpdate);
client.on('userUpdate', userUpdate);
client.on('ready', readyAgain); client.on('ready', readyAgain);
setTimeout(saveSettings, 30000); setTimeout(saveSettings, 30000);
@ -2196,7 +2414,8 @@ function readyOnce() {
log("AstralMod " + amVersion + " - locked and loaded!", logType.good); log("AstralMod " + amVersion + " - locked and loaded!", logType.good);
setInterval(function() { setInterval(function() {
titleBox.content = "AstralMod " + amVersion + " Console Uptime: " + moment.duration(client.uptime).humanize(); titleBox.content = "AstralMod " + amVersion + " Console │ Uptime: " + moment.duration(client.uptime).humanize() +
" │ Guilds: " + parseInt(client.guilds.size);
renderScreen(); renderScreen();
}, 1000); }, 1000);
} }

View file

@ -30,13 +30,17 @@ var connection;
function playAudio() { function playAudio() {
try { try {
if (connection.status == 0) {
dispatcher = connection.playFile("forecastvoice.mp3"); dispatcher = connection.playFile("forecastvoice.mp3");
dispatcher.on('end', playAudio); dispatcher.on('end', playAudio);
dispatcher.on('error', function(err) { dispatcher.on('error', function(err) {
dispatcher.end(); connection.disconnect();
log("APHC Voice connection encountered an error: " + err, logType.critical); log("APHC Voice connection encountered an error: " + err, logType.critical);
log("Disconnected from the waiting room.", logType.critical); log("Disconnected from the waiting room.", logType.critical);
}); });
} else {
log("connection.status " + parseInt(connection.status));
}
} catch (err) { } catch (err) {
log("Disconnected from the waiting room.", logType.critical); log("Disconnected from the waiting room.", logType.critical);
} }
@ -53,6 +57,7 @@ function startup() {
connection = conn; connection = conn;
connection.on('disconnect', function() { connection.on('disconnect', function() {
log("Disconnected from the waiting room.", logType.critical); log("Disconnected from the waiting room.", logType.critical);
//connection.disconnect();
}); });
connection.on('reconnecting', function() { connection.on('reconnecting', function() {
log("Attempting to reconnect to the waiting room.", logType.warning); log("Attempting to reconnect to the waiting room.", logType.warning);
@ -76,9 +81,13 @@ function startup() {
} }
}); });
connection.on('error', function(warning) { connection.on('error', function(warning) {
if (typeof warning == "string") {
log(warning, logType.warning);
} else {
if (warning.message) { if (warning.message) {
log(warning.message, logType.critical); log(warning.message, logType.critical);
} }
}
}); });
playAudio(); playAudio();
}); });
@ -108,7 +117,7 @@ function processCommand(message, isMod, command) {
for (var i = 0; i < membersInWaitingRoom.length; i++) { for (var i = 0; i < membersInWaitingRoom.length; i++) {
var member = membersInWaitingRoom[i]; var member = membersInWaitingRoom[i];
if (member.selfMute || member.serverMute || member.id == 282048599574052864 || isMod(member)) { if (member.selfMute || member.serverMute || member.id == 282048599574052864) {// || isMod(member)) {
membersInWaitingRoom.splice(i, 1); membersInWaitingRoom.splice(i, 1);
i--; i--;
} }
@ -197,6 +206,9 @@ module.exports = {
commandEmitter.on('reconnect', startup); commandEmitter.on('reconnect', startup);
}, },
destructor: function(commandEmitter) { destructor: function(commandEmitter) {
dispatcher.end("Disconnection");
connection.disconnect();
commandEmitter.removeListener('startup', startup); commandEmitter.removeListener('startup', startup);
commandEmitter.removeListener('processCommand', processCommand); commandEmitter.removeListener('processCommand', processCommand);
commandEmitter.removeListener('disconnect', disconnected); commandEmitter.removeListener('disconnect', disconnected);

View file

@ -58,6 +58,10 @@ function processCommand(message, isMod, command) {
} }
} else if (command == "panic") { } else if (command == "panic") {
message.channel.send('Panic Mode is coming soon. Stay Tuned!'); message.channel.send('Panic Mode is coming soon. Stay Tuned!');
} else if (command == "chnk") {
message.channel.send("Usage: mod:chnk user. For more information, `mod:help chnk`.");
} else if (command.startsWith("chnk ")) {
} }
} }
} }
@ -80,7 +84,8 @@ module.exports = {
], ],
modCommands: [ modCommands: [
"rm", "rm",
"panic" "panic",
"chnk"
] ]
} }
}, },
@ -99,6 +104,13 @@ module.exports = {
help.usageText = "mod:panic"; help.usageText = "mod:panic";
help.helpText = "Switches on Panic Mode. In this mode, no one can send messages."; help.helpText = "Switches on Panic Mode. In this mode, no one can send messages.";
break; break;
case "chnk":
help.title = "mod:chnk";
help.usageText = "mod:chnk user";
help.helpText = "Sets a random nickname to user.";
help.param1 = "- The User ID of the user to apply a new nickname to\n" +
"- Mention of the user to apply a new nickname to";
break;
} }
return help; return help;

View file

@ -47,7 +47,13 @@ function processCommand(message, isMod, command) {
} else if (command == "pingtime") { } else if (command == "pingtime") {
pingDate = Date.now(); pingDate = Date.now();
message.channel.send("Ping!").then(function(message) { message.channel.send("Ping!").then(function(message) {
message.edit(parseInt(Date.now() - pingDate) + "ms"); var time = Date.now() - pingDate;
if (time == 420) {
message.edit("Either 419ms or 421ms");
} else {
message.edit(parseInt(time) + "ms");
}
}).catch(function() { }).catch(function() {
}); });

View file

@ -76,7 +76,7 @@ function newMessage(message) {
} }
var messageText = message.content.toLowerCase(); var messageText = message.content.toLowerCase();
if (messageText.length > 3) { if (messageText.length > 3 && messageText != "okay" && messageText != "yeah") {
if (lastMessagesOfUser.includes(messageText)) { if (lastMessagesOfUser.includes(messageText)) {
spamCountingUser++; spamCountingUser++;

View file

@ -238,7 +238,7 @@ function processCommand(message, isMod, command) {
utcOffset = parseFloat(location); utcOffset = parseFloat(location);
if (isNaN(utcOffset) || utcOffset > 14 || utcOffset < -14) { if (isNaN(utcOffset) || utcOffset > 14 || utcOffset < -14) {
utcOffset = utcOffsetFromTimezone(location); utcOffset = utcOffsetFromTimezone(location);
if (utcOffset == null) { if (utcOffset == -3000) {
var user = location.replace("<", "").replace(">", "").replace("@", "").replace("!", ""); var user = location.replace("<", "").replace(">", "").replace("@", "").replace("!", "");
var userSettings = settings.users[user]; var userSettings = settings.users[user];