Last active
June 5, 2020 19:16
-
-
Save Cholik/a30a58bae7176baa7766bfd156f3e30e to your computer and use it in GitHub Desktop.
Add regional indicator reactions by just typing the corresponding keys
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
//META{"name":"AddReactionText","displayName":"AddReactionText","website":"https://gist.github.com/Cholik/a30a58bae7176baa7766bfd156f3e30e","source":"https://gist.githubusercontent.com/Cholik/a30a58bae7176baa7766bfd156f3e30e/raw/addreactiontext.plugin.js?update"}*// | |
class AddReactionText { | |
getName() { | |
return "AddReactionText"; | |
} | |
getDescription() { | |
return "Adds typed letters as reaction lel"; | |
} | |
getVersion() { | |
return "0.1.14"; | |
} | |
getAuthor() { | |
return "Cholik"; | |
} | |
start() { | |
ZLibrary.PluginUpdater.checkForUpdate( | |
this.getName(), | |
this.getVersion(), | |
"https://gist.githubusercontent.com/Cholik/a30a58bae7176baa7766bfd156f3e30e/raw/addreactiontext.plugin.js" | |
); | |
this.ri = { | |
A: "🇦", | |
B: "🇧", | |
C: "🇨", | |
D: "🇩", | |
E: "🇪", | |
F: "🇫", | |
G: "🇬", | |
H: "🇭", | |
I: "🇮", | |
J: "🇯", | |
K: "🇰", | |
L: "🇱", | |
M: "🇲", | |
N: "🇳", | |
O: "🇴", | |
P: "🇵", | |
Q: "🇶", | |
R: "🇷", | |
S: "🇸", | |
T: "🇹", | |
U: "🇺", | |
V: "🇻", | |
W: "🇼", | |
X: "🇽", | |
Y: "🇾", | |
Z: "🇿", | |
"0": "0️⃣", | |
"1": "1️⃣", | |
"2": "2️⃣", | |
"3": "3️⃣", | |
"4": "4️⃣", | |
"5": "5️⃣", | |
"6": "6️⃣", | |
"7": "7️⃣", | |
"8": "8️⃣", | |
"9": "9️⃣" | |
}; | |
if (!global.BDFDB) global.BDFDB = { | |
myPlugins: {} | |
}; | |
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this; | |
var libraryScript = document.querySelector('head script#BDFDBLibraryScript'); | |
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) { | |
if (libraryScript) libraryScript.remove(); | |
libraryScript = document.createElement("script"); | |
libraryScript.setAttribute("id", "BDFDBLibraryScript"); | |
libraryScript.setAttribute("type", "text/javascript"); | |
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js"); | |
libraryScript.setAttribute("date", performance.now()); | |
libraryScript.addEventListener("load", () => { | |
this.initialize(); | |
}); | |
document.head.appendChild(libraryScript); | |
} else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize(); | |
this.startTimeout = setTimeout(() => { | |
this.initialize(); | |
}, 30000); | |
this.interceptInput = false; | |
this.activeMessage = null; | |
this.lastMessage = []; | |
window.onkeyup = (function(evt) { | |
if (this.interceptInput) { | |
var input = document.querySelector('textarea'); | |
input.focus(); | |
input.selectionStart = 0; | |
input.selectionEnd = input.value.length; | |
document.execCommand("insertText", false, ''); | |
input.selectionStart = input.value.length; | |
input.selectionEnd = input.value.length; | |
var charCode = evt.keyCode || evt.which; | |
if (charCode == 13 || charCode == 27) { | |
this.interceptInput = false; | |
BdApi.showToast( | |
"AddReactionText mode deactivated.", { | |
type: "info" | |
} | |
); | |
return; | |
} else if (charCode == 8) { | |
if (this.lastMessage.length > 0) { | |
this.processMessage(charStr, this.activeMessage, true); | |
return; | |
} | |
} | |
var charStr = String.fromCharCode(charCode); | |
this.processMessage(charStr, this.activeMessage); | |
} | |
}).bind(this); | |
} | |
initialize() { | |
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { | |
if (this.started) return; | |
BDFDB.PluginUtils.init(this); | |
} else console.error(`%c[${this.getName()}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!'); | |
} | |
stop() { | |
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { | |
this.stopping = true; | |
BDFDB.PluginUtils.clear(this); | |
} | |
} | |
onMessageContextMenu(e) { | |
if (e.instance.props && e.instance.props.message && e.instance.props.message.content && e.instance.props.target) { | |
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "add-reaction", group: true}); | |
const itemgroup = BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuGroup, { | |
children: [ | |
BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, { | |
label: "Add text reaction", | |
hint: null, | |
id: BDFDB.ContextMenuUtils.createItemId(this.name, "add-text-reaction"), | |
action: _ => { | |
BDFDB.ContextMenuUtils.close(e.instance); | |
this.initReactionInput(e.instance); | |
} | |
}) | |
] | |
}); | |
if (index > -1) children.splice(index + 1, 0, itemgroup); | |
else children.push(itemgroup); | |
} | |
} | |
onMessageOptionContextMenu(e) { | |
if (e.instance.props.message && e.instance.props.channel) { | |
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "copy-id"}); | |
const copyItem = BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, { | |
label: "Add text reaction", | |
id: BDFDB.ContextMenuUtils.createItemId(this.name, "add-text-reaction-option"), | |
action: _ => { | |
e.instance.props.onClose(); | |
this.initReactionInput(e.instance); | |
} | |
}); | |
children.splice(index + 1, 0, copyItem); | |
} | |
} | |
initReactionInput(instance) { | |
this.lastMessage = []; | |
this.activeMessage = { | |
id: instance.props.message.id, | |
channel: instance.props.message.channel_id | |
}; | |
this.interceptInput = true; | |
BdApi.showToast( | |
"Type to enter reactions, exit this mode with Enter/ESC.\n" + | |
"Use backspace to delete typed reactions.", { | |
type: "info", | |
timeout: 3000 | |
} | |
); | |
} | |
processMessage(input, messageContext, remove) { | |
remove = remove || false; | |
if (remove) { | |
var i = this.lastMessage.pop(); | |
this.removeReaction( | |
messageContext.channel, | |
messageContext.id, | |
this.ri[i] | |
); | |
return; | |
} | |
if (this.ri[input]) { | |
this.lastMessage.push(input); | |
this.addReaction( | |
messageContext.channel, | |
messageContext.id, | |
this.ri[input] | |
); | |
} | |
} | |
addReaction(channelId, messageId, emoji) { | |
BDFDB.LibraryModules.ReactionUtils.addReaction( | |
channelId, | |
messageId, { | |
id: null, | |
name: emoji | |
} | |
); | |
} | |
removeReaction(channelId, messageId, emoji) { | |
BDFDB.LibraryModules.ReactionUtils.removeReaction( | |
channelId, | |
messageId, { | |
id: null, | |
name: emoji | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment