Working the modules into Shared, untested

This commit is contained in:
2018-10-26 18:05:17 +11:00
parent 3745a1efc4
commit cabcc88685
13 changed files with 64 additions and 137 deletions

View File

@@ -53,7 +53,6 @@ GROUP_C_LEADER_NAME="Dairo, High Prophet of The Hand"
# Channel IDs # Channel IDs
# Replace them with your own on a sandbox server! # Replace them with your own on a sandbox server!
CODEX_CHANNEL_ID="457580831284789249"
OUTSKIRTS_CHANNEL_ID="462382076927148054" OUTSKIRTS_CHANNEL_ID="462382076927148054"
TAVERN_CHANNEL_ID="462684096024543242" TAVERN_CHANNEL_ID="462684096024543242"
GATE_CHANNEL_ID="459196623923445779" GATE_CHANNEL_ID="459196623923445779"
@@ -111,5 +110,6 @@ RANK_1_THRESHOLD=1
RANK_2_THRESHOLD=15 RANK_2_THRESHOLD=15
RANK_3_THRESHOLD=30 RANK_3_THRESHOLD=30
# Server Pass Key # Server
SERVER_ADDRESS="http://skullboxstudios.com"
SERVER_PASS_KEY="" SERVER_PASS_KEY=""

View File

@@ -2,15 +2,14 @@
require('dotenv').config({path: '../.env'}); require('dotenv').config({path: '../.env'});
// Node Modules // Node Modules
let Discord = require('discord.js'); let discord = require('discord.js');
let client = new Discord.Client(); let client = new discord.Client();
let cron = require('node-cron'); let cron = require('node-cron');
// Bot Modules // Bot Modules
let npcSettings = require('./npcSettings');
let shared = require("../shared/shared");
let core = require("./core"); let core = require("./core");
let dataRequest = require('../modules/dataRequest'); let npcSettings = require('./npcSettings');
let shared = require("../Shared/shared");
//dialog system //dialog system
let dialog = shared.GenerateDialogFunction(require("./dialog.json")); let dialog = shared.GenerateDialogFunction(require("./dialog.json"));
@@ -37,7 +36,7 @@ dialog = function(baseDialog) {
//handle errors //handle errors
client.on('error', console.error); 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 // 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 {
@@ -63,7 +62,7 @@ client.on('ready', async () => {
//ADAM updates stamina (1) and health by 1% every 2 min. //ADAM updates stamina (1) and health by 1% every 2 min.
cron.schedule('*/2 * * * *', () => { cron.schedule('*/2 * * * *', () => {
console.log('Updating STAMINA every 2 min.'); console.log('Updating STAMINA every 2 min.');
dataRequest.sendServerData("updateStamina"); shared.SendServerData("updateStamina");
}); });
}); });

View File

@@ -1,8 +1,9 @@
exports = module.exports = {}; exports = module.exports = {};
let Discord = require('discord.js'); let dataRequest = require("../Shared/data_request");
let shared = require("../shared/shared"); let discord = require('discord.js');
let calcRandom = require('../modules/calcRandom'); let shared = require("../Shared/shared");
let calcRandom = require('../Shared/calc_random');
//ProcessGameplayCommands //ProcessGameplayCommands
//client - discord.js client //client - discord.js client
@@ -17,7 +18,7 @@ exports.ProcessGameplayCommands = function(client, message, dialog) {
switch (command) { switch (command) {
case "checkin": case "checkin":
let checkinAmount = calcRandom.random(4, 9); let checkinAmount = calcRandom.random(4, 9);
let checkInResponse = String(dataRequest.sendServerData("checkin", checkinAmount, message.author.id)); let checkInResponse = String(dataRequest.SendServerData("checkin", message.author.id, checkinAmount));
if (checkInResponse === "available") { if (checkInResponse === "available") {
shared.SendPublicMessage(client, message.author, message.channel, dialog("checkin", checkinAmount)); shared.SendPublicMessage(client, message.author, message.channel, dialog("checkin", checkinAmount));
shared.AddXP(client, message.author, 1); //1XP shared.AddXP(client, message.author, 1); //1XP
@@ -50,7 +51,7 @@ exports.ProcessGameplayCommands = function(client, message, dialog) {
return true; return true;
} }
let accountBalance = dataRequest.loadServerData("account",message.author.id); let accountBalance = dataRequest.LoadServerData("account", message.author.id);
//not enough money in account //not enough money in account
if (accountBalance < amount) { if (accountBalance < amount) {
@@ -59,7 +60,7 @@ exports.ProcessGameplayCommands = function(client, message, dialog) {
} }
//try to send the money //try to send the money
if (dataRequest.sendServerData("transfer", targetMember.id, message.author.id, amount) != "success") { if (dataRequest.SendServerData("transfer", message.author.id, targetMember.id, amount) != "success") {
shared.SendPublicMessage(client, message.channel, dialog("giveFailed", message.author.id)); shared.SendPublicMessage(client, message.channel, dialog("giveFailed", message.author.id));
return true; return true;
} }
@@ -130,7 +131,7 @@ exports.GetStats = function(user) { //Grabs all parameters from server
user = client.users.find(item => item.username === user || item.id === user); user = client.users.find(item => item.username === user || item.id === user);
} }
let userStatsResponse = String(dataRequest.loadServerData("userStats",user.id)).split(","); let userStatsResponse = String(dataRequest.LoadServerData("userStats", user.id)).split(",");
if (userStatsResponse[0] == "failure") { if (userStatsResponse[0] == "failure") {
throw "server returned an error to userStats request"; throw "server returned an error to userStats request";
@@ -194,7 +195,7 @@ exports.PrintStats = function(client, member, channel, stats) {
} }
// Creates embed & sends it // Creates embed & sends it
const embed = new Discord.RichEmbed() const embed = new discord.RichEmbed()
.setAuthor(`${member.user.username}`, member.user.avatarURL) .setAuthor(`${member.user.username}`, member.user.avatarURL)
.setColor(member.displayColor) .setColor(member.displayColor)
.setDescription(`${levelText} ${levelProgress} | ${crystalText} | ${cannisterText}`) .setDescription(`${levelText} ${levelProgress} | ${crystalText} | ${cannisterText}`)

View File

@@ -4,14 +4,17 @@
"description": "", "description": "",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "start": "forever -o forever.log -e error.log start adam.js",
"start": "node adam.js" "restart": "forever -o forever.log -e error.log restart adam.js",
"stop": "forever stop adam.js",
"node": "node adam.js"
}, },
"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",
"forever": "^0.15.3",
"node-cron": "^1.2.1" "node-cron": "^1.2.1"
} }
} }

