Last active
December 5, 2016 00:20
-
-
Save Rohansi/f503530f91c8714089bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name RohBot FILTERED | |
// @version 0.5 | |
// @description enter something useful | |
// @author Rohansi | |
// @match https://rohbot.net/ | |
// @grant none | |
// ==/UserScript== | |
var storageKey = "filtered-patterns2"; | |
var mutedIds = {}; // { "69": "toaster", ... } | |
function load() { try { mutedIds = JSON.parse(RohStore.get(storageKey) || "{}"); } catch (e) { } } | |
function save() { RohStore.set(storageKey, JSON.stringify(mutedIds)); } | |
function unescape(str) { return $('<textarea/>').html(str).text(); } | |
function convertToUser(chat, value, unmuting) { | |
if (!unmuting) { | |
var user = chat.userList.find(function (user) { return unescape(user.Name) == value; }); | |
if (user) | |
return { id: user.UserId, name: user.Name }; | |
if ($.isNumeric(value)) { | |
user = chat.userList.find(function (user) { return user.UserId == value; }); | |
if (user) | |
return { id: user.UserId, name: user.Name }; | |
return { id: value, name: "[unknown]" }; | |
} | |
} else { | |
var pairs = _.pairs(mutedIds); // [ [ id, name ], ... ] | |
var user = | |
pairs.find(function (p) { return unescape(p[1]) == value; }) || | |
pairs.find(function (p) { return p[0] == value; }); | |
if (user) | |
return { id: user[0], name: user[1] }; | |
} | |
return null; | |
} | |
cmd.register("mute", "]", function (chat, args) { | |
if (args.length != 1 || args[0].length === 0) { | |
chat.statusMessage("Usage: /mute [name|id]"); | |
return; | |
} | |
var user = convertToUser(chat, args[0]); | |
if (!user) { | |
chat.statusMessage("Name not found or ID is not valid."); | |
return; | |
} | |
if (mutedIds.hasOwnProperty(user.id)) { | |
chat.statusMessage("That user is already muted."); | |
return; | |
} | |
mutedIds[user.id] = user.name; | |
save(); | |
chat.statusMessage("Muted " + user.name + "!"); | |
}); | |
cmd.register("unmute", "]", function (chat, args) { | |
if (args.length != 1 || args[0].length === 0) { | |
chat.statusMessage("Usage: /unmute [name|id]"); | |
return; | |
} | |
var user = convertToUser(chat, args[0], true); | |
if (!user) { | |
chat.statusMessage("Name not found or ID is not valid."); | |
return; | |
} | |
delete mutedIds[user.id]; | |
save(); | |
chat.statusMessage("Unmuted " + user.name + "!"); | |
}); | |
cmd.register("muted", "", function (chat, args) { | |
if (Object.keys(mutedIds).length == 0) { | |
chat.statusMessage("Nobody is muted."); | |
return; | |
} | |
var muted = _.pairs(mutedIds).map(function (p) { return p[1] + " (" + p[0] + ")" }); | |
chat.statusMessage("Muted: " + muted.join(", ")); | |
}); | |
chatMgr.lineFilter.add(function (line, prepend, e) { | |
if (line.Sender) { | |
if (mutedIds.hasOwnProperty(line.SenderId)) | |
e.filtered = true; | |
} else { | |
if (mutedIds.hasOwnProperty(line.ForId)) | |
e.filtered = true; | |
if (line.By && mutedIds.hasOwnProperty(line.ById)) | |
e.filtered = true; | |
} | |
}); | |
chatMgr.userFilter.add(function (user, e) { | |
if (mutedIds.hasOwnProperty(user.UserId)) | |
e.filtered = true; | |
}); | |
load(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment