@@ -2,6 +2,7 @@ use crate::bot::Error;
22use redis:: AsyncCommands ;
33use serenity:: all:: { GuildId , Http , RoleId } ;
44use std:: collections:: HashMap ;
5+ use std:: str:: FromStr ;
56
67#[ derive( Debug , Clone , PartialEq , Eq ) ]
78pub enum RoleMode {
@@ -11,16 +12,20 @@ pub enum RoleMode {
1112 Custom ,
1213}
1314
14- impl RoleMode {
15- pub fn from_str ( s : & str ) -> Self {
16- match s {
15+ impl FromStr for RoleMode {
16+ type Err = ( ) ;
17+
18+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
19+ Ok ( match s {
1720 "levels" => Self :: Levels ,
1821 "classes" => Self :: Classes ,
1922 "custom" => Self :: Custom ,
2023 _ => Self :: None ,
21- }
24+ } )
2225 }
26+ }
2327
28+ impl RoleMode {
2429 pub fn as_str ( & self ) -> & str {
2530 match self {
2631 Self :: None => "none" ,
@@ -57,21 +62,24 @@ impl GuildRoleConfig {
5762 // Get the role mode
5863 let role_mode_key = format ! ( "guild:{}:role_mode" , guild_id) ;
5964 let role_mode: Option < String > = redis. get ( & role_mode_key) . await ?;
60- let mode = RoleMode :: from_str ( & role_mode. unwrap_or_else ( || "none" . to_string ( ) ) ) ;
65+ let mode = role_mode
66+ . unwrap_or_else ( || "none" . to_string ( ) )
67+ . parse ( )
68+ . unwrap_or ( RoleMode :: None ) ;
6169
6270 // Get level roles
6371 let mut level_roles = HashMap :: new ( ) ;
6472 for level in & [ "Undergrad" , "Graduate" ] {
6573 let key = format ! ( "guild:{}:role:level:{}" , guild_id, level) ;
66- if let Ok ( Some ( role_id_str) ) = redis. get :: < _ , Option < String > > ( & key) . await {
67- if let Ok ( role_id_u64) = role_id_str. parse :: < u64 > ( ) {
68- // Verify the role still exists
69- let role_id = RoleId :: new ( role_id_u64 ) ;
70- let roles = guild_id . roles ( http ) . await ? ;
71-
72- if roles . contains_key ( & role_id ) {
73- level_roles . insert ( level . to_string ( ) , role_id) ;
74- }
74+ if let Ok ( Some ( role_id_str) ) = redis. get :: < _ , Option < String > > ( & key) . await
75+ && let Ok ( role_id_u64) = role_id_str. parse :: < u64 > ( )
76+ {
77+ // Verify the role still exists
78+ let role_id = RoleId :: new ( role_id_u64 ) ;
79+ let roles = guild_id . roles ( http ) . await ? ;
80+
81+ if roles . contains_key ( & role_id) {
82+ level_roles . insert ( level . to_string ( ) , role_id ) ;
7583 }
7684 }
7785 }
@@ -88,15 +96,15 @@ impl GuildRoleConfig {
8896 "Doctoral" ,
8997 ] {
9098 let key = format ! ( "guild:{}:role:class:{}" , guild_id, class) ;
91- if let Ok ( Some ( role_id_str) ) = redis. get :: < _ , Option < String > > ( & key) . await {
92- if let Ok ( role_id_u64) = role_id_str. parse :: < u64 > ( ) {
93- // Verify the role still exists
94- let role_id = RoleId :: new ( role_id_u64 ) ;
95- let roles = guild_id . roles ( http ) . await ? ;
96-
97- if roles . contains_key ( & role_id ) {
98- class_roles . insert ( class . to_string ( ) , role_id) ;
99- }
99+ if let Ok ( Some ( role_id_str) ) = redis. get :: < _ , Option < String > > ( & key) . await
100+ && let Ok ( role_id_u64) = role_id_str. parse :: < u64 > ( )
101+ {
102+ // Verify the role still exists
103+ let role_id = RoleId :: new ( role_id_u64 ) ;
104+ let roles = guild_id . roles ( http ) . await ? ;
105+
106+ if roles . contains_key ( & role_id) {
107+ class_roles . insert ( class . to_string ( ) , role_id ) ;
100108 }
101109 }
102110 }
0 commit comments