Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaks implementation support from bot #8

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/shuttle-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
uses: actions/checkout@v4

- name: Run shuttle project locally.
uses: denytwice/shuttle-run@v1.0
uses: ivinjabraham/shuttle-run@v1.1
with:
secrets: |
DISCORD_TOKEN = '${{ secrets.DISCORD_TOKEN }}'
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The event handler takes care of the rest:
```rust
// On the event of a reaction being added
FullEvent::ReactionAdd { add_reaction } => {
let message_id = MessageId::new(ARCHIVE_MESSAGE_ID);
let message_id = MessageId::new(ROLES_MESSAGE_ID);
// Check if the reaction was added to the message we want and if it is reacted with the
// emoji we want
if add_reaction.message_id == message_id && data.reaction_roles.contains_key(&add_reaction.emoji) {
Expand Down
3 changes: 3 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ async fn amdctl(ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}

/// Every function that is defined *should* be added to the
/// returned vector in get_commands to ensure it is registered (available for the user)
/// when the bot goes online.
pub fn get_commands() -> Vec<poise::Command<Data, Error>> {
vec![amdctl()]
}
6 changes: 2 additions & 4 deletions src/scheduler/mod.rs → src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod scheduler;
pub mod tasks;

pub use self::scheduler::run_scheduler;
pub mod models;
pub mod queries;
28 changes: 25 additions & 3 deletions src/scheduler/tasks/mod.rs → src/graphql/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,29 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod status_update;
pub mod tasks;
use serde::Deserialize;
use std::borrow::Cow;

pub use self::tasks::{get_tasks, Task};
#[derive(Deserialize)]
pub struct Member<'a> {
id: Option<i32>,
roll_num: Option<Cow<'a, str>>,
name: Option<Cow<'a, str>>,
hostel: &'a str,
email: &'a str,
sex: &'a str,
year: i32,
mac_addr: &'a str,
discord_id: &'a str,
group_id: i32,
}

#[derive(Deserialize)]
struct Data<'a> {
getMember: Vec<Member<'a>>,
}

#[derive(Deserialize)]
struct Root<'a> {
data: Data<'a>,
}
105 changes: 105 additions & 0 deletions src/graphql/queries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
amFOSS Daemon: A discord bot for the amFOSS Discord server.
Copyright (C) 2024 amFOSS

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use serde_json::Value;
use anyhow::{Result};

use super::models::Member;

const REQUEST_URL: &str = "https://root.shuttleapp.rs/";

pub async fn fetch_members() -> Result<Vec<(String, i32)>, reqwest::Error> {
let client = reqwest::Client::new();
let query = r#"
query {
getMember {
name,
groupId,
discordId
name
id
}
}"#;

let response = client
.post(REQUEST_URL)
.json(&serde_json::json!({"query": query}))
.send()
.await?;

let json: Value = response.json().await?;

let member_names: Vec<(String, i32)> = json["data"]["getMember"]
.as_array()
.unwrap_or(&vec![])
.iter()
.map(|member| {
let id = member["id"].as_i64().unwrap_or(0) as i32;
let name = member["name"].as_str().unwrap_or("").to_string();
(name, id)
})
.collect();

Ok(member_names)
}

pub async fn send_streak_update(
root_api_url: &str,
id: i32,
has_sent_update: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let query = r#"
mutation updateStreak($id: Int!, $hasSentUpdate: Boolean!) {
updateStreak(id: $id, hasSentUpdate: $hasSentUpdate) {
id
streak
maxStreak
}
}
"#;

let variables = serde_json::json!({
"id": id,
"hasSentUpdate": has_sent_update
});

let body = serde_json::json!({
"query": query,
"variables": variables
});

let response = client
.post(root_api_url)
.header("Content-Type", "application/json")
.json(&body)
.send()
.await?;

let response_status = response.status();
let response_body = response.text().await?;

if response_status.is_success() {
println!("Successfully updated streak for ID {}: {}", id, response_body);
Ok(())
} else {
Err(anyhow::anyhow!(
"Failed to update streak for ID {}. HTTP status: {}, response: {}",
id, response_status, response_body
).into())
}
}
8 changes: 6 additions & 2 deletions src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub const ARCHIVE_MESSAGE_ID: u64 = 1298636092886749294;
/// Points to the Embed in the #roles channel.
pub const ROLES_MESSAGE_ID: u64 = 1298636092886749294;

// Role IDs
pub const ARCHIVE_ROLE_ID: u64 = 1208457364274028574;
pub const MOBILE_ROLE_ID: u64 = 1298553701094395936;
pub const SYSTEMS_ROLE_ID: u64 = 1298553801191718944;
Expand All @@ -24,8 +27,9 @@ pub const RESEARCH_ROLE_ID: u64 = 1298553855474270219;
pub const DEVOPS_ROLE_ID: u64 = 1298553883169132554;
pub const WEB_ROLE_ID: u64 = 1298553910167994428;

// Channel IDs
pub const GROUP_ONE_CHANNEL_ID: u64 = 1225098248293716008;
pub const GROUP_TWO_CHANNEL_ID: u64 = 1225098298935738489;
pub const GROUP_THREE_CHANNEL_ID: u64 = 1225098353378070710;
pub const GROUP_FOUR_CHANNEL_ID: u64 = 1225098407216156712;
pub const STATUS_UPDATE_CHANNEL_ID: u64 = 764575524127244318;
pub const STATUS_UPDATE_CHANNEL_ID: u64 = 764575524127244318;
Loading