Skip to content

Commit 4c9c695

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

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ GNU General Public License for more details.
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
18+
use anyhow::Context as _;
1819
use tracing::{info, trace};
1920
use tracing_subscriber::EnvFilter;
20-
use anyhow::Context as _;
2121

2222
use crate::{Context, Data, Error};
2323

src/tasks/status_update.rs

Lines changed: 71 additions & 31 deletions
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(|| {
161+
anyhow!("Failed to get messages from channel {}", channel_id)
162+
})?;
163+
164+
debug!("Messages: {:?}", messages);
165+
valid_updates.extend(messages.into_iter().filter(|msg| {
166+
let content = msg.content.to_lowercase();
167+
(content.contains("namah shivaya")
168+
&& content.contains("regards")
169+
&& msg.timestamp >= yesterday_five_pm.into())
170+
|| (content.contains("regards")
171+
&& msg.author.name == "amanoslean"
172+
&& msg.timestamp >= yesterday_five_pm.into())
173+
}));
174+
}
171175

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

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

0 commit comments

Comments
 (0)