I
I
Ilya Zholudev2021-03-03 18:15:52
Node.js
Ilya Zholudev, 2021-03-03 18:15:52

Why is the Discord bot message not changing?

Good day, I've been struggling with the code for 2 days, and I decided to turn here for help.
Let's talk a little about the bot, it already has 100% working /bug and /edit commands, the first one creates a message and sends it to a channel where others are denied access, but if someone made mistakes, do you need to fix it somehow? There is a /edit command for this, you understand the essence. I want to say that for the /edit command to work, I save the data to a local file.

I started working on the /repeatto command, its essence is to send a copy of the message to another channel. This is where the interesting moment begins, here is the code for the /repeatto command for a rough understanding of what I'm doing:

if(message.content.startsWith("/repeatto")) { 
        deleteLastM();
        if (!message.mentions.channels.first()) return message.author.send("Укажите один канал");
        if (message.mentions.channels.size == 1){
            if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.author.send("У вас недостаточно прав"), deleteLastM();
            const textrepeat = TRepeat();
            const repeatname = TRepeatName();
            var ID = GetNewID();
            const Mchannel = message.mentions.channels.first().id;
            
            var embed = new Discord.MessageEmbed()
           .setColor('#000000')
           .setFooter(`#${ID}`)
           .setTitle(`${repeatname}`)
           .addField("\u200B", `${textrepeat}`)
            
           client.channels.cache.get(Mchannel)
            .send(embed).then( repeat =>
            db.set(`${ID}`, {realid: `${repeat.id}`, type: "repeated", channelid:`${Mchannel}`, rname:`${repeatname}`, rtext: `${textrepeat}`})
            );
        }
    }


This is how my code looks, quite simply, but still I will explain: In the command, the first thing is to check for the mentioned channels, if there are none, an error is generated, if there is a mentioned channel, I get its ID and save it along with the message, after which it is sent.

Finally, it was the turn of the mistake itself. Of course, I made it so that the text could be changed, here somewhere my mistake lies:
if(server = '686225794766209088' && 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.has(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 == "repeated"){

            const ETrepeatname = Msg.rname
            const ETrepeat = Msg.rtext

            Erepeatname = function() {
                if (message.content.includes("-n")) return TRepeatName()
                else return ETrepeatname 
            }
            Erepeattext = function() {
                if (message.content.includes("-r")) return TRepeat() 
                else return ETrepeat
            }

            const dbErepeatname = Erepeatname();
            const dbErepeat = Erepeattext();

            var repeatedembed = new Discord.MessageEmbed()
           .setColor('#000000')
           .setFooter(`#${RID}`)
           .setTitle(`${dbErepeatname}`)
           .addField("\u200B", `${dbErepeat}`)
           db.set(`${RID}`, {realid: `${Msg.realid}`, type: "repeated", channelid:`${Msg.chennelid}`, rname: `${dbErepeatname}`, rtext: `${dbErepeat}`});
           somemessage.edit(repeatedembed);
           message.channel.send("Изменения успешны!").then(msg => {
            msg.delete({timeout: 10000})
            });
           }
            }
            EditMessage();
        }        
    }


Let's talk a little about what's wrong. When I write the /repeatto command for the first time, everything happens as it should, if I want to change something, I write the /edit command, everything also goes smoothly, but as soon as I repeat the /edit command, there are two options for events:

An error appears here:
let someserver = client.guilds.cache.get('686225794766209088');
let somechannel = someserver.channels.cache.get(Msg.channelid);
let somemessage = await somechannel.messages.fetch(Msg.realid); 
// < TypeError: Cannot read property 'messages' of undefined


And there is also an outcome where the Discord API error is mentioned.

Can you point out my mistakes in the code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Zholudev, 2021-03-08
@Astroreen

So I found the answer, I did not save the channel correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question