-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
461 lines (364 loc) · 15.9 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""
MIT License
Copyright (c) 2017-2019 kyb3r
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__version__ = '2.2.1'
import asyncio
import textwrap
import datetime
import os
import re
from types import SimpleNamespace
import discord
import aiohttp
from discord.ext import commands
from discord.ext.commands.view import StringView
from colorama import init, Fore, Style
import emoji
from core.api import Github, ModmailApiClient
from core.thread import ThreadManager
from core.config import ConfigManager
from core.changelog import ChangeLog
init()
line = Fore.BLACK + Style.BRIGHT + '-------------------------' + Style.RESET_ALL
class ModmailBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=self.get_pre)
self.version = __version__
self.start_time = datetime.datetime.utcnow()
self.threads = ThreadManager(self)
self.session = aiohttp.ClientSession(loop=self.loop)
self.config = ConfigManager(self)
self.modmail_api = ModmailApiClient(self)
self.data_task = self.loop.create_task(self.data_loop())
self.autoupdate_task = self.loop.create_task(self.autoupdate_loop())
self._add_commands()
def _add_commands(self):
"""Adds commands automatically"""
self.remove_command('help')
print(line + Fore.CYAN)
print('┌┬┐┌─┐┌┬┐┌┬┐┌─┐┬┬',
'││││ │ │││││├─┤││',
'┴ ┴└─┘─┴┘┴ ┴┴ ┴┴┴─┘', sep='\n')
print(f'v{__version__}')
print('Authors: kyb3r, fourjr' + Style.RESET_ALL)
print(line + Fore.CYAN)
for file in os.listdir('cogs'):
if not file.endswith('.py'):
continue
cog = f'cogs.{file[:-3]}'
print(f'Loading {cog}')
self.load_extension(cog)
async def logout(self):
await self.session.close()
self.data_task.cancel()
self.autoupdate_task.cancel()
await super().logout()
def run(self):
try:
super().run(self.token)
finally:
print(Fore.RED + ' - Shutting down bot' + Style.RESET_ALL)
@property
def log_channel(self):
channel_id = self.config.get('log_channel_id')
if channel_id is not None:
return self.get_channel(int(channel_id))
else:
return self.main_category.channels[0]
@property
def snippets(self):
return {k: v for k, v in self.config.get('snippets', {}).items() if v}
@property
def aliases(self):
return {k: v for k, v in self.config.get('aliases', {}).items() if v}
@property
def token(self):
return self.config.token
@property
def guild_id(self):
return int(self.config.guild_id)
@property
def guild(self):
'''The guild that the bot is serving (the server where users message it from)'''
return discord.utils.get(self.guilds, id=self.guild_id)
@property
def modmail_guild(self):
'''The guild that the bot is operating in (where the bot is creating threads)'''
modmail_guild_id = self.config.get('modmail_guild_id')
if not modmail_guild_id:
return self.guild
else:
return discord.utils.get(self.guilds, id=int(modmail_guild_id))
@property
def using_multiple_server_setup(self):
return self.modmail_guild != self.guild
@property
def main_category(self):
category_id = self.config.get('main_category_id')
if category_id is not None:
return discord.utils.get(self.modmail_guild.categories, id=int(category_id))
if self.modmail_guild:
return discord.utils.get(self.modmail_guild.categories, name='Mod Mail')
@property
def blocked_users(self):
return self.config.get('blocked', {})
@property
def prefix(self):
return self.config.get('prefix', '?')
@staticmethod
async def get_pre(bot, message):
"""Returns the prefix."""
return [bot.prefix, f'<@{bot.user.id}> ', f'<@!{bot.user.id}> ']
async def on_connect(self):
print(line + Fore.RED + Style.BRIGHT)
await self.validate_api_token()
print(line)
print(Fore.CYAN + 'Connected to gateway.')
await self.config.refresh()
status = self.config.get('status')
if status:
await self.change_presence(activity=discord.Game(status))
async def on_ready(self):
"""Bot startup, sets uptime."""
print(textwrap.dedent(f"""
{line}
{Fore.CYAN}Client ready.
{line}
{Fore.CYAN}Logged in as: {self.user}
{Fore.CYAN}User ID: {self.user.id}
{Fore.CYAN}Guild ID: {self.guild.id if self.guild else 0}
{line}
""").strip())
if not self.guild:
print(Fore.RED + Style.BRIGHT + 'WARNING - The GUILD_ID provided does not exist!' + Style.RESET_ALL)
else:
await self.threads.populate_cache()
async def process_modmail(self, message):
"""Processes messages sent to the bot."""
ctx = SimpleNamespace(bot=self, guild=self.modmail_guild)
converter = commands.EmojiConverter()
blocked_emoji = self.config.get('blocked_emoji', '🚫')
sent_emoji = self.config.get('sent_emoji', '✅')
if blocked_emoji not in emoji.UNICODE_EMOJI:
try:
blocked_emoji = await converter.convert(ctx, blocked_emoji.strip(':'))
except:
pass
if sent_emoji not in emoji.UNICODE_EMOJI:
try:
sent_emoji = await converter.convert(ctx, sent_emoji.strip(':'))
except:
pass
reaction = blocked_emoji if str(message.author.id) in self.blocked_users else sent_emoji
try:
await message.add_reaction(reaction)
except:
pass
blocked_em = discord.Embed(
title='Message not sent!',
color=discord.Color.red(),
description='You have been blocked from using modmail.'
)
if str(message.author.id) in self.blocked_users:
await message.author.send(embed=blocked_em)
else:
thread = await self.threads.find_or_create(message.author)
await thread.send(message)
async def get_context(self, message, *, cls=commands.Context):
"""
Returns the invocation context from the message.
Supports getting the prefix from database as well as command aliases.
"""
view = StringView(message.content)
ctx = cls(prefix=None, view=view, bot=self, message=message)
if self._skip_check(message.author.id, self.user.id):
return ctx
prefixes = [self.prefix, f'<@{bot.user.id}> ', f'<@!{bot.user.id}>']
invoked_prefix = discord.utils.find(view.skip_string, prefixes)
if invoked_prefix is None:
return ctx
invoker = view.get_word().lower()
# Check if there is any aliases being called.
alias = self.config.get('aliases', {}).get(invoker)
if alias is not None:
ctx._alias_invoked = True
_len = len(f'{invoked_prefix}{invoker}')
ctx.view = view = StringView(f'{alias}{ctx.message.content[_len:]}')
invoker = view.get_word()
ctx.invoked_with = invoker
ctx.prefix = self.prefix # Sane prefix (No mentions)
ctx.command = self.all_commands.get(invoker)
if ctx.command is self.get_command('eval') and hasattr(ctx, '_alias_invoked'):
# ctx.command.checks = None # Let anyone use the command.
pass
return ctx
async def on_message(self, message):
if message.type == discord.MessageType.pins_add and message.author == self.user:
await message.delete()
if message.author.bot:
return
if isinstance(message.channel, discord.DMChannel):
return await self.process_modmail(message)
prefix = self.prefix
if message.content.startswith(prefix):
cmd = message.content[len(prefix):].strip()
if cmd in self.snippets:
message.content = f'{prefix}reply {self.snippets[cmd]}'
await self.process_commands(message)
async def on_guild_channel_delete(self, channel):
if channel.guild != self.modmail_guild:
return
mod = None
audit_logs = self.modmail_guild.audit_logs()
entry = await audit_logs.find(lambda e: e.target.id == channel.id)
mod = entry.user
if mod.bot:
return
thread = await self.threads.find(channel=channel)
if not thread:
return
await thread.close(closer=mod, silent=True, delete_channel=False)
async def on_message_delete(self, message):
"""Support for deleting linked messages"""
if message.embeds and not isinstance(message.channel, discord.DMChannel):
message_id = str(message.embeds[0].author.url).split('/')[-1]
if message_id.isdigit():
thread = await self.threads.find(channel=message.channel)
channel = thread.recipient.dm_channel
async for msg in channel.history():
if msg.embeds and msg.embeds[0].author:
url = str(msg.embeds[0].author.url)
if message_id == url.split('/')[-1]:
return await msg.delete()
async def on_message_edit(self, before, after):
if before.author.bot:
return
if isinstance(before.channel, discord.DMChannel):
thread = await self.threads.find(recipient=before.author)
async for msg in thread.channel.history():
if msg.embeds:
embed = msg.embeds[0]
matches = str(embed.author.url).split('/')
if matches and matches[-1] == str(before.id):
if ' - (Edited)' not in embed.footer.text:
embed.set_footer(text=embed.footer.text + ' - (Edited)')
embed.description = after.content
await msg.edit(embed=embed)
break
async def on_command_error(self, ctx, error):
if isinstance(error, (commands.MissingRequiredArgument, commands.UserInputError)):
await ctx.invoke(self.get_command('help'), command=str(ctx.command))
else:
raise error
def overwrites(self, ctx):
"""Permision overwrites for the guild."""
overwrites = {
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False),
ctx.guild.me: discord.PermissionOverwrite(read_messages=True)
}
for role in ctx.guild.roles:
if role.permissions.manage_guild:
overwrites[role] = discord.PermissionOverwrite(read_messages=True)
return overwrites
async def validate_api_token(self):
valid = True
try:
self.config.modmail_api_token
except KeyError:
print('MODMAIL_API_TOKEN not found.')
print('Set a config variable called MODMAIL_API_TOKEN with a token from https://dashboard.modmail.tk')
valid = False
else:
valid = await self.modmail_api.validate_token()
if not valid:
print('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk')
finally:
if not valid:
await self.logout()
else:
username = (await self.modmail_api.get_user_info())['user']['username']
print(Style.RESET_ALL + Fore.CYAN + 'Validated token.' )
print(f'GitHub user: {username}' + Style.RESET_ALL)
async def data_loop(self):
await self.wait_until_ready()
self.owner = (await self.application_info()).owner
while True:
data = {
"owner_name": str(self.owner),
"owner_id": self.owner.id,
"bot_id": self.user.id,
"bot_name": str(self.user),
"guild_id": self.guild_id,
"guild_name": self.guild.name,
"member_count": len(self.guild.members),
"uptime": (datetime.datetime.utcnow() - self.start_time).total_seconds(),
"latency": f'{self.ws.latency * 1000:.4f}',
"version": __version__
}
await self.modmail_api.post_metadata(data)
await asyncio.sleep(3600)
async def autoupdate_loop(self):
while True:
if self.config.get('disable_autoupdates'):
await asyncio.sleep(3600)
continue
metadata = await self.modmail_api.get_metadata()
if metadata['latest_version'] != self.version:
data = await self.modmail_api.update_repository()
em = discord.Embed(color=discord.Color.green())
commit_data = data['data']
user = data['user']
em.set_author(name=user['username'] + ' - Updating Bot', icon_url=user['avatar_url'], url=user['url'])
em.set_footer(text=f"Updating modmail v{self.version} -> v{metadata['latest_version']}")
changelog = await ChangeLog.from_repo(self)
latest = changelog.latest_version
em.description = latest.description
for name, value in latest.fields.items():
em.add_field(name=name, value=value)
if commit_data:
message = commit_data['commit']['message']
html_url = commit_data["html_url"]
short_sha = commit_data['sha'][:6]
em.add_field(name='Merge Commit', value=f"[`{short_sha}`]({html_url}) {message} - {user['username']}")
print('Updating bot.')
channel = self.log_channel
await channel.send(embed=em)
await asyncio.sleep(3600)
async def get_latest_updates(self, limit=3):
latest_commits = ''
async for commit in Github(self).get_latest_commits(limit=limit):
short_sha = commit['sha'][:6]
html_url = commit['html_url']
message = commit['commit']['message'].splitlines()[0]
latest_commits += f'[`{short_sha}`]({html_url}) {message}\n'
return latest_commits
@property
def uptime(self):
now = datetime.datetime.utcnow()
delta = now - self.start_time
hours, remainder = divmod(int(delta.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)
fmt = '{h}h {m}m {s}s'
if days:
fmt = '{d}d ' + fmt
return fmt.format(d=days, h=hours, m=minutes, s=seconds)
if __name__ == '__main__':
bot = ModmailBot()
bot.run()