I
I
Ilya Zholudev2021-03-23 16:02:46
JavaScript
Ilya Zholudev, 2021-03-23 16:02:46

Why can't the Discord bot change the message?

I have commands like /question; /suggestion; /bug; All of these commands send messages to the same channels as their name. I think we've cleared that up. The commands themselves work fine, but I also have the /edit command, as you understand, it changes the message.

Let's analyze the /question and /edit commands,
all commands except /edit create an ID for themselves, and for this I write them back to the local file.

Syntax:
/question -q Text
/edit ID -q Text

Now the code itself:

if(message.content.startsWith("/edit")) {
        deleteLastM(); 
        var tokens = message.content.split(" ");
        const MTid = tokens[1];
        if (MTid == null || MTid == undefined) return message.author.send("Будьте добры указать ID сообщения, которого вы пытаетесь изменить.");
        else if (db.get(MTid)) {
            const Msg = db.get(MTid);

            const RID = parseInt(MTid, 10);

            async function EditMessage() {
                
            let someserver = client.guilds.cache.get('686225794766209088');
            let somechannel = someserver.channels.cache.get(Msg.channelid);
            let somemessage = await somechannel.messages.fetch(Msg.realid);

           if (Msg.type == "question") {
            const Color = Msg.color;
            const questions = message.content.split("-q");

            var questionembed = new Discord.MessageEmbed()
            .setTitle("Вопрос")
            .setAuthor(Author, MyAvatar)
            .setColor(`${Color}`)
            .setFooter(`#${RID}`)
            .addFields(
             {name: "\u200B", value: `${questions[1]}`}
            )
            db.set(`${RID}`, {realid: `${Msg.realid}`, type: "question", channelid: `${Msg.channelid}`, color: `${Color}`, text: `${questions[1]}`,
            userpostid: `${Msg.userpostid}`, author: `${Msg.author}`, authoravatar: `${Msg.authoravatar}`});

            somemessage.edit(questionembed);
            message.channel.send("Изменения успешны!").then(msg => {
                msg.delete({timeout: 10000})
            });
           }

            EditMessage();
        }
        
    }


Now, let's look at some points:
deleteLastM() - immediately after the command was noticed, I delete the author's message;
MTid - after the first space, according to the syntax, the message ID should go;
db.get(MTid) - I get a message by id from the local file, before checking whether I can get it.
RID - I turn MTid into a number, because it is saved in a local file as a string

Also, if you are interested, here is the code of the command itself:
if (server = '686225794766209088' && message.content.startsWith("/question"))
    { 
        var GetLastMID = GetNewID();

        const Color = RColor();
        const text = message.content.split("-q");

        deleteLastM();
        var embed = new Discord.MessageEmbed()
           .setTitle("Вопрос")
           .setAuthor(Author, MyAvatar)
           .setColor(`${Color}`)
           .setFooter(`#${GetLastMID}`)
           .addFields(
            {name: "\u200B", value: `${text[1]}`}
           )

        message.channel.send(embed).then(msg => {
            msg.delete({timeout: 60000})
        }
        );
        client.channels.cache.find(e=>e.name == "questions")
        .send(embed).then( question =>
            db.set(`${GetLastMID}`, {realid: `${question.id}`, type: "question", channelid:`${message.channel.id}`, color: `${Color}`, text: `${text[1]}`, 
            userpostid: `${user_id}`, author: `${Author}`, authoravatar: `${MyAvatar}`})
        );

In short, here I save what I wrote, send an embed to the same channel, then delete it after a minute, and also send the same message to a channel called "bugs".

The main question is:
Where is the error? Why doesn't the code work?

If you are interested in an error, then the console writes DiscordAPI: Unknown Message
And the code itself stops at this point:
let somemessage = await somechannel.messages.fetch(Msg.realid);


Can you point out my mistake?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question