17
Shared/calc_random.js Normal file
View File

@@ -0,0 +1,17 @@
//initialize the exports
exports = module.exports = {};
//Random
//low - the inclusive lower bounds of the result
//high - the inclusive higher bounds of the result
exports.Random = function(low, high) {
//inclusive high
return Math.floor(Math.random() * (high - low + 1) + low);
}
//Gamble (depreciated)
//percentage - chance of winning as a percentage
exports.Gamble = function(percentage) {
// Random generation by %
return exports.Random(1, 100) <= percentage;
}

15
Shared/data_request.js Normal file
View File

@@ -0,0 +1,15 @@
//initialize the exports
exports = module.exports = {};
require("dotenv").config({path: "../.env"});
let request = require("sync-request");
exports.LoadServerData = function(dataType, usersID = "") {
let response = request("GET", `${process.env.SERVER_ADDRESS}/getData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=${dataType}&userid=${usersID}`);
return response.getBody();
}
exports.SendServerData = function(dataType, usersID = "", dataToSend="", dataToSend2 = ""){
let response = request("GET", `${process.env.SERVER_ADDRESS}/sendData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=${dataType}&userid=${usersID}&dataToSend=${dataToSend}&dataToSend2=${dataToSend2}`);
return response.getBody();
}

View File

@@ -1,8 +1,8 @@
//initialize the exports //initialize the exports
exports = module.exports = {}; exports = module.exports = {};
dataRequest = require("../modules/dataRequest"); let dataRequest = require("./data_request");
messaging = require("./messaging"); let messaging = require("./messaging");
//CheckFaction //CheckFaction
//factionRole - the value to check //factionRole - the value to check
@@ -82,13 +82,13 @@ exports.ChangeFaction = async function(client, factionRole, channel, member) {
return "alreadyJoined"; return "alreadyJoined";
} }
if (dataRequest.loadServerData("hasConvertedToday", member.user.id) == 1) { if (dataRequest.LoadServerData("hasConvertedToday", member.user.id) == 1) {
//can't change too fast //can't change too fast
return "hasConvertedToday"; return "hasConvertedToday";
} }
//Creates a new user //Creates a new user
var newUserResponse = String(dataRequest.sendServerData("newUser", "New user.", member.user.id)); var newUserResponse = String(dataRequest.SendServerData("newUser", member.user.id, "New user."));
//joins the new faction //joins the new faction
await member.removeRole(process.env.GROUP_A_ROLE); await member.removeRole(process.env.GROUP_A_ROLE);
@@ -97,7 +97,7 @@ exports.ChangeFaction = async function(client, factionRole, channel, member) {
await member.addRole(factionRole); await member.addRole(factionRole);
//send the server the info (for logging) //send the server the info (for logging)
dataRequest.sendServerData("conversion", "Converted to " + exports.GetFactionName(factionRole), member.user.id); dataRequest.SendServerData("conversion", member.user.id, "Converted to " + exports.GetFactionName(factionRole));
if (newUserResponse === "createdUser") { if (newUserResponse === "createdUser") {
//send the private welcoming message //send the private welcoming message

View File

@@ -1,6 +1,6 @@
exports = module.exports = {}; exports = module.exports = {};
const dataRequest = require('../modules/dataRequest'); let dataRequest = require('./data_request');
//AddXP //AddXP
//client - discord.js client //client - discord.js client
@@ -12,7 +12,7 @@ exports.AddXP = function(client, user, amount) {
user = client.users.find(item => item.username === user || item.id === user); user = client.users.find(item => item.username === user || item.id === user);
} }
dataRequest.sendServerData("addXP", amount, user.id); dataRequest.SendServerData("addXP", user.id, amount);
} }
//LevelUp //LevelUp
@@ -32,7 +32,7 @@ exports.LevelUp = function(client, member) { //NOTE: why is this called separate
if (client.user.username == process.env.GROUP_B_LEADER_NAME && !member.roles.has(process.env.GROUP_B_ROLE)) return; if (client.user.username == process.env.GROUP_B_LEADER_NAME && !member.roles.has(process.env.GROUP_B_ROLE)) return;
if (client.user.username == process.env.GROUP_C_LEADER_NAME && !member.roles.has(process.env.GROUP_C_ROLE)) return; if (client.user.username == process.env.GROUP_C_LEADER_NAME && !member.roles.has(process.env.GROUP_C_ROLE)) return;
let response = String(dataRequest.sendServerData("getLevelUp", 0, member.user.id)); //TODO: please change the order of sendServerData's arguments! let response = String(dataRequest.sendServerData("getLevelUp", member.user.id));
let responseArray = response.split(","); let responseArray = response.split(",");
let responseMessage = responseArray[0]; let responseMessage = responseArray[0];

View File

@@ -1,6 +1,8 @@
//initialize the exports //initialize the exports
exports = module.exports = {}; exports = module.exports = {};
exports = Object.assign(exports, require("./calc_random.js"));
exports = Object.assign(exports, require("./data_request.js"));
exports = Object.assign(exports, require("./factions.js")); exports = Object.assign(exports, require("./factions.js"));
exports = Object.assign(exports, require("./inventory.js")); exports = Object.assign(exports, require("./inventory.js"));
exports = Object.assign(exports, require("./messaging.js")); exports = Object.assign(exports, require("./messaging.js"));

View File

@@ -1,27 +0,0 @@
module.exports = {
// Changed it from exclusive high to inclusive high
random: function(low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
},
randomExc: function(low, high) {
return Math.floor(Math.random() * (high - low) + low);
},
// Random generation by %
gamble: function(percentage) {
// I found out that it wasn't acutally entirely accurate (0.1 difference)
// So I changed it to be 1 to 100, and have a <= instead of <
// I calculated it with PineTools and a small JS script to calculate it.
// Feel free to change it!
var winState = Math.floor(module.exports.random(1, 100));
//var winState = Math.floor(module.exports.random(0, 101));
if (winState <= percentage) {
// if (winState < percentage)
return true;
} else {
return false;
}
}
}

View File

@@ -1,34 +0,0 @@
require('dotenv').config({path: '../.env'});
module.exports = {
isBotChannel: function(channelID) {
// Insert new channels here
var botChannels = [
process.env.GROUP_A_BOT_ID,
process.env.GROUP_B_BOT_ID,
process.env.GROUP_C_BOT_ID,
process.env.TEST_CHANNEL_ID
];
// Apparently Outskirts was in the discord.io code, add that if needed
for (let index = 0; index < botChannels.length; index++) {
const element = botChannels[index];
if (channelID == element) return true;
}
return false;
},
isRaidChannel: function(channelID) {
// Insert new channels here
var raidChannels = [
process.env.HELLS_GATE_CHANNEL_ID
];
for (let index = 0; index < raidChannels.length; index++) {
const element = raidChannels[index];
if (channelID == element) return true;
}
}
}

View File

@@ -1,29 +0,0 @@
var request = require('sync-request');
require('dotenv').config({path: '../.env'});
module.exports = {
loadServerData: function(dataToLoad, usersID = ''){
var res = request('GET', `http://skullboxstudios.com/projects/sanctum/botserver/getData.php?pk=${process.env.SERVER_PASS_KEY}&dataToLoad=` + dataToLoad + '&userid=' + usersID);
console.log(res.getBody());
return res.getBody();
},
sendServerData: function(dataType, dataToSend, usersID = '', dataToSend2 = ''){
var res = request('GET', `http://skullboxstudios.com/projects/sanctum/botserver/sendData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=` + dataType + '&dataToSend=' + dataToSend + '&dataToSend2=' + dataToSend2 + '&userid=' + usersID);
console.log(res.getBody());
return res.getBody();
},
sendAttackData: function(dataType, dataToSend, dataToSend2 = ''){
var res = request('GET', `http://skullboxstudios.com/projects/sanctum/botserver/sendPostData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=` + dataType + '&dataToSend=' + dataToSend + '&dataToSend2=' + dataToSend2)
console.log(res.getBody());
return res.getBody();
},
// Possibly unused?
postServerData: function(dataType, dataToPost, dataToSend2 = ''){
var res = request('POST', `http://skullboxstudios.com/projects/sanctum/botserver/sendPostData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=` + dataType + '&dataToSend2=' + dataToSend2, dataToPost)
console.log(res.getBody());
return res.getBody();
}
}

View File

@@ -1,20 +0,0 @@
// Do not use, no client reference
module.exports = {
sendMessage: function(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);
}
}