Skip to content

Instantly share code, notes, and snippets.

@izy521
Last active November 12, 2021 06:14
Show Gist options
  • Save izy521/5447b5e483d6b41729ec6ede28325872 to your computer and use it in GitHub Desktop.
Save izy521/5447b5e483d6b41729ec6ede28325872 to your computer and use it in GitHub Desktop.
Use CTRL + Enter to send a Discord Embedified message. You'll need to provide your account token to the code, on line 3. Will be removed if you refresh. Highlighting a selection of your message will let you send only that as the embed, and the rest as a normal message.
(function start_embed_generator(window, document) {
var my_token = "-YOUR_TOKEN_HERE-";
var color_reg = /(\#.+?)(\s|$)/;
var text_bar_color = "rgb(92, 156, 199)";
window.addEventListener('keydown' , handle_event);
window.addEventListener('keyup' , revert_bar );
return !console.log("Embed generator up!");
function handle_event(event) {
var embed, text_bar, text_area, highlighted, not_highlighted;
if (!event.ctrlKey) return;
text_bar = document.getElementsByClassName('channel-textarea-inner')[0];
if (!text_bar) return;
if (!text_bar.style.borderColor) text_bar.style.borderColor = text_bar_color;
if (event.keyCode !== 13) return;
text_area = text_bar.children[1];
if (!text_area || text_area.value === "") return;
if (text_area.selectionStart !== text_area.selectionEnd) {
highlighted = text_area.value.slice(text_area.selectionStart, text_area.selectionEnd);
not_highlighted = text_area.value.replace(highlighted, "");
}
send_request(resolve_channel_id(), format_embed(highlighted || text_area.value), not_highlighted);
return void(text_area.value = "");
}
function send_request(channelID, embed, content) {
var url = `https://discordapp.com/api/v6/channels/${channelID}/messages`;
var opts = {
method: 'POST',
headers: {"Authorization": my_token, "Content-Type": 'application/json'},
body: JSON.stringify({ content: content || "", embed: embed })
};
return fetch(url, opts).catch(console.error);
}
function format_embed(string) {
var match, color = null;
if ( match = string.match(color_reg) ) {
color = parseInt(match[0].slice(1, match[0].length), 16);
string = string.replace(match[0], "");
}
return {
description: string,
color: color || 0,
}
}
function resolve_channel_id() {
var path = window.location.pathname;
return path.slice( path.lastIndexOf("/") + 1, path.length );
}
function revert_bar(event) {
if (event.keyCode !== 17) return;
var text_bar = document.getElementsByClassName('channel-textarea-inner')[0];
if (!text_bar) return;
return text_bar.style.borderColor ? void(text_bar.style.borderColor = "") : false;
}
})(window, document);
@MiningMark48
Copy link

This. Awesome. 👍

@Cynosphere
Copy link

Bad hex color regex.

@izy521
Copy link
Author

izy521 commented Feb 7, 2017

I don't really know regex, could you explain what you'd do better @LUModder?

@AviKav
Copy link

AviKav commented Feb 7, 2017

This will help keep the heap clean and allow future versions of the snippetto be loaded without refresh

function cleanup() {
        window.removeEventListener('keypress', handle_event);
    }
try {embedGlobalCleanup();
    } catch(event) {}
    embedGlobalCleanup= cleanup;

@AviKav
Copy link

AviKav commented Feb 7, 2017

This limits the regex to the very beginning and end of a message. Otherwise, you run the risk of accidentally capturing something that wasn't intended to be captured.

/*
Code...
*/

var color_reg = /((^(#[0-9a-fA-F]{3}){1,2})|((#[0-9a-fA-F]{3}){1,2}$)){1}/;

/*
...More code...
*/

function format_embed(string) {
        var match, color = null;
        if ( match = string.match(color_reg) ) {
            color = parseInt(match[0].slice(1, match[0].length), 16);
            string = string.replace(match[0], "");
        }

/*
...The last of the code
*/

@SharkFinPro
Copy link

Nice!

Copy link

ghost commented Feb 8, 2018

not working anymore. try pressing ctrl+enter but its not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment