JusteThom
JusteThom

Reputation: 120

Variable scope in do while loop

Ok so, I'm new to JS and Discord.js and I started to write a small discord bot last week.

I want my bot to be able to delete all messages older than 6 days in a specific channel.

The main issues I have to face are :

1 - I can't use message.channel.bulkDelete() because it can't delete messages older than 14 days

2 - message.channel.messages.fetch() can only fetch a maximum of 100 messages

I wrote this code :

    try {
    if (message.channel.id === '<TheChannelID>') {
        do {
            let TargetedMsgs = 0 // Initializing my counter of the messages that meet my date condition
            message.channel.messages.fetch().then(messages => { // fetching the messages in the channel (100 max)
                messages.forEach(message => { // loop through each message in the fetched messages
                    let Today = new Date()
                    let TargetDate = new Date(Today.setDate(Today.getDate()-6))
                    if (message.createdAt < TargetDate) { // the message is older than 6 days
                        message.delete()
                        TargetedMsgs ++ // There is at least on message that meets the condition in this fetch batch
                    }
                })
            })
        }
        while (
            TargetedMsgs > 0
        )
    }
}
catch (err){
    console.error(err)
}  

I get :

ReferenceError: TargetedMsgs is not defined

In my while condition.

I understand why. It's because my let TargetedMsgs is out of scope because it's out of my do block.

The thing is, how can I accomplish what I want to do? Using a for loop? I don't know how to do it.

I would gladly take your advices!

Thanks in advance! :)

Upvotes: 0

Views: 1046

Answers (2)

JusteThom
JusteThom

Reputation: 120

OK so! Thanks to the help of @ibrahimmahrir who put me on the right track, I figured out how to resolve my main issue!

The thing is the fetch() method is asynchronous and I needed to wait (with the await operator) for it to be able to delete the targeted messages. I also change how I filter the messages to retrieve only the ones I want.

Here is the code :

if (message.channel.id === config.MyChannelID) {
    try {
        let TargetedMsgsCount // Initializing my variable which I use in my do/while loop
        do {
            TargetedMsgsCount = 0
            await message.channel.messages.fetch({limit:100}).then(messages => {
                let Today = new Date()
                let TargetDate = new Date(Today.setDate(Today.getDate()-6))
                let TargetedMsgs = messages.filter(msg => msg.createdAt < TargetDate)
                TargetedMsgs.forEach(TargetedMsg => {
                    TargetedMsg.delete()
                    TargetedMsgsCount ++ // So if there is at least one message that match my date condition, there is maybe more
                })
            })
        }
        while (
            TargetedMsgsCount > 0
        )

With that, my loop is now OK!

I have another issue now but that was not my main question and it would be out of topic to ask here so I consider this question closed!

Thanks again to Ibrahim if you read this!

Upvotes: 2

Globallizado
Globallizado

Reputation: 1

You can try Const this variable,

const thing = 0;

or make it inside the block you need,

messages.forEach(message => { // loop through each message in the fetched messages

                    let Today = new Date()

                    let TargetedMsgs = 0

                    let TargetDate = new Date(Today.setDate(Today.getDate()-6))

                    if (message.createdAt < TargetDate) { // the message is older than 6 days
                        message.delete()

                        TargetedMsgs ++ // There is at least on message that meets the condition in this fetch batch

                    }

Upvotes: 0

Related Questions