[英]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.好的,我是 JS 和 Discord.js 的新手,上周我开始编写一个小型的 Discord 机器人。
I want my bot to be able to delete all messages older than 6 days in a specific channel.我希望我的机器人能够删除特定频道中超过 6 天的所有消息。
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 1 - 我不能使用message.channel.bulkDelete()
因为它不能删除超过 14 天的消息
2 - message.channel.messages.fetch()
can only fetch a maximum of 100 messages 2 - message.channel.messages.fetch()
最多只能获取 100 条消息
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.这是因为我的let TargetedMsgs
超出了范围,因为它超出了我的do
块。
The thing is, how can I accomplish what I want to do?问题是,我怎样才能完成我想做的事? Using a for loop?使用for循环? I don't know how to do it.我不知道该怎么做。
I would gladly take your advices!我很乐意接受你的建议!
Thanks in advance!提前致谢! :) :)
OK so!好吧! Thanks to the help of @ibrahimmahrir who put me on the right track, I figured out how to resolve my main issue!感谢@ibrahimmahrir 的帮助,他让我走上了正轨,我找到了解决主要问题的方法!
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.问题是fetch()
方法是异步的,我需要等待(使用await运算符)才能删除目标消息。 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!再次感谢 Ibrahim,如果您阅读了本文!
You can try Const this variable,你可以试试 Const 这个变量,
const thing = 0;常量事物 = 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.