44import random
55
66
7- class TicketsCore :
7+ class SafeMember :
8+ def __init__ (self , member : discord .Member ):
9+ self .name = member .name
10+ self .mention = member .mention
11+
12+ def __str__ (self ):
13+ return self .name
14+
15+ def __getattr__ (self , name ):
16+ return ""
817
18+
19+ class TicketsCore :
920 async def red_delete_data_for_user (self , ** kwargs ):
1021 """ Nothing to delete """
1122 return
@@ -15,53 +26,78 @@ def __init__(self, bot):
1526
1627 self .config = Config .get_conf (self , identifier = 2134287593 )
1728 default_guild = {
18- ' category' : None ,
19- ' closed_category' : None ,
20- ' ticket_role' : None ,
21- ' default_message_ticket_channel' : None ,
22- ' sessions' : {}
29+ " category" : None ,
30+ " closed_category" : None ,
31+ " ticket_role" : None ,
32+ " default_message_ticket_channel" : None ,
33+ " sessions" : {},
2334 }
2435 self .config .register_guild (** default_guild )
2536
26- self .ticket_info_format = ' \n \n **[{datetime}]** [{author}]\n {information}'
37+ self .ticket_info_format = " \n \n **[{datetime}]** [{author}]\n {information}"
2738
2839 async def create_ticket (self , context ):
2940 guild = context .guild
3041 author = context .author
3142
32- ticket_role = [role for role in guild .roles if await self .config .guild (guild ).ticket_role () == role .id ]
43+ ticket_role = [
44+ role
45+ for role in guild .roles
46+ if await self .config .guild (guild ).ticket_role () == role .id
47+ ]
3348
3449 if ticket_role :
3550 ticket_role = ticket_role [0 ]
3651 category_channel = await self .config .guild (guild ).category ()
37- default_message_ticket_channel = await self .config .guild (guild ).default_message_ticket_channel ()
38-
39- if category_channel and category_channel in [category .id for category in guild .categories ]:
40- n1 = 10 ** 10
52+ default_message_ticket_channel = await self .config .guild (
53+ guild
54+ ).default_message_ticket_channel ()
55+
56+ if category_channel and category_channel in [
57+ category .id for category in guild .categories
58+ ]:
59+ n1 = 10 ** 10
4160 n2 = n1 * 10 - 1
4261 ticket_id = int (random .randint (n1 , n2 ))
43- ticket_channel = await guild .create_text_channel ('{}-{}' .format (author .display_name , ticket_id ),
44- category = self .bot .get_channel (category_channel ))
45-
46- await ticket_channel .set_permissions (author , read_messages = True , send_messages = True )
47- await ticket_channel .set_permissions (guild .me , read_messages = True , send_messages = True , manage_channels = True )
48-
49- await ticket_channel .edit (topic = self .ticket_info_format .format (ticket = ticket_id ,
50- datetime = datetime .utcnow ().strftime ('%d/%m/%Y %H:%M:%S' ),
51- author = author .display_name ,
52- information = 'Ticket opened' ))
62+ ticket_channel = await guild .create_text_channel (
63+ "{}-{}" .format (author .display_name , ticket_id ),
64+ category = self .bot .get_channel (category_channel ),
65+ )
66+
67+ await ticket_channel .set_permissions (
68+ author , read_messages = True , send_messages = True
69+ )
70+ await ticket_channel .set_permissions (
71+ guild .me , read_messages = True , send_messages = True , manage_channels = True
72+ )
73+
74+ await ticket_channel .edit (
75+ topic = self .ticket_info_format .format (
76+ ticket = ticket_id ,
77+ datetime = datetime .utcnow ().strftime ("%d/%m/%Y %H:%M:%S" ),
78+ author = author .display_name ,
79+ information = "Ticket opened" ,
80+ )
81+ )
5382
5483 if default_message_ticket_channel :
55- await ticket_channel .send (default_message_ticket_channel .format (member = author ,
56- channel = ticket_channel ,
57- origin = context .channel ,
58- ticket_role = ticket_role ))
84+ try :
85+ await ticket_channel .send (
86+ default_message_ticket_channel .format (
87+ member = SafeMember (author ),
88+ channel = ticket_channel ,
89+ origin = context .channel ,
90+ ticket_role = ticket_role ,
91+ )
92+ )
93+ except :
94+ return "Oops there has been an unexpected error with your new ticket message. Please contact the bot owner for assistance"
5995
6096 async with self .config .guild (guild ).sessions () as session :
61- session .update ({ticket_channel .id : author .id })
97+ session .update ({ticket_channel .id : author .id })
6298
6399 else :
64- return ' Naughty! You need to run the setup first.'
100+ return " Naughty! You need to run the setup first."
65101
66102 async def update_ticket (self , context , status ):
67103 try :
@@ -75,15 +111,20 @@ async def update_ticket(self, context, status):
75111
76112 sessions = await self .config .guild (guild ).sessions ()
77113
78- if str (channel .id ) in sessions and await self .config .guild (guild ).ticket_role () in [role .id for role in author .roles ]:
79-
80- ticket_id = str (channel .name ).split ('-' )[1 ]
81- await channel .edit (topic = channel .topic + self .ticket_info_format .format (
82- ticket = ticket_id ,
83- datetime = datetime .utcnow ().strftime ('%d/%m/%Y %H:%M:%S' ),
84- author = author .display_name ,
85- information = status )
86- )
114+ if str (channel .id ) in sessions and await self .config .guild (
115+ guild
116+ ).ticket_role () in [role .id for role in author .roles ]:
117+
118+ ticket_id = str (channel .name ).split ("-" )[1 ]
119+ await channel .edit (
120+ topic = channel .topic
121+ + self .ticket_info_format .format (
122+ ticket = ticket_id ,
123+ datetime = datetime .utcnow ().strftime ("%d/%m/%Y %H:%M:%S" ),
124+ author = author .display_name ,
125+ information = status ,
126+ )
127+ )
87128
88129 async def close_ticket (self , context ):
89130 try :
@@ -98,65 +139,94 @@ async def close_ticket(self, context):
98139 sessions = await self .config .guild (guild ).sessions ()
99140
100141 if str (channel .id ) not in sessions :
101- return await channel .send ("Make sure you are doing this within the ticket channel that you want to close." )
102- if await self .config .guild (guild ).ticket_role () not in [role .id for role in author .roles ]:
103- return await channel .send ("You do not have the proper role to manage tickets" )
142+ return await channel .send (
143+ "Make sure you are doing this within the ticket channel that you want to close."
144+ )
145+ if await self .config .guild (guild ).ticket_role () not in [
146+ role .id for role in author .roles
147+ ]:
148+ return await channel .send (
149+ "You do not have the proper role to manage tickets"
150+ )
104151 else :
105152 member = guild .get_member (sessions [str (channel .id )])
106- ticket_id = str (channel .name ).split ('-' )[1 ]
153+ ticket_id = str (channel .name ).split ("-" )[1 ]
107154
108155 closed_category = await self .config .guild (guild ).closed_category ()
109156 closed_category = self .bot .get_channel (closed_category )
110157
111- await channel .set_permissions (member , read_messages = True , send_messages = False )
112- await channel .edit (category = closed_category ,
113- topic = channel .topic + self .ticket_info_format .format (
114- ticket = ticket_id ,
115- datetime = datetime .utcnow ().strftime ('%d/%m/%Y %H:%M:%S' ),
116- author = author .display_name ,
117- information = 'Ticket closed' ))
158+ await channel .set_permissions (
159+ member , read_messages = True , send_messages = False
160+ )
161+ await channel .edit (
162+ category = closed_category ,
163+ topic = channel .topic
164+ + self .ticket_info_format .format (
165+ ticket = ticket_id ,
166+ datetime = datetime .utcnow ().strftime ("%d/%m/%Y %H:%M:%S" ),
167+ author = author .display_name ,
168+ information = "Ticket closed" ,
169+ ),
170+ )
118171
119172 async with self .config .guild (guild ).sessions () as session :
120- session .pop (channel .id , None )
173+ session .pop (channel .id , None )
121174
122175 async def purge_tickets (self , context ):
123176 try :
124177 guild = context .guild
125- closed_channels = [channel for channel in guild .channels if channel .category_id == await self .config .guild (guild ).closed_category ()]
178+ closed_channels = [
179+ channel
180+ for channel in guild .channels
181+ if channel .category_id
182+ == await self .config .guild (guild ).closed_category ()
183+ ]
126184 for channel in closed_channels :
127185 await channel .delete ()
128186
129- return ' All closed tickets removed!'
187+ return " All closed tickets removed!"
130188 except discord .Forbidden :
131- return ' I need permissions to manage channels.'
189+ return " I need permissions to manage channels."
132190
133191 async def set_default_message_ticket_channel (self , context , message ):
134192 guild = context .guild
135193
136194 await self .config .guild (guild ).default_message_ticket_channel .set (message )
137195
138- return ' Your default message has been set.'
196+ return " Your default message has been set."
139197
140198 async def automatic_setup (self , context ):
141199 guild = context .guild
142200
143201 try :
144202 overwrites = {
145- guild .default_role : discord .PermissionOverwrite (send_messages = False , read_messages = False ),
203+ guild .default_role : discord .PermissionOverwrite (
204+ send_messages = False , read_messages = False
205+ ),
146206 }
147207
148- category_channel = await guild .create_category ('Tickets' , overwrites = overwrites )
149- closed_category_channel = await guild .create_category ('Closed Tickets' , overwrites = overwrites )
208+ category_channel = await guild .create_category (
209+ "Tickets" , overwrites = overwrites
210+ )
211+ closed_category_channel = await guild .create_category (
212+ "Closed Tickets" , overwrites = overwrites
213+ )
150214
151- ticket_role = await guild .create_role (name = ' Ticket' )
215+ ticket_role = await guild .create_role (name = " Ticket" )
152216
153- await category_channel .set_permissions (ticket_role , read_messages = True , send_messages = True )
154- await closed_category_channel .set_permissions (ticket_role , read_messages = True , send_messages = True )
217+ await category_channel .set_permissions (
218+ ticket_role , read_messages = True , send_messages = True
219+ )
220+ await closed_category_channel .set_permissions (
221+ ticket_role , read_messages = True , send_messages = True
222+ )
155223
156224 await self .config .guild (guild ).category .set (category_channel .id )
157- await self .config .guild (guild ).closed_category .set (closed_category_channel .id )
225+ await self .config .guild (guild ).closed_category .set (
226+ closed_category_channel .id
227+ )
158228 await self .config .guild (guild ).ticket_role .set (ticket_role .id )
159229
160- return ' :tada: Fabulous! You\ ' re all done! Now add the `Ticket` role to anyone who you deem good enough to handle tickets. And if you care, you can change the name of the role and category if you _really_ want to.'
230+ return " :tada: Fabulous! You're all done! Now add the `Ticket` role to anyone who you deem good enough to handle tickets. And if you care, you can change the name of the role and category if you _really_ want to."
161231 except discord .Forbidden :
162- return ' That didn\ ' t go well... I need permissions to manage channels and manage roles. :rolling_eyes:'
232+ return " That didn't go well... I need permissions to manage channels and manage roles. :rolling_eyes:"
0 commit comments