20
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
SOFTWARE.
23
-
24
23
'''
25
24
25
+ GUILD_ID = 364718578223808514
26
+
26
27
import discord
27
28
from discord .ext import commands
28
29
import asyncio
35
36
import os
36
37
import re
37
38
import textwrap
39
+ import string
40
+
38
41
39
42
class Modmail (commands .Bot ):
40
43
def __init__ (self ):
@@ -97,8 +100,13 @@ async def on_connect(self):
97
100
print ('---------------' )
98
101
print ('Modmail connected!' )
99
102
103
+ @property
104
+ def guild_id (self ):
105
+ return int (os .environ .get ('GUILD_ID' )) or GUILD_ID
106
+
100
107
async def on_ready (self ):
101
108
'''Bot startup, sets uptime.'''
109
+ self .guild = discord .utils .get (self .guilds , id = self .guild_id )
102
110
if not hasattr (self , 'uptime' ):
103
111
self .uptime = datetime .datetime .utcnow ()
104
112
print (textwrap .dedent (f'''
@@ -130,11 +138,18 @@ async def setup(self, ctx):
130
138
if discord .utils .get (ctx .guild .categories , name = 'modmail' ):
131
139
return await ctx .send ('This server is already set up.' )
132
140
133
-
134
141
categ = await ctx .guild .create_category (name = 'modmail' , overwrites = self .overwrites (ctx ))
135
142
await categ .edit (position = 0 )
136
143
await ctx .send ('Successfully set up server.' )
137
144
145
+ @commands .command ()
146
+ @commands .has_permissions (manage_guild = True )
147
+ async def close (self , ctx ):
148
+ user_id = int (ctx .channel .topic .split (': ' )[1 ])
149
+ user = self .get_user (user_id )
150
+ await user .send ('A moderator has closed this modmail session.' )
151
+ await ctx .channel .delete ()
152
+
138
153
@commands .command ()
139
154
async def ping (self , ctx ):
140
155
"""Pong! Returns your websocket latency."""
@@ -150,14 +165,97 @@ def guess_modroles(self, ctx):
150
165
if role .permissions .manage_guild :
151
166
yield role
152
167
168
+ def format_info (self , user ):
169
+ '''Get information about a member of a server'''
170
+ server = self .guild
171
+ user = self .guild .get_member (user .id )
172
+ avi = user .avatar_url
173
+ roles = sorted (user .roles , key = lambda c : c .position )
174
+
175
+ for role in roles :
176
+ if str (role .color ) != "#000000" :
177
+ color = role .color
178
+ if 'color' not in locals ():
179
+ color = 0
180
+
181
+ rolenames = ', ' .join ([r .name for r in roles if r .name != "@everyone" ]) or 'None'
182
+ time = datetime .datetime .utcnow ()
183
+ desc = 'Modmail thread started.'
184
+ member_number = sorted (server .members , key = lambda m : m .joined_at ).index (user ) + 1
185
+
186
+ em = discord .Embed (colour = color , description = desc , timestamp = time )
187
+ em .add_field (name = 'Nick' , value = user .nick , inline = True )
188
+ em .add_field (name = 'Member No.' ,value = str (member_number ),inline = True )
189
+ em .add_field (name = 'Account Created' , value = str ((time - user .created_at ).days )+ ' days ago.' )
190
+ em .add_field (name = 'Joined' , value = str ((time - user .joined_at ).days )+ ' days ago.' )
191
+ em .add_field (name = 'Roles' , value = rolenames , inline = True )
192
+ em .set_footer (text = 'User ID: ' + str (user .id ))
193
+ em .set_thumbnail (url = avi )
194
+ em .set_author (name = user , icon_url = server .icon_url )
195
+
196
+ return em
197
+
198
+ async def send_mail (self , message , channel ):
199
+ author = message .author
200
+ if isinstance (channel , discord .TextChannel ):
201
+ fmt = f'» **{ author } :** { message .content } '
202
+ else :
203
+ fmt = f'» **{ author } (Mod):** { message .content } '
204
+ embed = None
205
+ if message .embeds :
206
+ embed = message .embeds [0 ]
207
+ if message .attachments :
208
+ fmt += '\n \n **Attachment: ' + message .attachments [0 ].url
209
+ await channel .send (fmt , embed = embed )
210
+
211
+ async def process_reply (self , message ):
212
+ await message .delete ()
213
+ await self .send_mail (message , message .channel )
214
+ user_id = int (message .channel .topic .split (': ' )[1 ])
215
+ user = self .get_user (user_id )
216
+ await self .send_mail (message , user )
217
+
218
+ def format_name (self , author ):
219
+ name = author .name
220
+ new_name = ''
221
+ for letter in name :
222
+ if letter in string .ascii_letters + string .digits :
223
+ new_name += letter
224
+ if not new_name :
225
+ new_name = 'null'
226
+ new_name += f'-{ author .discriminator } '
227
+ return new_name
153
228
154
229
async def process_modmail (self , message ):
155
- pass
230
+ guild = self .guild
231
+ author = message .author
232
+ topic = f'User ID: { author .id } '
233
+ channel = discord .utils .get (guild .text_channels , topic = topic )
234
+ categ = discord .utils .get (guild .categories , name = 'modmail' )
235
+
236
+ if channel is not None :
237
+ await self .send_mail (message , channel )
238
+ else :
239
+ channel = await guild .create_text_channel (
240
+ name = self .format_name (author ),
241
+ category = categ
242
+ )
243
+ await channel .edit (topic = topic )
244
+ await channel .send (embed = self .format_info (author ))
245
+ await self .send_mail (message , channel )
156
246
157
247
async def on_message (self , message ):
248
+ if message .author .bot :
249
+ return
158
250
await self .process_commands (message )
159
251
if isinstance (message .channel , discord .DMChannel ):
160
252
await self .process_modmail (message )
253
+ else :
254
+ c_id = message .channel .category_id
255
+ categ = discord .utils .get (message .guild .categories , id = c_id )
256
+ if categ is not None :
257
+ if categ .name == 'modmail' :
258
+ await self .process_reply (message )
161
259
162
260
if __name__ == '__main__' :
163
- Modmail .init ('token' )
261
+ Modmail .init ()
0 commit comments