11from discord .ext import commands
22from esportsbot .base_functions import role_id_from_mention
33from esportsbot .db_gateway import DBGatewayActions
4- from esportsbot .models import Guild_info
4+ from esportsbot .models import Guild_info , Default_roles
5+ from esportsbot .base_functions import role_id_from_mention , send_to_log_channel
56
67
78class DefaultRoleCog (commands .Cog ):
@@ -14,15 +15,24 @@ async def on_member_join(self, member):
1415 guild = DBGatewayActions ().get (Guild_info , guild_id = member .guild .id )
1516 if not guild :
1617 return
17-
18- if guild .default_role_id :
19- default_role = member .guild .get_role (guild .default_role_id )
20- await member .add_roles (default_role )
18+ # Get all the default role for the server from database
19+ guild_default_roles = DBGatewayActions ().list (Default_roles , guild_id = member .guild .id )
20+ # Check to see if any roles exist
21+ if guild_default_roles :
22+ # Create list of roles from database response
23+ apply_roles = [member .guild .get_role (role .role_id ) for role in guild_default_roles ]
24+ # Add all the roles to the user, we don't check if they're valid as we do this on input
25+ await member .add_roles (* apply_roles )
2126 await self .bot .adminLog (
2227 None ,
2328 {
24- "Cog" : str (type (self )),
25- "Message" : f"{ member .mention } has joined the server and received the { default_role .mention } role"
29+ "Cog" :
30+ str (type (self )),
31+ "Message" :
32+ self .STRINGS ['default_role_join' ].format (
33+ member_name = member .mention ,
34+ role_ids = (' ' .join (f'<@&{ x .id } >' for x in apply_roles ))
35+ )
2636 },
2737 guildID = member .guild .id
2838 )
@@ -31,76 +41,86 @@ async def on_member_join(self, member):
3141 None ,
3242 {
3343 "Cog" : str (type (self )),
34- "Message" : f" { member .mention } has joined the server"
44+ "Message" : self . STRINGS [ 'default_role_join_no_role' ]. format ( member_name = member .mention )
3545 },
3646 guildID = member .guild .id
3747 )
3848
3949 @commands .command (
40- name = "setdefaultrole " ,
41- usage = "<role_id> or <@role>" ,
42- help = "Sets the role that the server gives to members when they join the server"
50+ name = "setdefaultroles " ,
51+ usage = "<@role> <@role> <@role> ... " ,
52+ help = "Sets the roles that the server gives to members when they join the server"
4353 )
4454 @commands .has_permissions (administrator = True )
45- async def setdefaultrole (self , ctx , given_role_id ):
46- cleaned_role_id = role_id_from_mention (given_role_id ) if given_role_id else False
47- if cleaned_role_id :
48- guild = DBGatewayActions ().get (Guild_info , guild_id = ctx .author .guild .id )
49-
50- if not guild :
51- db_item = Guild_info (guild_id = ctx .guild .id , default_role_id = cleaned_role_id )
52- DBGatewayActions ().create (db_item )
53- else :
54- guild .default_role_id = cleaned_role_id
55- DBGatewayActions ().update (guild )
56-
57- await ctx .channel .send (self .STRINGS ['default_role_set' ].format (role_id = cleaned_role_id ))
58- default_role = ctx .author .guild .get_role (cleaned_role_id )
59- await self .bot .adminLog (
60- ctx .message ,
61- {
62- "Cog" :
63- str (type (self )),
64- "Message" :
65- self .STRINGS ['default_role_set_log' ].format (author = ctx .author .mention ,
66- role_mention = default_role .mention )
67- }
68- )
55+ async def setdefaultroles (self , ctx , * , args : str ):
56+ role_list = args .split (" " )
57+ if len (role_list ) == 0 :
58+ await ctx .channel .send (self .STRINGS ['default_roles_set_empty' ])
6959 else :
70- await ctx .channel .send (self .STRINGS ['default_role_set_missing_params' ])
60+ checked_roles = []
61+ checking_error = False
62+ # Loop through the roles to check the input formatting is correct and that roles exist
63+ for role in role_list :
64+ try :
65+ # Clean the inputted role to just the id
66+ cleaned_role_id = role_id_from_mention (role )
67+ # Obtain role object from the guild to check it exists
68+ role_obj = ctx .author .guild .get_role (cleaned_role_id )
69+ # Add role to array to add post checks
70+ checked_roles .append (cleaned_role_id )
71+ except Exception as err :
72+ print (err )
73+ checking_error = True
74+ if not checking_error :
75+ for role in checked_roles :
76+ DBGatewayActions ().create (Default_roles (guild_id = ctx .author .guild .id , role_id = role ))
77+ await ctx .channel .send (self .STRINGS ['default_roles_set' ].format (roles = args ))
78+ await self .bot .adminLog (
79+ ctx .message ,
80+ {
81+ "Cog" : str (type (self )),
82+ "Message" : self .STRINGS ['default_roles_set_log' ].format (author_mention = ctx .author .mention ,
83+ roles = args )
84+ }
85+ )
86+ else :
87+ await ctx .channel .send (self .STRINGS ['default_roles_set_error' ])
7188
7289 @commands .command (
73- name = "getdefaultrole " ,
90+ name = "getdefaultroles " ,
7491 usage = "" ,
75- help = "Gets the role that the server gives to members when they join the server"
92+ help = "Gets the roles that the server gives to members when they join the server"
7693 )
7794 @commands .has_permissions (administrator = True )
78- async def getdefaultrole (self , ctx ):
79- guild = DBGatewayActions ().get (Guild_info , guild_id = ctx .author .guild .id )
80- if not guild :
81- await ctx .channel .send (self .STRINGS ['default_role_missing' ])
82- return
83-
84- if guild .default_role_id :
85- await ctx .channel .send (self .STRINGS ['default_role_get' ].format (role_id = guild .default_role_id ))
95+ async def getdefaultroles (self , ctx ):
96+ # Get all the default role for the server from database
97+ guild_default_roles = DBGatewayActions ().list (Default_roles , guild_id = ctx .author .guild .id )
98+ # Check to see if any roles exist
99+ if guild_default_roles :
100+ # Create list of roles from database response
101+ apply_roles = [ctx .author .guild .get_role (role .role_id ) for role in guild_default_roles ]
102+ # Return all the default roles to the user
103+ await ctx .channel .send (
104+ self .STRINGS ['default_role_get' ].format (role_ids = (' ' .join (f'<@&{ x .id } >' for x in apply_roles )))
105+ )
86106 else :
87107 await ctx .channel .send (self .STRINGS ['default_role_missing' ])
88108
89109 @commands .command (
90- name = "removedefaultrole " ,
110+ name = "removedefaultroles " ,
91111 usage = "" ,
92- help = "Removes the role that the server gives to members when they join the server"
112+ help = "Removes the roles that the server gives to members when they join the server"
93113 )
94114 @commands .has_permissions (administrator = True )
95- async def removedefaultrole (self , ctx ):
96- guild = DBGatewayActions (). get ( Guild_info , guild_id = ctx . author . guild . id )
97- if not guild :
98- await ctx . channel . send ( self . STRINGS [ 'default_role_missing' ])
99- return
100-
101- if guild . default_role_id :
102- guild . default_role_id = None
103- DBGatewayActions (). update ( guild )
115+ async def removedefaultroles (self , ctx ):
116+ # Get all the default role for the server from database
117+ guild_default_roles = DBGatewayActions (). list ( Default_roles , guild_id = ctx . author . guild . id )
118+ # Check to see if any roles exist
119+ if guild_default_roles :
120+ for default_role in guild_default_roles :
121+ # Remove the current role
122+ DBGatewayActions (). delete ( default_role )
123+ # Return a response to the user
104124 await ctx .channel .send (self .STRINGS ['default_role_removed' ])
105125 await self .bot .adminLog (
106126 ctx .message ,
0 commit comments