Skip to content

Commit

Permalink
add domain specific roles and update event handler accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
ivinjabraham committed Oct 23, 2024
1 parent c9fd045 commit 057ff5c
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ use std::collections::HashMap;

use poise::{Context as PoiseContext, Framework, FrameworkOptions, PrefixFrameworkOptions};
use serenity::{
client::Context as SerenityContext,
client::FullEvent,
model::{
client::{Context as SerenityContext, FullEvent}, model::{
channel::ReactionType,
gateway::GatewayIntents,
id::{MessageId, RoleId},
},
}
};

type Context<'a> = PoiseContext<'a, Data, Error>;
type Error = Box<dyn std::error::Error + Send + Sync>;

struct Data {
reaction_roles: HashMap<MessageId, (ReactionType, RoleId)>,
reaction_roles: HashMap<ReactionType, RoleId>,
}

const ARCHIVE_MESSAGE_ID: u64 = 1295821555586175083;
const ARCHIVE_MESSAGE_ID: u64 = 1298636092886749294;
const ARCHIVE_ROLE_ID: u64 = 1208457364274028574;
const MOBILE_ROLE_ID: u64 = 1298553701094395936;
const SYSTEMS_ROLE_ID: u64 = 1298553801191718944;
const AI_ROLE_ID: u64 = 1298553753523453952;
const RESEARCH_ROLE_ID: u64 = 1298553855474270219;
const DEVOPS_ROLE_ID: u64 = 1298553883169132554;
const WEB_ROLE_ID: u64 = 1298553910167994428;

#[shuttle_runtime::main]
async fn main(
Expand Down Expand Up @@ -93,13 +97,26 @@ fn initialize_data() -> Data {
reaction_roles: HashMap::new(),
};

let message_id = MessageId::new(ARCHIVE_MESSAGE_ID);
let role_id = RoleId::new(ARCHIVE_ROLE_ID);
let archive_role_id = RoleId::new(ARCHIVE_ROLE_ID);
let mobile_role_id = RoleId::new(MOBILE_ROLE_ID);
let systems_role_id = RoleId::new(SYSTEMS_ROLE_ID);
let ai_role_id = RoleId::new(AI_ROLE_ID);
let research_role_id = RoleId::new(RESEARCH_ROLE_ID);
let devops_role_id = RoleId::new(DEVOPS_ROLE_ID);
let web_role_id = RoleId::new(WEB_ROLE_ID);


let message_roles = [
(ReactionType::Unicode("πŸ“".to_string()), archive_role_id),
(ReactionType::Unicode("πŸ“±".to_string()), mobile_role_id),
(ReactionType::Unicode("βš™οΈ".to_string()), systems_role_id),
(ReactionType::Unicode("πŸ€–".to_string()), ai_role_id),
(ReactionType::Unicode("πŸ“œ".to_string()), research_role_id),
(ReactionType::Unicode("πŸš€".to_string()), devops_role_id),
(ReactionType::Unicode("🌐".to_string()), web_role_id),
];

data.reaction_roles.insert(
message_id,
(ReactionType::Unicode("πŸ“".to_string()), role_id),
);
data.reaction_roles.extend::<HashMap<ReactionType, RoleId>>(message_roles.into());

data
}
Expand All @@ -112,38 +129,32 @@ async fn event_handler(
) -> Result<(), Error> {
match event {
FullEvent::ReactionAdd { add_reaction } => {
if let Some((expected_reaction, role_id)) =
data.reaction_roles.get(&add_reaction.message_id)
{
if &add_reaction.emoji == expected_reaction {
let message_id = MessageId::new(ARCHIVE_MESSAGE_ID);
if add_reaction.message_id == message_id && data.reaction_roles.contains_key(&add_reaction.emoji) {
if let Some(guild_id) = add_reaction.guild_id {
// TODO: Use try_join to await concurrently?
if let Ok(member) =
guild_id.member(ctx, add_reaction.user_id.unwrap()).await
{
if let Err(e) = member.add_role(&ctx.http, *role_id).await {
if let Err(e) = member.add_role(&ctx.http, data.reaction_roles.get(&add_reaction.emoji).expect("Hard coded value verified earlier.")).await {
eprintln!("Error: {:?}", e);
}
}
}
}
}
}

FullEvent::ReactionRemove { removed_reaction } => {
if let Some((expected_reaction, role_id)) =
data.reaction_roles.get(&removed_reaction.message_id)
{
if &removed_reaction.emoji == expected_reaction {
let message_id = MessageId::new(ARCHIVE_MESSAGE_ID);
if message_id == removed_reaction.message_id && data.reaction_roles.contains_key(&removed_reaction.emoji) {
if let Some(guild_id) = removed_reaction.guild_id {
if let Ok(member) = guild_id
.member(ctx, removed_reaction.user_id.unwrap())
.await
{
if let Err(e) = member.remove_role(&ctx.http, *role_id).await {
if let Err(e) = member.remove_role(&ctx.http, *data.reaction_roles.get(&removed_reaction.emoji).expect("Hard coded value verified earlier")).await {
eprintln!("Error: {:?}", e);
}
}
}
}
}
Expand Down

0 comments on commit 057ff5c

Please sign in to comment.