Skip to content

Commit caf2c5b

Browse files
authored
Merge pull request #325 from Aras14HD/revert-monitor-disconnect
2 parents adba631 + 29d5a14 commit caf2c5b

File tree

6 files changed

+59
-58
lines changed

6 files changed

+59
-58
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

factorion-bot-discord/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "factorion-bot-discord"
3-
version = "2.2.3"
3+
version = "2.2.4"
44
edition = "2024"
55
description = "factorion-bot (for factorials and related) on Discord"
66
license = "MIT"
@@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math", "discord"]
1010
categories = ["mathematics", "web-programming", "parser-implementations"]
1111

1212
[dependencies]
13-
factorion-lib = { path = "../factorion-lib", version = "4.2.3", features = ["serde", "influxdb"] }
13+
factorion-lib = { path = "../factorion-lib", version = "4.2.4", features = ["serde", "influxdb"] }
1414
serenity = { version = "0.12", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache"] }
1515
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "time"] }
1616
dotenvy = "^0.15.7"

factorion-bot-reddit/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "factorion-bot-reddit"
3-
version = "5.3.3"
3+
version = "5.3.4"
44
edition = "2024"
55
description = "factorion-bot (for factorials and related) on Reddit"
66
license = "MIT"
@@ -10,7 +10,7 @@ keywords = ["factorial", "termial", "bot", "math"]
1010
categories = ["mathematics", "web-programming", "parser-implementations"]
1111

1212
[dependencies]
13-
factorion-lib = {path = "../factorion-lib", version = "4.2.3", features = ["serde", "influxdb"]}
13+
factorion-lib = {path = "../factorion-lib", version = "4.2.4", features = ["serde", "influxdb"]}
1414
reqwest = { version = "0.12.28", features = ["json", "native-tls"], default-features = false }
1515
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
1616
serde_json = "1.0.140"

factorion-bot-reddit/src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use factorion_lib::{
77
locale::Locale,
88
rug::{Complete, Integer, integer::IntegerExt64},
99
};
10-
use log::{error, info, warn};
10+
use log::{debug, error, info, warn};
1111
use reddit_api::RedditClient;
1212
use reddit_api::id::DenseId;
1313
use serde::{Deserialize, Serialize};
@@ -224,7 +224,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
224224

