-
-
Save capJavert/fec7156378e5bc0d95cbf954b2fcb999 to your computer and use it in GitHub Desktop.
Discord commands for custom emojis
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
const emojiNameMatcher = /^[a-z0-9_]+$/ | |
const unsupportedImageFormats = ['image/webp'] | |
const command = async (interaction, client) => { | |
let deffered = false | |
const reply = async (...args) => { | |
if (deffered) { | |
await interaction.editReply(...args) | |
} else { | |
await interaction.reply(...args) | |
} | |
} | |
try { | |
const emojiName = interaction.options.get('name', true).value | |
if (!emojiNameMatcher.test(emojiName)) { | |
return await reply({ content: 'Name can only be letter, number or underscore...' }) | |
} | |
const { attachment } = interaction.options.get('image', true) | |
const fit = interaction.options.get('fit', false)?.value || 'cover' | |
if (!attachment.url || !attachment.contentType.startsWith('image/')) { | |
return await reply({ content: 'Attachment is not an image...' }) | |
} | |
if (unsupportedImageFormats.includes(attachment.contentType)) { | |
return await reply({ content: 'webp format is not supported, use png or jpeg...' }) | |
} | |
const emojiImageParams = new URLSearchParams() | |
emojiImageParams.append('image', attachment.url) | |
emojiImageParams.append('fit', fit) | |
await interaction.deferReply({ ephemeral: true, fetchReply: true }) | |
deffered = true | |
interaction.guild.emojis.cache.clear() | |
await interaction.guild.emojis.fetch() | |
const existingEmoji = interaction.guild.emojis.cache.find(emoji => emoji.name === emojiName) | |
if (existingEmoji) { | |
return await reply({ content: 'Emoji already exists...' }) | |
} | |
// /discord/emoji is an example, you should adjust this to your own API/route | |
const emojiImage = await fetch(`/discord/emoji?${emojiImageParams.toString()}`, {}).then(res => | |
res.arrayBuffer() | |
) | |
const emoji = await interaction.guild.emojis.create({ | |
name: emojiName, | |
attachment: Buffer.from(emojiImage) | |
}) | |
await reply({ content: `Done! You can use new emoji with :${emoji.name}:` }) | |
} catch (error) { | |
console.error('[interactions][create-emoji error]', error.message) | |
reply({ content: 'Something went wrong...' }) | |
} | |
} | |
export default command |
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
const emojiNameMatcher = /^[a-z0-9_]+$/ | |
const command = async (interaction, client) => { | |
let deffered = false | |
const reply = async (...args) => { | |
if (deffered) { | |
await interaction.editReply(...args) | |
} else { | |
await interaction.reply(...args) | |
} | |
} | |
try { | |
const emojiName = interaction.options.get('name', true).value | |
if (!emojiNameMatcher.test(emojiName)) { | |
return await reply({ content: 'Name can only be letter, number or underscore...' }) | |
} | |
await interaction.deferReply({ ephemeral: true, fetchReply: true }) | |
deffered = true | |
interaction.guild.emojis.cache.clear() | |
await interaction.guild.emojis.fetch() | |
const emoji = interaction.guild.emojis.cache.find(emoji => emoji.name === emojiName) | |
if (!emoji) { | |
return await reply({ content: 'Emoji does not exist...' }) | |
} | |
await interaction.guild.emojis.delete(emoji.id) | |
await reply({ content: `Emoji is removed!` }) | |
} catch (error) { | |
console.error('[interactions][remove-emoji error]', error.message) | |
reply({ content: 'Something went wrong...' }) | |
} | |
} | |
export default command |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment