-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscordbot.py
83 lines (71 loc) · 3.26 KB
/
discordbot.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
import discord
import asyncio
import json
import datetime as dt
import logging
logger = logging.getLogger(__name__)
class DiscordClient(discord.Client):
def __init__(self, bot, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bot = bot
self.last_deleted_uid = None
self.del_vids_channel = None
self.logger = logger
async def start(self, token):
self.logger.info('Starting discord')
self.bot.socket.on('delete', self.handle_vid_delete)
self.bot.socket.on('chatMsg', self.handle_chatMsg)
asyncio.create_task(super().start(token))
async def add_del_vids_channel(self, channel_id: int):
'''Add channel id for reporting deleted videos.'''
if not self.is_ready():
asyncio.create_task(self.add_del_vids_channel(channel_id))
return
self.logger.debug(f'Getting channel {channel_id}')
self.del_vids_channel = self.get_channel(channel_id)
if self.del_vids_channel is None:
self.logger.error(f'Discord channel {channel_id} not found')
return
self.logger.info('Connected to deleted-vids channel')
async def handle_vid_delete(self, data):
self.last_deleted_uid = data['uid']
async def handle_chatMsg(self, data):
#on bot event 'chatMsg'
if not self.is_ready(): return
if 'db' not in self.bot.modules or not self.bot.modules['db']: return
if not self.del_vids_channel: return
if data['msg'].split(' ', 1)[0] != 'deleted': return
try:
if data['meta']['action'] != True or data['meta']['addClass'] != 'action': return
except KeyError: return
if not self.bot.userlist[data['username']].rank >= 2: return
title = data['msg'].split('"', 1)[1].rsplit('"', 1)[0]
async with self.bot.db.pool.acquire() as connection:
async with connection.transaction():
db_result = await connection.fetch("""select videos.video_type, videos.video_id, videos.video_title, video_adds.from_username
from videos
inner join video_adds
on videos.video_id = video_adds.video_id and videos.video_type = video_adds.video_type
where videos.video_title = $1
order by video_adds.ts desc
limit 1
""",
title
)
db_result = db_result[0] #grab the first (and only) row
from_username = db_result['from_username']
embed = discord.Embed()
embed.title = title
embed.type = 'rich'
embed.set_author(name='Video deleted')
embed.colour = 0xFF6666
embed.add_field(name='Posted by', value=from_username)
embed.add_field(name='Deleted by', value=data['username'])
if db_result['video_type'] == 'yt':
embed.description = 'https://youtu.be/' + db_result['video_id']
await self.del_vids_channel.send(embed=embed)
async def init(bot, config_file):
config = config_file['modules']['discord']
bot.modules['discord'] = DiscordClient(bot)
await bot.modules['discord'].start(config['token'])
await bot.modules['discord'].add_del_vids_channel(config['deleted-vids-channel'])