|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
|
|
|
import discord
|
|
|
|
|
|
|
|
from commands import CommandError
|
|
|
|
|
|
|
|
|
|
|
|
class DiscordClient(discord.Client):
|
|
|
|
|
|
|
|
def __init__(self, config, logger, commands):
|
|
|
|
self.config = config
|
|
|
|
self.logger = logger
|
|
|
|
self.commands = commands
|
|
|
|
self.supported_commands = [
|
|
|
|
(re.compile(r'^!(bella(gram|pics)|insta(gram|bella))$'), self._do_bellagram),
|
|
|
|
(re.compile(r'^!yt\s+(?P<q>")?(?P<query>.+)(?(q)")$'), self._do_yt),
|
|
|
|
]
|
|
|
|
self.cooldowns = {p.pattern: {} for p, _ in self.supported_commands}
|
|
|
|
super(DiscordClient, self).__init__()
|
|
|
|
|
|
|
|
async def start_(self):
|
|
|
|
token = self.config['Discord'].get('token')
|
|
|
|
await self.start(token)
|
|
|
|
|
|
|
|
async def on_ready(self):
|
|
|
|
self.logger.info('Logged in as {0}'.format(self.user.name))
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
def check_cooldown(pattern, user):
|
|
|
|
retries = self.config['Discord'].getint('cooldown_retries')
|
|
|
|
seconds = self.config['Discord'].getint('cooldown_seconds')
|
|
|
|
if user not in self.cooldowns[pattern]:
|
|
|
|
self.cooldowns[pattern][user] = dict(tries=0, time=0)
|
|
|
|
cooldown = self.cooldowns[pattern][user]
|
|
|
|
if cooldown['tries'] >= retries and time.time() - cooldown['time'] <= seconds:
|
|
|
|
return False
|
|
|
|
cooldown['tries'] = 1 if cooldown['tries'] >= retries else cooldown['tries'] + 1
|
|
|
|
cooldown['time'] = time.time()
|
|
|
|
return True
|
|
|
|
for pattern, action in self.supported_commands:
|
|
|
|
m = pattern.match(message.content)
|
|
|
|
if m:
|
|
|
|
if check_cooldown(pattern.pattern, message.author.id):
|
|
|
|
await action(message, **m.groupdict())
|
|
|
|
else:
|
|
|
|
await self.send_message(message.channel,
|
|
|
|
'Sorry {0}, you have to wait a while before running '
|
|
|
|
'the same command again'.format(message.author.mention))
|
|
|
|
|
|
|
|
async def _do_bellagram(self, message, **kwargs):
|
|
|
|
try:
|
|
|
|
bellagram = self.commands.bellagram()
|
|
|
|
except CommandError as e:
|
|
|
|
await self.send_message(message.channel, 'Sorry {0}, {1}'.format(message.author.mention, e))
|
|
|
|
else:
|
|
|
|
embed = discord.Embed(title=bellagram['title'], url=bellagram['url'], color=0x8545bc)
|
|
|
|
embed.set_image(url=bellagram['display_url'])
|
|
|
|
embed.set_author(name=bellagram['owner'], url=bellagram['owner_url'],
|
|
|
|
icon_url=bellagram['owner_pic_url'])
|
|
|
|
await self.send_message(message.channel, embed=embed)
|
|
|
|
|
|
|
|
async def _do_yt(self, message, query, **kwargs):
|
|
|
|
try:
|
|
|
|
result = self.commands.query_youtube(query)
|
|
|
|
except CommandError as e:
|
|
|
|
await self.send_message(message.channel, 'Sorry {0}, {1}'.format(message.author.mention, e))
|
|
|
|
else:
|
|
|
|
embed = discord.Embed(title=result['title'], url=result['url'],
|
|
|
|
description=result['description'], color=0xff0000)
|
|
|
|
embed.set_thumbnail(url=result['thumbnail_url'])
|
|
|
|
embed.set_author(name=result['channel_title'], url=result['channel_url'],
|
|
|
|
icon_url=result['channel_thumbnail_url'])
|
|
|
|
await self.send_message(message.channel, embed=embed)
|