Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/graphql/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,63 @@ impl GraphQLClient {
);
Ok(attendance)
}

pub async fn save_member_roles( &self, discord_id: String, roles: Vec<String>,) -> anyhow::Result<()> {

let query = r#"
mutation($discordId: String!, $roles: [String!]!) {
saveMemberRoles(discordId: $discordId, roles: $roles)
}"#;

let variables = serde_json::json!({
"discordId": discord_id,
"roles": roles
});

let res = self.http()
.post(self.root_url())
.bearer_auth(self.api_key())
.json(&serde_json::json!({
"query": query,
"variables": variables
}))
.send()
.await?;
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
Ok(())
}

pub async fn get_member_roles( &self, discord_id: String,) -> anyhow::Result<Vec<String>> {
let query = r#"
query($discordId: String!) {
memberRoles(discordId: $discordId)
}"#;

let variables = serde_json::json!({
"discordId": discord_id
});

let response = self.http()
.post(self.root_url())
.bearer_auth(self.api_key())
.json(&serde_json::json!({
"query": query,
"variables": variables
}))
.send()
.await?
.json::<serde_json::Value>()
.await?;

Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
//remove @everyone role, a defult role that every member has and is not useful for our purposes. It has the same ID as the guild, so we can filter it out by comparing with the guild ID.
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
let roles = response["data"]["memberRoles"]
.as_array()
.unwrap_or(&vec![])
.iter()
Comment thread
swayam-agrahari marked this conversation as resolved.
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect();

Ok(roles)
}

}
33 changes: 32 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn prepare_data(config: &Config, reload_handle: ReloadHandle) -> Data {
async fn build_client(config: &Config, data: Data) -> Result<Client, anyhow::Error> {
ClientBuilder::new(
config.discord_token.clone(),
GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT,
GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS,
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
)
Comment thread
hrideshmg marked this conversation as resolved.
.framework(build_framework(
config.owner_id,
Expand Down Expand Up @@ -160,6 +160,37 @@ async fn event_handler(
FullEvent::ReactionRemove { removed_reaction } => {
handle_reaction(ctx, removed_reaction, data, false).await?;
}

FullEvent::GuildMemberRemoval { guild_id: _, user, member_data_if_available } => {

if let Some(member) = member_data_if_available {

let roles: Vec<String> = member.roles
.iter()
.filter(|r| r.get() != member.guild_id.get())
.map(|r| r.get().to_string())
.collect();

if !roles.is_empty() {
data.graphql_client
.save_member_roles(user.id.to_string(), roles)
.await?;
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
}
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
}
Comment thread
hrideshmg marked this conversation as resolved.
}
FullEvent::GuildMemberAddition { new_member } => {

let roles = data
.graphql_client
.get_member_roles(new_member.user.id.to_string())
.await?;
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated

for role in roles {
if let Ok(role_id) = role.parse::<u64>() {
let _ = new_member.add_role(ctx, RoleId::new(role_id)).await;
}
Comment thread
hrideshmg marked this conversation as resolved.
}
Comment thread
swayam-agrahari marked this conversation as resolved.
Outdated
}
_ => {}
}

Expand Down
Loading