225225
// Polling Reddit for new comments
226226
for i in (0..u8::MAX).cycle() {
227-
info!("Polling Reddit for new comments...");
227+
debug!("Polling Reddit for new comments...");
228228
let mut thread_calcs_changed = false;
229229

230230
let start = SystemTime::now();
@@ -277,7 +277,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
277277
continue;
278278
}
279279
let Ok(mut dense_id) = u64::from_str_radix(&comment.meta.thread, 36) else {
280-
warn!("Failed to make id dense {}", comment.meta.thread);
280+
if comment.meta.thread == "" {
281+
info!("Empty thread id on comment {}", comment.meta.id);
282+
} else {
283+
warn!("Failed to make id dense {}", comment.meta.thread);
284+
}
281285
continue;
282286
};
283287
dense_id |= 3 << 61;
@@ -413,7 +417,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
413417
if let Some(t) = t {
414418
rate = t;
415419
} else {
416-
warn!("Missing ratelimit");
420+
info!("Missing ratelimit");
417421
}
418422
factorion_lib::influxdb::reddit::log_comment_reply(
419423
influx_client,

factorion-bot-reddit/src/reddit_api.rs

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ impl std::fmt::Display for RateLimitErr {
7777
impl std::error::Error for RateLimitErr {}
7878
#[derive(Debug, Clone, Default)]
7979
pub struct LastIds {
80-
pub comments: (String, String),
81-
pub posts: (String, String),
82-
pub mentions: (String, String),
80+
pub comments: String,
81+
pub posts: String,
82+
pub mentions: String,
8383
}
8484

8585
impl RedditClient {
@@ -209,7 +209,7 @@ impl RedditClient {
209209
let (subs_response, posts_response, mentions_response) = join!(
210210
OptionFuture::from(SUBREDDIT_URL.clone().map(|subreddit_url| {
211211
let request = self.client.get(subreddit_url);
212-
let request = add_query(request, &last_ids.comments.0);
212+
let request = add_query(request, &last_ids.comments);
213213
request.bearer_auth(&self.token.access_token).send()
214214
})),
215215
OptionFuture::from(
@@ -218,14 +218,14 @@ impl RedditClient {
218218
.flatten()
219219
.map(|subreddit_url| {
220220
let request = self.client.get(subreddit_url);
221-
let request = add_query(request, &last_ids.posts.0);
221+
let request = add_query(request, &last_ids.posts);
222222
request.bearer_auth(&self.token.access_token).send()
223223
})
224224
),
225225
OptionFuture::from(check_mentions.then_some(MENTION_URL.clone()).map(
226226
|subreddit_url| {
227227
let request = self.client.get(subreddit_url);
228-
let request = add_query(request, &last_ids.mentions.0);
228+
let request = add_query(request, &last_ids.mentions);
229229
request.bearer_auth(&self.token.access_token).send()
230230
}
231231
)),
@@ -265,14 +265,11 @@ impl RedditClient {
265265

266266
reset_timer = Self::update_reset_timer(reset_timer, t);
267267

268-
if !a.is_empty()
269-
&& last_ids.mentions.1 != ""
270-
&& !a.iter().any(|x| x.meta.id == last_ids.mentions.1)
268+
if last_ids.mentions != ""
269+
&& a.len()
270+
== *COMMENT_COUNT.get().expect("Comment count uninitialized") as usize
271271
{
272-
warn!(
273-
"Failed to keep up with mentions. last_id: {}",
274-
last_ids.mentions.1
275-
);
272+
warn!("Got a full response, meaning capacity is reached!");
276273
}
277274

278275
if let Some(id) = id {
@@ -296,14 +293,11 @@ impl RedditClient {
296293

297294
reset_timer = Self::update_reset_timer(reset_timer, t);
298295

299-
if !a.is_empty()
300-
&& last_ids.comments.1 != ""
301-
&& !a.iter().any(|x| x.meta.id == last_ids.comments.1)
296+
if last_ids.comments != ""
297+
&& a.len()
298+
== *COMMENT_COUNT.get().expect("Comment count uninitialized") as usize
302299
{
303-
warn!(
304-
"Failed to keep up with comments. last_id: {}",
305-
last_ids.comments.1
306-
);
300+
warn!("Got a full response, meaning capacity is reached!");
307301
}
308302

309303
if let Some(id) = id {
@@ -327,14 +321,11 @@ impl RedditClient {
327321

328322
reset_timer = Self::update_reset_timer(reset_timer, t);
329323

330-
if !posts.is_empty()
331-
&& last_ids.posts.1 != ""
332-
&& !posts.iter().any(|x| x.meta.id == last_ids.posts.1)
324+
if last_ids.posts != ""
325+
&& posts.len()
326+
== *COMMENT_COUNT.get().expect("Comment count uninitialized") as usize
333327
{
334-
warn!(
335-
"Failed to keep up with posts. last_id: {}",
336-
last_ids.posts.1
337-
);
328+
warn!("Got a full response, meaning capacity is reached!");
338329
}
339330

340331
if let Some(id) = id {
@@ -603,17 +594,28 @@ impl RedditClient {
603594

604595
fn check_response_status(response: &Response) -> Result<(), ()> {
605596
if !response.status().is_success() {
606-
if response.status().as_u16() == 500 {
607-
error!(
597+
match response.status().as_u16() {
598+
500 => error!(
608599
"Failed to get comments. Statuscode: {:?}. Internal server error.",
609600
response.status()
610-
);
611-
} else {
612-
error!(
601+
),
602+
502 => error!(
603+
"Failed to get comments. Statuscode: {:?}. Bad Gateway.",
604+
response.status()
605+
),
606+
503 => error!(
607+
"Failed to get comments. Statuscode: {:?}. Service unavailable.",
608+
response.status()
609+
),
610+
504 => error!(
611+
"Failed to get comments. Statuscode: {:?}. Gateway Timeout.",
612+
response.status()
613+
),
614+
_ => error!(
613615
"Failed to get comments. Statuscode: {:?}. Response: {:?}",
614616
response.status(),
615617
response
616-
);
618+
),
617619
}
618620
return Err(());
619621
}
@@ -637,7 +639,7 @@ impl RedditClient {
637639
Vec<CommentConstructed<Meta>>,
638640
Vec<(String, (String, Commands, String))>,
639641
Option<(f64, f64)>,
640-
Option<(String, String)>,
642+
Option<String>,
641643
),
642644
Box<dyn std::error::Error>,
643645
> {
@@ -775,12 +777,10 @@ impl RedditClient {
775777
comments.push(extracted_comment);
776778
}
777779
let id = if comments.is_empty() {
778-
warn!("No comments. Requested comment (last_id or summon) is gone.");
779-
Some((String::new(), String::new()))
780+
info!("No comments. Requested comment (last_id or summon) is gone.");
781+
Some(String::new())
780782
} else {
781-
comments
782-
.get(1)
783-
.map(|o| (o.meta.id.clone(), comments.get(0).unwrap().meta.id.clone()))
783+
comments.get(1).map(|o| o.meta.id.clone())
784784
};
785785

786786
Ok((
@@ -1092,9 +1092,9 @@ mod tests {
10921092
let _ = COMMENT_COUNT.set(100);
10931093
let mut already_replied = vec![];
10941094
let mut last_ids = LastIds {
1095-
comments: ("t1_m86nsre".to_owned(), "".to_owned()),
1096-
posts: ("t3_83us27sa".to_owned(), "".to_owned()),
1097-
mentions: ("".to_owned(), "".to_owned()),
1095+
comments: "t1_m86nsre".to_owned(),
1096+
posts: "t3_83us27sa".to_owned(),
1097+
mentions: "".to_owned(),
10981098
};
10991099
let (status, comments) = join!(
11001100
async {
@@ -1327,10 +1327,7 @@ mod tests {
13271327
);
13281328
println!("{comments:?}");
13291329
assert_eq!(t, Some((350.0, 10.0)));
1330-
assert_eq!(
1331-
id.unwrap(),
1332-
("t3_m38msug".to_owned(), "t3_m38msum".to_owned())
1333-
);
1330+
assert_eq!(id.unwrap(), "t3_m38msug".to_owned());
13341331
}
13351332

13361333
#[test]

factorion-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "factorion-lib"
3-
version = "4.2.3"
3+
version = "4.2.4"
44
edition = "2024"
55
description = "A library used to create bots to recognize and calculate factorials and related concepts"
66
license = "MIT"

0 commit comments

Comments
 (0)