fishing-bot-mod/src/Bot.js

270 lines
10 KiB
JavaScript
Raw Normal View History

2022-01-13 06:18:06 +01:00
const { sendChat } = require('./MPPClient');
// const DiscordClient = require('./DiscordClient');
const MPPClient = require('./MPPClient');
const StaticEventEmitter = require('./StaticEventEmitter');
module.exports = class Bot extends StaticEventEmitter {
static start(token) {
MPPClient.start(token);
this.bindEventListeners();
this.commands = new Map();
this.admin = [
"2ffc3744fbc1bc6c6ef4a330",
2022-01-16 12:24:01 +01:00
"a41651bf8a46bafc5548dad6",
2022-01-18 23:03:59 +01:00
"ead940199c7d9717e5149919",
2022-03-15 19:35:27 +01:00
"8107156a27514cebcb65195d",
"e1e7e489ec8d635d980c65cd"
2022-01-13 06:18:06 +01:00
];
this.prefix = "/"; // never change this
this.loadCommands();
}
static bindEventListeners() {
this.on("chat.receive", msg => {
console.log(msg.p.name + ": " + msg.a);
let m = {
referer: msg.p,
};
this.runCommand(msg);
});
this.on("chat.send", msg => {
MPPClient.sendChat(msg);
});
}
static runCommand(msg) {
// let role;
// msg.member.guild.roles.cache.forEach(r => {
// if (r.name.toString() == msg.member.user.id.toString()) {
// role = r;
// }
// });
// msg.p = {
// name: msg.author.username,
// _id: msg.author.id,
// color: role.color,
// id: msg.author.id
// }
// msg.a = msg.content;
if(msg.a[0] == "" && msg.p.id !== client.participantId) {
msg.a[0] = "/";
}
function findParticipantByName(name) {
if(!name || name.trim() == "") return undefined;
2022-01-18 23:03:59 +01:00
for(var id in MPPClient.client.ppl) {
if(MPPClient.client.ppl.hasOwnProperty(id) && MPPClient.client.ppl[id].name === name) {
return MPPClient.client.ppl[id];
2022-01-13 06:18:06 +01:00
}
}
return undefined;
};
function findParticipantByNameCaseInsensitive(name) {
if(!name || name.trim() == "") return undefined;
var part = findParticipantByName(name);
if(!part) {
2022-01-18 23:03:59 +01:00
var name_lc = name.toLowerCase();
for (let p of Object.values(MPPClient.client.ppl)) {
2022-01-13 06:18:06 +01:00
if(p.name.toLowerCase() === name_lc) {
part = p;
}
2022-01-18 23:03:59 +01:00
}
2022-01-13 06:18:06 +01:00
}
return part;
};
function findParticipantByNameFuzzy(name) {
if(!name || name.trim() == "") return undefined;
name = name.toLowerCase();
var part = findParticipantByNameCaseInsensitive(name);
for(var id in MPPClient.client.ppl) {
2022-01-18 23:03:59 +01:00
if(MPPClient.client.ppl.hasOwnProperty(id) && MPPClient.client.ppl[id].name.toLowerCase().indexOf(name) === 0) {
part = MPPClient.client.ppl[id];
2022-01-13 06:18:06 +01:00
break;
}
}
for(var id in MPPClient.client.ppl) {
2022-01-18 23:03:59 +01:00
if(MPPClient.client.ppl.hasOwnProperty(id) && MPPClient.client.ppl[id].name.toLowerCase().indexOf(name) !== -1) {
2022-01-13 06:18:06 +01:00
part = MPPClient.client.ppl[id];
break;
}
}
2022-01-18 23:03:59 +01:00
// new
if (!part) {
// for (var id in MPPClient.client.ppl) {
// let p = MPPClient.client.ppl[id];
// if (!p) continue;
// if (p._id.toLowerCase().includes(name.toLowerCase())) {
// part = p;
// break;
// }
// }
for (var p of Object.values(MPPClient.client.ppl)) {
if (!p) continue;
if (p._id.toLowerCase().includes(name.toLowerCase())) {
part = p;
break;
}
}
}
2022-01-13 06:18:06 +01:00
return part;
};
2022-01-18 23:03:59 +01:00
var TOO_MANY_FISH = 50;
2022-01-13 06:18:06 +01:00
if (msg.cmd.startsWith("give_")) {
2022-01-18 23:03:59 +01:00
var amt = parseInt(msg.cmd.substr(5));
console.log(msg.cmd.substr(5));
if(amt > 0) {
console.log('test');
if(amt > 100 && msg.p.id !== client.participantId) {
sendChat("Friend "+msg.p.name+": you can only give up to 100 at a time.");
} else {
var thief = msg.p;
var victim = findParticipantByNameFuzzy(msg.args[0]);
if(!victim) {
sendChat("Friend " +thief.name+" missed");
return;
}
if(victim._id == thief._id) {
sendChat("Friendly friend " +thief.name+" fudged");
return;
}
var target_fish = msg.argcat(1);
db.getFish(thief._id, function(thief_fish) {
db.getFish(victim._id, function(victim_fish) {
if(victim_fish.length >= TOO_MANY_FISH) {
sendChat("Friend " +victim.name+" is carrying too much.");
return;
}
if(thief_fish.length > 0) {
var arg = target_fish.trim().toLowerCase();
var thefish = "items";
for(var j = 0; j < amt; j++) {
var idx = -1;
for(var i = 0; i < thief_fish.length; i++) {
if(arg == "" || thief_fish[i].toLowerCase().indexOf(arg) !== -1) {
idx = i;
break;
}
}
if(idx == -1) {
sendChat("Friend " +thief.name+": You don't have "+amt+" "+arg+".");
return;
}
thefish = thief_fish[idx];
thief_fish.splice(idx, 1);
victim_fish.push(thefish);
}
sendChat("Our friend " +thief.name+" gave "+victim.name+" his/her e.g. ("+thefish+") x "+amt+".");
db.putFish(thief._id, thief_fish);
db.putFish(victim._id, victim_fish);
} else {
sendChat("Friend " +thief.name+": You don't have the fish to give.");
}
});
});
return;
}
}
// return sendChat("You may not /give_, it misses every time.");
2022-01-13 06:18:06 +01:00
}
if (msg.cmd.startsWith("bestow_")) {
// var amt = parseInt(msg.cmd.substr(8));
// if(amt > 0) {
// if(amt > 100 && msg.p.id !== client.participantId) {
// sendChat("Friend "+msg.p.name+": you can only bestow up to 100 at a time.");
// } else {
// var thief = msg.p;
// var victim = client.ppl[msg.args[0]];
// if(!victim) {
// sendChat("Friend " +thief.name+" missed");
// return;
// }
// if(victim._id == thief._id) {
// sendChat("Friendly friend " +thief.name+" fudged");
// return;
// }
// var target_fish = msg.argcat(1);
// db.getFish(thief._id, function(thief_fish) {
// db.getFish(victim._id, function(victim_fish) {
// if(victim_fish.length >= TOO_MANY_FISH) {
// sendChat("Friend " +victim.name+" is carrying too much.");
// return;
// }
// if(thief_fish.length > 0) {
// var arg = target_fish.trim().toLowerCase();
// var thefish = "items";
// for(var j = 0; j < amt; j++) {
// var idx = -1;
// for(var i = 0; i < thief_fish.length; i++) {
// if(arg == "" || thief_fish[i].toLowerCase().indexOf(arg) !== -1) {
// idx = i;
// break;
// }
// }
// if(idx == -1) {
// sendChat("Friend " +thief.name+": You don't have "+amt+" "+arg+".");
// return;
// }
// thefish = thief_fish[idx];
// thief_fish.splice(idx, 1);
// victim_fish.push(thefish);
// }
// sendChat("Our friend " +thief.name+" bestowed "+victim.name+" his/her e.g. ("+thefish+") x "+amt+".");
// db.putFish(thief._id, thief_fish);
// db.putFish(victim._id, victim_fish);
// } else {
// sendChat("Friend " +thief.name+": You don't have the fish to bestow.");
// }
// });
// });
// return;
// }
// }
return sendChat("You may not /bestow_, it misses every time.");
}
if (msg.a == '/get fruit') {
msg.cmd = 'get fruit'
}
this.commands.forEach(cmd => {
let usedCommand = false;
cmd.cmd.forEach(c => {
if (msg.cmd == c) {
usedCommand = true;
}
});
if (!usedCommand) return;
if (msg.rank == 'admin') {
cmd.func(msg, true);
} else {
cmd.func(msg, false);
}
});
}
static fixUsage(str) {
return str.split("&PREFIX").join(this.prefix);
}
static getRank(id) {
if (this.admin.indexOf(id) !== -1) {
return "admin";
} else {
return "none";
}
}
static async loadCommands() {
(require('./Commands'))(this);
}
}