Created librarian as a simple bot

This commit is contained in:
2018-10-27 09:25:19 +11:00
parent 35c0b0cede
commit 5393f51fb4
8 changed files with 135 additions and 146 deletions

View File

@@ -1,8 +1,8 @@
require('dotenv').config({path: '../.env'}); require('dotenv').config({path: '../.env'});
module.exports = { module.exports = {
activity: "Automated Data Analysis Machine.", activity: "Automated Data Analysis Machine.",
type: "PLAYING", type: "PLAYING",
token: process.env.ADAM_TOKEN, token: process.env.ADAM_TOKEN,
botChannel: "default" botChannel: "default"
} }

View File

@@ -1,8 +1,8 @@
require('dotenv').config({path: '../.env'}); require('dotenv').config({path: '../.env'});
module.exports = { module.exports = {
activity: "for !genesis recruits.", activity: "for !genesis recruits.",
type: "WATCHING", type: "WATCHING",
token: process.env.MONTGOMERY_GENESIS_TOKEN, token: process.env.MONTGOMERY_GENESIS_TOKEN,
botChannel: process.env.GROUP_B_BOT_ID botChannel: process.env.GROUP_B_BOT_ID
} }

View File

@@ -1,8 +1,8 @@
require('dotenv').config({path: '../.env'}); require('dotenv').config({path: '../.env'});
module.exports = { module.exports = {
activity: "for !hand recruits.", activity: "for !hand recruits.",
type: "WATCHING", type: "WATCHING",
token: process.env.DAIRO_HAND_TOKEN, token: process.env.DAIRO_HAND_TOKEN,
botChannel: process.env.GROUP_C_BOT_ID botChannel: process.env.GROUP_C_BOT_ID
} }

View File

@@ -1,8 +1,8 @@
require('dotenv').config({path: '../.env'}); require('dotenv').config({path: '../.env'});
module.exports = { module.exports = {
activity: "for !obsidian recruits.", activity: "for !obsidian recruits.",
type: "WATCHING", type: "WATCHING",
token: process.env.KAMALA_OBSIDIAN_TOKEN, token: process.env.KAMALA_OBSIDIAN_TOKEN,
botChannel: process.env.GROUP_A_BOT_ID botChannel: process.env.GROUP_A_BOT_ID
} }

6
Librarian/dialog.json Normal file
View File

@@ -0,0 +1,6 @@
{
"help": "PLEASE CHOOSE A SUBJECT: help [subject]",
"lore": "PLEASE CHOOSE A SUBJECT: lore [subject]",
"unknown": "UNKNOWN"
}

View File

