Fix some logic errors

This commit is contained in:
winneratwin 2022-07-16 00:43:59 +01:00
parent 0e08fed0c5
commit 6b76a68fde
Signed by: winneratwin
GPG Key ID: CDBC42F8803D689E
2 changed files with 69 additions and 63 deletions

View File

@ -1,3 +1,3 @@
this is a automated
message with newlines
hi <@172716215817601026>
by <@172716215817601026>

View File

@ -31,6 +31,11 @@ struct Typing {
thread_create_cooldown_ms: Option<i32>,
}
struct RecentMessages {
last_message_timestamp: i64,
messages_before_last_message: i32,
}
#[tokio::main]
async fn main() {
// read lines from channels.txt file
@ -49,6 +54,7 @@ async fn main() {
let mut message = String::new();
file.read_to_string(&mut message)
.expect("Unable to read file");
message = message.trim().to_owned();
// read discord token from file
let mut file = File::open("token.txt").expect("Unable to open file");
@ -56,19 +62,33 @@ async fn main() {
file.read_to_string(&mut token)
.expect("Unable to read file");
let token = token.trim().to_owned();
// get userid
let user_id = get_token_user_id(token.clone()).await;
println!(
"loaded account with name: {}#{}",
user_id.username, user_id.discriminator
);
println!("loaded channels:");
for channel in &channels {
println!("{}", channel);
}
println!("loaded message:");
println!("\"{}\"", message);
println!("\nstarting bot...");
// set delay for how long to wait before posting cringe
let delay = 60 * 15;
let delay = 60 * 15;
// create handle list for each channel
let mut handles = Vec::new();
// create a handle for each channel
for channel in channels {
// create clones of the variables to use in thread
let channel_id = channel.clone();
let message = message.clone();
@ -79,92 +99,57 @@ async fn main() {
loop {
// check if we have sent a message in the last x minutes and more then 4 messages before our last message
let cutoff = delay;
// get current time
let current_time = Local::now();
let current_time_timestamp = current_time.timestamp();
// get last message time
let mut has_sent_message_recently = false;
let mut time_since_last_message = 0;
// set channel
let working_channel = channel_id.clone();
// get messages
let messages = get_messages(token.clone(), working_channel.clone()).await;
let r_messages =
recent_messages(token.clone(), channel_id.clone(), user_id.id.clone()).await;
// get messages after our last message
let mut messages_before_last_message = 0;
//loop through messages
for message in messages {
// check if message is sent by user
if message.author.id == user_id.id {
// convert rfc 3339 timestamp to unix timestamp
// fuck discord for not using unix millis instead of rfc 3339
let timestamp =
DateTime::parse_from_rfc3339(message.timestamp.as_str()).unwrap();
// get time since message
let seconds_since_message = current_time_timestamp - timestamp.timestamp();
// check if message is sent in the last x minutes
if seconds_since_message < cutoff {
has_sent_message_recently = true;
time_since_last_message = seconds_since_message;
break;
} else {
has_sent_message_recently = false;
break;
}
} else {
// increment counter for each message before our last message
messages_before_last_message += 1;
}
}
if has_sent_message_recently {
let time_since_last_message =
current_time_timestamp - r_messages.last_message_timestamp;
if time_since_last_message < cutoff {
println!(
"has sent message in the last {} minutes in {}",
cutoff/60,working_channel
cutoff / 60,
working_channel
);
let time_to_wait = cutoff - time_since_last_message;
println!("waiting {} seconds before sending message", time_to_wait);
sleep(Duration::from_secs(time_to_wait as u64));
}
// check if more then 5 or more messages before our last message
if messages_before_last_message < 4 {
if r_messages.messages_before_last_message <= 4 {
println!(
"others have sent less then 5 messages before last message in {}",
working_channel
);
// name this loop so we can break out from it
'outer: loop {
loop {
// check every 5 minutes to see if there are more then 5 or more messages before our last message
println!(
"checking if there are more then 4 messages before our last message in {}",
working_channel
);
//count messages
let mut messages_before_last_message = 0;
for x in get_messages(token.clone(), working_channel.clone()).await {
if x.author.id == user_id.id {
if messages_before_last_message > 4 {
break 'outer;
}
}
messages_before_last_message += 1;
let r_messages =
recent_messages(token.clone(), channel_id.clone(), user_id.id.clone())
.await;
if r_messages.messages_before_last_message > 4 {
println!(
"there are more then 4 messages before our last message in {}",
working_channel
);
break;
}
println!("checking again in 5 minutes");
// sleep for 5 minutes
sleep(Duration::from_secs(60 * 5));
}
}
// check if we are being ratelimited by channel slowmode by using the typing endpoint(why tf is slowmode api hidden here)
let typing = get_typing(token.clone(), working_channel.clone()).await;
match typing.message_send_cooldown_ms {
@ -178,7 +163,7 @@ async fn main() {
}
None => {}
}
// send message
send_message(token.clone(), message.clone(), working_channel.clone()).await;
@ -189,8 +174,8 @@ async fn main() {
}
println!("threads started");
for handle in handles {
match handle.await{
Ok(_) => {},
match handle.await {
Ok(_) => {}
Err(e) => {
println!("{:?}", e);
}
@ -198,6 +183,27 @@ async fn main() {
}
}
// return number of messages before last message by user and timestamp of last message by user
async fn recent_messages(token: String, channel_id: String, user_id: String) -> RecentMessages {
let messages = get_messages(token, channel_id).await;
let mut messages_before_last_message = 0;
let mut last_message_timestamp = 0;
for message in messages {
if message.author.id == user_id {
// convert rfc 3339 timestamp to unix timestamp
// fuck discord for not using unix millis instead of rfc 3339
let timestamp = DateTime::parse_from_rfc3339(message.timestamp.as_str()).unwrap();
last_message_timestamp = timestamp.timestamp();
break;
}
messages_before_last_message += 1;
}
RecentMessages {
last_message_timestamp: last_message_timestamp,
messages_before_last_message: messages_before_last_message,
}
}
async fn get_token_user_id(token: String) -> User {
let url = format!("https://discordapp.com/api/v9/users/@me");
let client = reqwest::Client::new();
@ -216,7 +222,7 @@ async fn get_token_user_id(token: String) -> User {
async fn get_messages(token: String, channel: String) -> Vec<Message> {
let mut messages: Vec<Message> = Vec::new();
let url = format!(
"https://discordapp.com/api/v9/channels/{}/messages?limit=10",
"https://discordapp.com/api/v9/channels/{}/messages?limit=50",
channel
);
let mut headers = reqwest::header::HeaderMap::new();