Finished tokebot implementation. tz and of tokes decapitalized and

consolidated, !r added, toke log and  per user toke count tracking. Toke
count on tooltip. Improved moderator commands & tokebot output
formatting.
This commit is contained in:
rainbownapkin 2022-07-23 09:24:19 +00:00
parent 46bcb040f2
commit fcb562397a
8 changed files with 389 additions and 49 deletions

View file

@ -624,13 +624,18 @@ Channel.prototype.maybeResendUserlist = function maybeResendUserlist(user, newRa
};
Channel.prototype.packUserData = function (user) {
var tc = 0;
if(this.modules.tokebot.statmap != null){
tc = (this.modules.tokebot.statmap.get(user.getName()) == null ? 0 : this.modules.tokebot.statmap.get(user.getName()));
}
var base = {
name: user.getName(),
rank: user.account.effectiveRank,
profile: user.account.profile,
meta: {
afk: user.is(Flags.U_AFK),
muted: user.is(Flags.U_MUTED) && !user.is(Flags.U_SMUTED)
muted: user.is(Flags.U_MUTED) && !user.is(Flags.U_SMUTED),
toke: tc
}
};
@ -643,7 +648,8 @@ Channel.prototype.packUserData = function (user) {
muted: user.is(Flags.U_MUTED),
smuted: user.is(Flags.U_SMUTED),
aliases: user.account.aliases,
ip: user.displayip
ip: user.displayip,
toke: tc
}
};
@ -656,7 +662,8 @@ Channel.prototype.packUserData = function (user) {
muted: user.is(Flags.U_MUTED),
smuted: user.is(Flags.U_SMUTED),
aliases: user.account.aliases,
ip: user.realip
ip: user.realip,
toke: tc
}
};
@ -697,7 +704,6 @@ Channel.prototype.sendUserProfile = function (users, user) {
name: user.getName(),
profile: user.account.profile
};
users.forEach(function (u) {
u.socket.emit("setUserProfile", packet);
});

View file

