Created
April 10, 2020 23:34
-
-
Save DarkSeraphim/a1f6697cf4a5ea2a67151dbb0265be08 to your computer and use it in GitHub Desktop.
Discord.js live embed builder
This file contains hidden or 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
const { RichEmbed, MessageEmbed } = require('discord.js'); | |
const Embed = RichEmbed || MessageEmbed; | |
const commands = { | |
'title': (embed, input) => embed.setTitle(input), | |
'description' (embed, input) => embed.setDescription(input), | |
// etc | |
} | |
async function create(user, channel) { | |
let embed = new Embed(); | |
let message = await channel.send(embed); | |
while (true) { | |
let input = await getInput(user, channel); | |
if (!input) { | |
throw new Error('Timed out waiting for user'); | |
} | |
if (input === 'done') { | |
return embed; | |
} | |
const [command, rest] = splitN(input, ' ', 2); | |
const func = commands[command]; | |
if (!func) { | |
continue; // not a command | |
} | |
embed = func(embed, ); | |
message = await message.edit(embed); | |
} | |
channel.send() | |
} | |
async function getInput(user, channel) { | |
return (await channel.awaitMessages(m => m.author.id === user.id, {max: 1})).first(); | |
} | |
function splitN(haystack, needle, n) { | |
if (n <= 0) { | |
throw new Error('n must be positive'); | |
} | |
const result = []; | |
let idx = haystack.indexOf(needle); | |
while (--n && idx !== -1) { | |
result.push(haystack.slice(0, idx); | |
idx = haystack.indexOf(needle, idx + 1); | |
} | |
if (haystack) { | |
result.push(haystack); | |
} | |
return result; | |
} | |
module.exports = create; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment