will
will

Reputation: 71

discord.js - put code into embed

I have the following code:

    ("...............🚗......").then(sentMessage => sentMessage.edit("............🚗.........")).then(sentMessage => sentMessage.edit("........🚗............")).then(sentMessage => sentMessage.edit(".....🚗...............")).then(sentMessage => sentMessage.edit("..🚗..................")).then(sentMessage => sentMessage.edit("🚗....................")).then(sentMessage => sentMessage.edit("😮....🚗..............")).then(sentMessage => sentMessage.edit(".😧..🚗......................")).then(sentMessage => sentMessage.edit("..:tired_face:.:red_car:......................")).then(sentMessage => sentMessage.edit("...:ghost::red_car:......................")

How can I put this into a discord.js embed with the following code:

    message.channel.send({
        "embed": {
          "title": "Car",
          "description": - i want the above code to be here -,
          "color": 16763981,
          "footer": {
            "text": "Have a fun ride!"
          }
        }
      })
     }

Is this possible in discord.js? If so, please help me out! Have no clue how to achieve this.

:) Will

Upvotes: 0

Views: 3422

Answers (4)

GabRio Blu
GabRio Blu

Reputation: 85

Got it! What I did to solve it was on startup, to grab the right channel using channel = client.user.guilds.cache.get("Guild id here").channels.cache.get("channel id"), built an embed that just said old, and then sent the embed.

I did include an array of animation steps like Emil Choparinov, and a msgProgress variable. The bot detects when a message is sent, and checks if (msg.content === ''). if true, it will set the recievedEmbed constant to msg.embeds[0].

Then, a new const, embed, is set to a new Discord.MessageEmbed, using the old embed as a starting point, and setting the title to animationSteps[msgProgress]. It then calls msg.edit(embed), and changes the msgProgress variable by 1.

There is also a client.on('messageUpdate', msg => {}), and it has the same code, except at the start, it checks if msg progress > 9, and if so, it returns. Here is the code:

require('dotenv').config();

const Discord = require('discord.js');
const client = new Discord.Client();

var channel;

const genericEmbed = new Discord.MessageEmbed()
    .setTitle("old");

const animationSteps = [
    "...............:red_car:......",
    "............:red_car:.........",
    "........:red_car:............",
    ".....:red_car:...............",
    "..:red_car:..................",
    ":red_car:....................",
    ":open_mouth:....:red_car:..............",
    ".:cold_sweat:..:red_car:......................",
    "..:tired_face:.:red_car:......................",
    "...:ghost::red_car:......................"
];

var msgProgress = 0;

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
    channel = client.guilds.cache.get("753227876207165570").channels.cache.get("753227876207165573");
    console.log(channel);
    const firstEmbed = new Discord.MessageEmbed()
        .setTitle("old");

    channel.send(firstEmbed);
});

client.on('message', msg => {
    if (msg.content === '') {
        console.log("good");
        channel = msg.channel;

        const receivedEmbed = msg.embeds[0];
        const embed = new Discord.MessageEmbed(receivedEmbed)
            .setTitle(animationSteps[msgProgress]);

        msg.edit(embed);

        msgProgress++;
    }
});

client.on('messageUpdate', msg => {
    if (msgProgress > 9) {
        return;
    }

    if (msg.content === '') {
        console.log("good");
        channel = msg.channel;
        const receivedEmbed = msg.embeds[0];
        const embed = new Discord.MessageEmbed(receivedEmbed)
            .setTitle(animationSteps[msgProgress]);
        msg.edit(embed);

        msgProgress++;
    }
});

client.login(process.env.DISCORD_TOKEN);

Hope this helped! 😃

Upvotes: 0

Emil Choparinov
Emil Choparinov

Reputation: 11

If I'm understanding you correctly, you want to send the first snippet of code into the description field and edit it, trying to make it be an animation?

I haven't tried editing an embedded message before but this is how I would go around it.

const sendCarAnimation = async (message) => {

    // define the steps here
    const animationSteps = [
        "...............🚗......",
        "............🚗.........",
        "........🚗............",
        ".....🚗...............",
        "..🚗..................",
        "🚗....................",
        "😮....🚗..............",
        ".😧..🚗......................",
        "..:tired_face:.:red_car:......................",
        "...:ghost::red_car:......................"
    ];

    // generate an embed using the RichEmbed functionality
    const embed = new Discord.RichEmbed()
        .setTitle('Car')
        .setDescription(animationSteps[0])
        .setColor(16763981)
        .setFooter('Have a fun ride!')

    // initialize the message with the first embed
    let messageState = await message.channel.send(embed);


    // loop through and edit the message 
    let isFirst = true;
    for(let currentStep of animationSteps) {
        if(isFirst) {
            isFirst = false;
            continue;
        }
        embed.setDescription(currentStep);
        messageState = await messageState.edit(embed);
    }
}

NOTE: This will take a lot of requests to do and you will most likely get rate limited by discord for doing this. So I don't think this is a good idea. Here's their documentation on it. You can probably pull off some tricky code using the Discord.js's

client.on('rateLimit', (rateLimitInfo) => {});

event. Documentation link to this as well. Good luck!

Upvotes: 1

NintendoZaedus
NintendoZaedus

Reputation: 693

Instead of deleting it and sending it again you can create a variable of the lastMessage (you might have to make a delay before selecting it) then do message.edit()

Upvotes: 0

nook
nook

Reputation: 1884

I don't know what exactly you are trying to do. I guess what you made is an animation, if not, and you just want to print litterally this piece of code in your embed, just put this piece of code inside backticks

description: `("...............🚗......")
  .then(sentMessage => sentMessage.edit("............🚗........."))
  .then(sentMessage => sentMessage.edit("........🚗............"))
  .then(sentMessage => sentMessage.edit(".....🚗..............."))
  .then(sentMessage => sentMessage.edit("..🚗.................."))
  .then(sentMessage => sentMessage.edit("🚗...................."))
  .then(sentMessage => sentMessage.edit("😮....🚗.............."))
  .then(sentMessage => sentMessage.edit(".😧..🚗......................"))
  .then(sentMessage => sentMessage.edit("..:tired_face:.:red_car:......................"))
  .then(sentMessage => sentMessage.edit("...:ghost::red_car:......................")`,

Then it looks like this : enter image description here

If you want to make an animation, you're gonna have to use the bot to delete and rewrite the embed for each step of your animation (You can't just edit an embed if I'm not wrong)

Try to be more specific on what you really want to display

Upvotes: 1

Related Questions