@ -17,8 +17,12 @@ import fs from 'fs';
var ChannelModule = require("./module");
//global vars
var tokes = loadTokes();
var solotokes = ["", "https://ourfore.st/img/femotes/onetoker.jpg","https://ourfore.st/img/femotes/solotoke.jpg","https://ourfore.st/img/femotes/1toker.gif"];
function loadTokes(){
//global functions
function loadTokes(){//load tokes as array from file
const rawContents = fs.readFileSync("tokebot/tokes").toString('utf8');
var spcReg = /^\s*$/g;
var t = rawContents.split("\n").filter(function(i){
@ -27,12 +31,36 @@ function loadTokes(){
return t;
}
var tokes = loadTokes();
//used to store ES6 maps as arrays in JSON
function mapReplacer(key, value) {
if(value instanceof Map) {
return {
dataType: 'Map',
value: Array.from(value.entries()), // or with spread: value: [...value]
};
} else {
return value;
}
}
function mapReviver(key, value) {
if(typeof value === 'object' && value !== null) {
if (value.dataType === 'Map') {
return new Map(value.value);
}
}
return value;
}
function randi(len) {//get random number from zero to len, meant for use to pull random items from an array
return Math.floor(Math.random() * len); //The maximum is exclusive and the minimum is inclusive
}
//constructor
function TokebotModule(_channel){
ChannelModule.apply(this, arguments);
//mod command registration
this.channel.modules.chat.registerCommand("!resettoke", this.resettoke.bind(this));
@ -45,21 +73,27 @@ function TokebotModule(_channel){
//!toke command registration
this.updatetokes();
this.channel.modules.chat.registerCommand("!r", this.randotoke.bind(this));
this.loadtfile();//load up toke stats from toke file.
}
//protoype definition
TokebotModule.prototype = Object.create(ChannelModule.prototype);
//tokebot object properties
TokebotModule.prototype.toking = 0;
TokebotModule.prototype.tokers = [];
TokebotModule.prototype.cdown = 3;
TokebotModule.prototype.cdel = 120;
TokebotModule.prototype.ctime = 120;
TokebotModule.prototype.solotokes = ["", "https://ourfore.st/img/femotes/onetoker.jpg","https://ourfore.st/img/femotes/solotoke.jpg","https://ourfore.st/img/femotes/1toker.gif"];
TokebotModule.prototype.statmap = null;
//mod commands
TokebotModule.prototype.resettoke = function(user, msg, _meta){
if(user.account.effectiveRank >= 2 && this.toking == 2){
//this.toking = 0;
this.ctime = 0;
this.tokewhisper("!toke cooldown reset.", user.account.name);
}
@ -98,6 +132,11 @@ TokebotModule.prototype.tokewhisperCmd = function(user, msg, _meta){
}
}
//extra user commands
TokebotModule.prototype.randotoke = function(user, msg, _meta){
this.toke(user, '!' + tokes[randi(tokes.length)],_meta);
}
//main toke logic (adapted from chozobot implementation)
TokebotModule.prototype.toke = function (user, msg, _meta){
var name = user.getName()
@ -116,7 +155,7 @@ TokebotModule.prototype.toke = function (user, msg, _meta){
if(this.tokers.includes(name)){
this.tokewhisper(" You're already taking part in this toke!", name);
}else{
this.tokesay("joined the toke! Post " + msg + " to take part!");
this.tokesay(name + " joined the toke! Post " + msg + " to take part!");
this.tokers.push(name);
this.cdown = 3;
}
@ -156,8 +195,9 @@ TokebotModule.prototype.endtoke = function (tb){
tb.tokesay("Take a toke " + callstring + "! " + tb.tokers.length + " tokers!");
}else{
tb.tokesay("Take a toke " + tb.tokers.toString() + ". " + (tb.solotokes[tb.getRandomInt(0,tb.solotokes.length)]));
tb.tokesay("Take a toke " + tb.tokers.toString() + ". " + (solotokes[randi(solotokes.length)]));
}
tb.logtoke();
tb.tokers = [];
tb.toking = 2;//reset toking mode
setTimeout(tb.cooldown, 1000, tb);
@ -181,9 +221,7 @@ TokebotModule.prototype.updatetokes = function (){
tokes = loadTokes();
if(this.channel.modules.chat){//register !toke commands
if(tokes == null){//if for some reason tokes file couldnt be loaded
this.channel.modules.chat.registerCommand("!toke", this.toke.bind(this));
console.log("[tokebot] Unable to load toke commands from ./tokebot/tokes, defaulting to !toke definition");
if(tokes == null){//if for some reason tokes file couldnt be loaded this.channel.modules.chat.registerCommand("!toke", this.toke.bind(this)); console.log("[tokebot] Unable to load toke commands from ./tokebot/tokes, defaulting to !toke definition");
}else{//if we we're able to pull toke commands
var _this = this;//we need to use this, might put this up higher to replace the tb parameter in other member functions
tokes.forEach(function(tokec){
@ -200,7 +238,7 @@ TokebotModule.prototype.tokesay = function (msg,quiet){
meta:{
addClass: (quiet ? null : "shout"),
addClassToNameAndTimestamp: true,
forceShowName: true,
forceShowName: (quiet ? true : false), //It's loud enough when announcing. Toke chats are rare enouhg to be more prominent :P
modflair: 3
},
time: Date.now()
@ -224,10 +262,46 @@ TokebotModule.prototype.tokewhisper = function (msg, usr){//(msg, username)
}
}
TokebotModule.prototype.getRandomInt = function (min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
};
//filesystem manipulation functions
TokebotModule.prototype.writetokelog = function(){//append a toke to current channels toke log
var _this = this;
fs.appendFile("tokebot/" + this.channel.name + "_toke.log" ,('[' + this.tokers.toString() + '],' + this.tokers.length + ',' + new Date().getTime() + "\n"), function(err){
if(err){
console.log("[chan: " + _this.channel.name + "] TOKE LOG WRITE ERROR: " + err);
}
});
}
TokebotModule.prototype.loadtfile = function(){//load tokefile into statmap(default to new Map() on error)
var _this = this;
fs.readFile("tokebot/" + this.channel.name + "_tokefile", function(err,data){
if(err){
console.log("[chan: " + _this.channel.name + "] TOKE FILE READ ERROR (may overwrite existing stat file!): " + err);
_this.statmap = new Map();
}else{
_this.statmap = JSON.parse(data, mapReviver);
}
});
}
TokebotModule.prototype.writetfile = function(){//write statmap to tokefile
var _this = this;
fs.writeFile("tokebot/" + this.channel.name + "_tokefile", JSON.stringify(_this.statmap, mapReplacer), function(err,data){
if(err){
console.log("[chan: " + _this.channel.name + "] TOKE FILE WRITE ERROR: " + err);
}
});
}
TokebotModule.prototype.logtoke = function(){
this.writetokelog();//save toke to toke log
var _this = this;
this.tokers.forEach(function (u){//for all tokers
var ct = _this.statmap.get(u);//lets make this a lil prettier :P
_this.statmap.set(u,(ct == null ? 1 : ++ct));//increment toke counter
});
this.writetfile();//write statmap to tfile
}
module.exports = TokebotModule;