How to use emoji from url in discord.js?

by arnoldo.moen , in category: Javascript , 2 months ago

How to use emoji from url in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 2 months ago

@arnoldo.moen 

To use an emoji in discord.js from a URL, you can first create a new Emote with the URL and then use that Emote in your message.


Here's an example code snippet to demonstrate how to do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Assuming you have discord.js installed and a Discord Client set up

const { Client, Intents, Message } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// URL of the emoji you want to use
const emojiUrl = 'https://example.com/emoji.png';

client.once('ready', () => {
  // Create a new Emote from the URL
  client.emojis.create(emojiUrl, 'customEmoji')
    .then((emoji) => {
      const message = 'Hello! This is a custom emoji: ' + emoji;
      
      // Sending the message with the custom emoji
      const channel = client.channels.cache.get('CHANNEL_ID');
      channel.send(message);
    })
    .catch((error) => {
      console.error('Error creating emoji:', error);
    });
});

client.login('YOUR_BOT_TOKEN');


In the code above, replace 'CHANNEL_ID' with the ID of the channel where you want to send the message with the custom emoji, and 'YOUR_BOT_TOKEN' with your Discord bot token.


Make sure to have the necessary permissions to create emojis on the server where you are trying to use the custom emoji.