@@ -2,131 +2,116 @@
require('dotenv').config({path: '../.env'}); require('dotenv').config({path: '../.env'});
// Node Modules // Node Modules
const Discord = require('discord.js'); let discord = require('discord.js');
const client = new Discord.Client(); let client = new discord.Client();
const cron = require('node-cron'); //let cron = require('node-cron');
// Bot Modules // Bot Modules
const dataRequest = require('../modules/dataRequest'); let npcSettings = require('./npcSettings');
const calcRandom = require('../modules/calcRandom'); let shared = require("../Shared/shared");
// State Machine (Uncomment if needed) //dialog system
var BotEnumState = { let dialog = shared.GenerateDialogFunction(require("./dialog.json"));
WAITING: 0,
ACTIVE: 1
}
var botState = BotEnumState.ACTIVE;
const playingMessage = 'Scribe of the Codex'; //ADAM dialog decorator
const breakMessage = "Taking a break..." //NOTE: This isn't strictly necessary for the bots
dialog = function(baseDialog) {
return function(key, ...data) {
if ( (key === "help" || key === "lore") && typeof(data[0]) !== "undefined") {
//force the arg into camelCase
arg = data[0].toLowerCase();
arg = arg.charAt(0).toUpperCase() + arg.substr(1);
key += arg;
}
// The ready event is vital, it means that your bot will only start reacting to information let result = baseDialog(key, ...data);
// from Discord _after_ ready is emitted
if (result === "") {
return "No result for \"" + key + "\"";
}
return result;
}
}(dialog);
//handle errors
client.on('error', console.error);
// The ready event is vital, it means that your bot will only start reacting to information from discord _after_ ready is emitted
client.on('ready', async () => { client.on('ready', async () => {
// Generates invite link // Generates invite link
try { try {
let link = await client.generateInvite(["ADMINISTRATOR"]); let link = await client.generateInvite(["ADMINISTRATOR"]);
console.log("Invite Link: " + link); console.log("Invite Link: " + link);
} catch(e) { } catch(e) {
console.log(e.stack); console.log(e.stack);
} }
// You can set status to 'online', 'invisible', 'away', or 'dnd' (do not disturb) // You can set status to 'online', 'invisible', 'away', or 'dnd' (do not disturb)
client.user.setStatus('online'); client.user.setStatus('online');
// Sets your "Playing"
client.user.setActivity(playingMessage); // Sets your "Playing"
console.log(`Connected! \ if (npcSettings.activity) {
\nLogged in as: ${client.user.username} - (${client.user.id})`); client.user.setActivity(npcSettings.activity, { type: npcSettings.type })
//DEBUGGING
.then(presence => console.log("Activity set to " + (presence.game ? presence.game.name : 'none')) )
.catch(console.error);
}
console.log("Logged in as: " + client.user.username + " - " + client.user.id);
}); });
// Create an event listener for messages // Create an event listener for messages
client.on('message', async message => { client.on('message', async message => {
// Ignores ALL bot messages // Ignores ALL bot messages
if (message.author.bot) return; if (message.author.bot) {
// Message has to be in Outskirts (should be edited later) return;
if (!(message.channel.id === process.env.TAVERN_CHANNEL_ID }
|| message.channel.id === process.env.TEST_CHANNEL_ID)) return;
// Has to be (prefix)command
if (message.content.indexOf(process.env.PREFIX) !== 0) return;
// "This is the best way to define args. Trust me." //skip the statis channel
// - Some tutorial dude on the internet if (message.channel.id === process.env.STASIS_CHANNEL_ID) {
const args = message.content.slice(process.env.PREFIX.length).trim().split(/ +/g); return;
const command = args.shift().toLowerCase(); }
switch (command) { //skip the gate channel
case "ping": if (message.channel.id === process.env.GATE_CHANNEL_ID) {
if (isAdmin(message.author.id)) return;
message.reply("Pong!"); }
break;
case "summon": // Has to be (prefix)command
if (isAdmin(message.author.id)) { if (message.content.indexOf(process.env.PREFIX) !== 0) {
console.log("Summon the bot!"); return;
BotTurnOnline(process.env.TAVERN_CHANNEL_ID); }
}
break; if (processBasicCommands(client, message)) {
case "vanish": return;
if (isAdmin(message.author.id)) { }
BotTurnOffline(process.env.TAVERN_CHANNEL_ID);
}
break;
}
}); });
client.on('error', console.error); //Log our bot in
client.login(npcSettings.token);
// Turn online and turn offline function processBasicCommands(client, message) {
function BotTurnOnline(channel) { // "This is the best way to define args. Trust me."
sendMessage(channel, `Insert Online Message here. \ // - Some tutorial dude on the internet
\n\n***SOME BOLD AND ITALIC TEXT***`); let args = message.content.slice(process.env.PREFIX.length).trim().split(/ +/g);
client.user.setStatus('online'); let command = args.shift().toLowerCase();
client.user.setActivity(playingMessage);
botState = BotEnumState.ACTIVE; switch (command) {
case "ping":
if (shared.IsAdmin(client, message.author)) {
shared.SendPublicMessage(client, message.author, message.channel, "PONG!");
}
return true;
case "help":
case "lore":
shared.SendPublicMessage(client, message.author, message.channel, dialog(command, args[0]));
return true;
default:
shared.SendPublicMessage(client, message.author, message.channel, dialog("unknown"));
return true;
}
return false;
} }
function BotTurnOffline(channel) {
sendMessage(channel, `Insert Offline Message here. \
\n\n***SOME BOLD AND ITALIC TEXT***`);
client.user.setStatus('invisible');
client.user.setActivity('');
botState = BotEnumState.WAITING;
}
// You may use cron normally
cron.schedule('* * * * Saturday', function() {
console.log('Saturday join.');
});
// Async Waiting
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
});
}
// Gets if user has an Overseers rank
function isAdmin(userID) {
var guild = client.guilds.get(process.env.SANCTUM_ID);
return guild.members.get(userID).roles.find(role => role.name === "Overseers");
}
// Send message handler
function sendMessage(userID, channelID, message) {
// Handle optional first argument (so much for default arugments in node)
if (message === undefined) {
message = channelID;
channelID = userID;
userID = null;
}
// Utility trick (@userID with an optional argument)
if (userID != null) {
message = "<@" + userID + "> " + message;
}
// Sends message (needs client var, therefore I think external script won't work)
client.channels.get(channelID).send(message);
}
// Log our bot in (change the token by looking into the .env file)
client.login(process.env.LIBRARIAN_TOKEN);

7
Librarian/npcSettings.js Normal file
View File

@@ -0,0 +1,7 @@
require('dotenv').config({path: '../.env'});
module.exports = {
activity: "for the signal.",
type: "WATCHING",
token: process.env.LIBRARIAN_TOKEN,
}

View File

@@ -1,29 +1,20 @@
{ {
"name": "sanctum-deploypackage", "name": "nodejs-elasticbeanstalk",
"version": "1.0.1", "version": "1.0.0",
"description": "Universal package for all SANCTUM bots, using pm2.", "description": "",
"main": "", "main": "server.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "start": "forever -o forever.log -e error.log start librarian.js",
"start": "echo \"Error: you need to use the bot name as a command.\" && exit 1", "restart": "forever -o forever.log -e error.log restart librarian.js",
"alexis": "pm2 start alexis.js -i max --watch", "stop": "forever stop librarian.js",
"graze": "pm2 start graze.js -i max --watch", "node": "node librarian.js"
"librarian": "pm2 start librarian.js -i max --watch",
"mainframe": "pm2 start mainframe.js -i max --watch",
"mori": "pm2 start mori.js -i max --watch",
"mosiah": "pm2 start mosiah.js -i max --watch",
"ravager": "pm2 start ravager.js -i max --watch",
"rey": "pm2 start rey.js -i max --watch",
"troll": "pm2 start troll.js -i max --watch"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"discord.js": "^11.4.2", "discord.js": "^11.4.2",
"dotenv": "^6.0.0", "dotenv": "^6.0.0",
"express": "^4.16.3", "forever": "^0.15.3",
"node-cron": "^1.2.1", "node-cron": "^1.2.1"
"socket.io": "^2.1.1",
"socket.io-request": "^0.8.0"
} }
} }