Skip to content

Commit 7a71cfb

Browse files
committed
hotfix status update check failing when config. file is not found while getting message ids
1 parent 3ddb3e9 commit 7a71cfb

File tree

1 file changed

+71
-31
lines changed

1 file changed

+71
-31
lines changed

src/tasks/status_update.rs

+71-31
Original file line numberDiff line numberDiff line change
@@ -139,38 +139,78 @@ async fn send_and_save_limiting_messages(
139139
async fn collect_updates(channel_ids: &[ChannelId], ctx: &Context) -> anyhow::Result<Vec<Message>> {
140140
trace!("Collecting updates");
141141
let mut valid_updates: Vec<Message> = vec![];
142-
let message_ids = get_msg_ids()?;
143-
let now = chrono::Local::now().with_timezone(&chrono_tz::Asia::Kolkata);
144-
let today_five_am = chrono::Local
145-
.with_ymd_and_hms(now.year(), now.month(), now.day(), 5, 0, 0)
146-
.earliest()
147-
.expect("Failed to create 5 AM timestamp");
148-
let yesterday_five_pm = today_five_am - chrono::Duration::hours(12);
149-
for (&channel_id, &msg_id) in channel_ids.iter().zip(message_ids.iter()) {
150-
let messages = channel_id
151-
.messages(
152-
&ctx.http,
153-
serenity::builder::GetMessages::new()
154-
.after(msg_id)
155-
.limit(100),
156-
)
157-
.await
158-
.with_context(|| anyhow!("Failed to get messages from channel {}", channel_id))?;
159-
160-
debug!("Messages: {:?}", messages);
161-
valid_updates.extend(messages.into_iter().filter(|msg| {
162-
let content = msg.content.to_lowercase();
163-
(content.contains("namah shivaya")
164-
&& content.contains("regards")
165-
&& msg.timestamp >= yesterday_five_pm.into())
166-
|| (content.contains("regards")
167-
&& msg.author.name == "amanoslean"
168-
&& msg.timestamp >= yesterday_five_pm.into())
169-
}));
170-
}
142+
let message_ids = get_msg_ids();
143+
match message_ids {
144+
Ok(message_ids) => {
145+
let now = chrono::Local::now().with_timezone(&chrono_tz::Asia::Kolkata);
146+
let today_five_am = chrono::Local
147+
.with_ymd_and_hms(now.year(), now.month(), now.day(), 5, 0, 0)
148+
.earliest()
149+
.expect("Failed to create 5 AM timestamp");
150+
let yesterday_five_pm = today_five_am - chrono::Duration::hours(12);
151+
for (&channel_id, &msg_id) in channel_ids.iter().zip(message_ids.iter()) {
152+
let messages = channel_id
153+
.messages(
154+
&ctx.http,
155+
serenity::builder::GetMessages::new()
156+
.after(msg_id)
157+
.limit(100),
158+
)
159+
.await
160+
.with_context(|| anyhow!("Failed to get messages from channel {}", channel_id))?;
161+
162+
debug!("Messages: {:?}", messages);
163+
valid_updates.extend(messages.into_iter().filter(|msg| {
164+
let content = msg.content.to_lowercase();
165+
(content.contains("namah shivaya")
166+
&& content.contains("regards")
167+
&& msg.timestamp >= yesterday_five_pm.into())
168+
|| (content.contains("regards")
169+
&& msg.author.name == "amanoslean"
170+
&& msg.timestamp >= yesterday_five_pm.into())
171+
}));
172+
}
171173

172-
debug!("Valid updates: {:?}", valid_updates);
173-
Ok(valid_updates)
174+
debug!("Valid updates: {:?}", valid_updates);
175+
Ok(valid_updates)
176+
177+
},
178+
Err(e) => {
179+
debug!("Failed to get message_ids {}. Defaulting to default GetMessages()", e);
180+
let now = chrono::Local::now().with_timezone(&chrono_tz::Asia::Kolkata);
181+
let today_five_am = chrono::Local
182+
.with_ymd_and_hms(now.year(), now.month(), now.day(), 5, 0, 0)
183+
.earliest()
184+
.expect("Failed to create 5 AM timestamp");
185+
let yesterday_five_pm = today_five_am - chrono::Duration::hours(12);
186+
for &channel_id in channel_ids {
187+
let messages = channel_id
188+
.messages(
189+
&ctx.http,
190+
serenity::builder::GetMessages::new()
191+
.limit(100),
192+
)
193+
.await
194+
.with_context(|| anyhow!("Failed to get messages from channel {}", channel_id))?;
195+
196+
debug!("Messages: {:?}", messages);
197+
valid_updates.extend(messages.into_iter().filter(|msg| {
198+
let content = msg.content.to_lowercase();
199+
(content.contains("namah shivaya")
200+
&& content.contains("regards")
201+
&& msg.timestamp >= yesterday_five_pm.into())
202+
|| (content.contains("regards")
203+
&& msg.author.name == "amanoslean"
204+
&& msg.timestamp >= yesterday_five_pm.into())
205+
}));
206+
}
207+
208+
debug!("Valid updates: {:?}", valid_updates);
209+
Ok(valid_updates)
210+
211+
212+
}
213+
}
174214
}
175215

176216
fn get_msg_ids() -> anyhow::Result<Vec<MessageId>> {

0 commit comments

Comments
 (0)