Last active
August 23, 2021 16:07
-
-
Save C10udburst/b7a12bc03a49fbaaca47e9ac42759f5f 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
/** | |
* @name SendEmbeds | |
* @author Cloudburst | |
* @version 2.1.0 | |
* @description Allows you to send embeds | |
* @website https://gist.github.com/C10udburst/b7a12bc03a49fbaaca47e9ac42759f5f | |
* @source https://gist.githubusercontent.com/C10udburst/b7a12bc03a49fbaaca47e9ac42759f5f/raw/dbddc0a912ad2026a15226ea937012197e1573c5/betterSendEmbeds.js | |
*/ | |
const config = { | |
info: { | |
name: 'SendEmbeds', | |
authors: [ | |
{ | |
name: 'Cloudburst', | |
github_username: 'C10udburst' | |
} | |
], | |
version: '1.0', | |
description: 'Allows you to send embeds', | |
github: 'https://gist.github.com/C10udburst/b7a12bc03a49fbaaca47e9ac42759f5f', | |
github_raw: 'https://gist.githubusercontent.com/C10udburst/b7a12bc03a49fbaaca47e9ac42759f5f/raw/dbddc0a912ad2026a15226ea937012197e1573c5/betterSendEmbeds.js' | |
} | |
}; | |
module.exports = class { | |
constructor() { | |
this._config = config; | |
} | |
getName() { | |
return config.info.name; | |
} | |
getAuthor() { | |
return config.info.authors.map(author => author.name).join(', '); | |
} | |
getDescription() { | |
return config.info.description; | |
} | |
getVersion() { | |
return config.info.version; | |
} | |
load() { } | |
unload() { } | |
start() { this.attachHandler(); } | |
onSwitch() { this.attachHandler(); } | |
stop() { | |
let el = document.querySelectorAll('form[class^="form-'); | |
if (el.length == 0) return; | |
// Remove handlers and injected script | |
el[0].removeEventListener('keydown', this.handler); | |
} | |
attachHandler() { | |
this.handler = this.handleKeypress.bind(this); | |
let el = document.querySelectorAll('form[class^="form-'); | |
if (el.length == 0) return; | |
// Bind the handler | |
el[0].addEventListener('keydown', this.handler, false); | |
} | |
// Function that sends the embed | |
sendEmbed(embed) { | |
// Get the ID of the channel we want to send the embed | |
let channelID = BdApi.findModuleByProps('getChannelId').getChannelId(); | |
// Create the message | |
let MessageQueue = BdApi.findModuleByProps('enqueue'); | |
let MessageParser = BdApi.findModuleByProps('createBotMessage'); | |
let msg = MessageParser.createBotMessage(channelID, ''); | |
// Send the message | |
MessageQueue.enqueue({ | |
type: 0, | |
message: { | |
channelId: channelID, | |
content: '', | |
tts: false, | |
nonce: msg.id, | |
embed: embed | |
} | |
}, r => { | |
if (r.ok !== true) { | |
BdApi.showToast(`Message send error: ${r.body.message}`) | |
} | |
}); | |
} | |
// Handling the user input | |
handleKeypress(e) { | |
var code = e.keyCode || e.which; | |
if (code !== 13) { | |
return; | |
} | |
if (e.shiftKey) { | |
return; | |
} | |
// Split a string on only the first delimeter | |
function splitSingle(str, delimeter) { | |
let part1 = str.substr(0, str.indexOf(delimeter)); | |
let part2 = str.substr(str.indexOf(delimeter) + 1); | |
return [part1, part2] | |
}; | |
// Get the deepest child of a parent | |
function getDeepest(elem) { | |
if (elem.firstChild == null) { | |
return elem; | |
} else { | |
return getDeepest(elem.firstChild); | |
} | |
}; | |
// Parse the text | |
let elements = Array.from(document.querySelectorAll('div[class^="textArea-')[0].children[0].children); | |
let text = ''; | |
elements.forEach(function (l0) { | |
Array.from(l0.children).forEach(function (l1) { | |
Array.from(l1.children).forEach(function (elem) { | |
elem = getDeepest(elem); | |
if (elem.alt) { | |
text += elem.alt; | |
} else { | |
text += elem.textContent; | |
} | |
}); | |
}); | |
text += '\n'; | |
}); | |
text = text.replace(/[\s\n]/g, " "); | |
if (!text.startsWith('/e ')) { | |
return; | |
}; | |
// Cancel the event so we can handle it ourselves | |
e.preventDefault(); | |
e.stopPropagation(); | |
// Strip and split the text | |
text = text.replace('\uFEFF', ''); | |
text = text.slice(3) | |
let discordEmbed = {}; | |
try { | |
discordEmbed = JSON.parse(text); | |
} | |
catch (e) { | |
BdApi.showToast(`JSON parsing error: ${e.message}`); | |
this.lastKey = 0; | |
return; | |
} | |
if (discordEmbed.color !== undefined | |
&& discordEmbed.color.match !== undefined | |
&& discordEmbed.color.match(/^#?[0-9A-Z]{6}/gi) !== undefined) { | |
discordEmbed.color = discordEmbed.color.replace(/#/g, ""); | |
discordEmbed.color = parseInt(discordEmbed.color, 16) | |
} | |
console.log({ discordEmbed }) | |
this.sendEmbed(discordEmbed); | |
this.lastKey = 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment