restructured handle_message and added GifJokes
This commit is contained in:
37
jokes.py
37
jokes.py
@@ -72,6 +72,41 @@ class DominosJoke(Joke):
|
|||||||
await message.reply(msg)
|
await message.reply(msg)
|
||||||
|
|
||||||
|
|
||||||
|
class GifJoke(Joke):
|
||||||
|
url: str
|
||||||
|
|
||||||
|
def __init__(self, url: str):
|
||||||
|
self.url = url
|
||||||
|
|
||||||
|
async def respond(self, message: discord.Message, client: discord.Client, scan_res):
|
||||||
|
await message.channel.send(self.url)
|
||||||
|
|
||||||
|
|
||||||
|
class BeansJoke(GifJoke):
|
||||||
|
@property
|
||||||
|
def regex(self) -> re.Pattern:
|
||||||
|
return re.compile('beans', re.IGNORECASE)
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(url='https://c.tenor.com/TjX1yORoln0AAAAM/this-is-beans-beans.gif')
|
||||||
|
|
||||||
|
async def respond(self, message: discord.Message, client: discord.Client, scan_res):
|
||||||
|
await message.reply('Somebody help! I\'ve got beans in my motherboard!\n')
|
||||||
|
await super().respond(message, client, scan_res)
|
||||||
|
|
||||||
|
|
||||||
|
class NotLikeThisJoke(GifJoke):
|
||||||
|
@property
|
||||||
|
def regex(self) -> re.Pattern:
|
||||||
|
return re.compile('not like this', re.IGNORECASE)
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(url='https://tenor.com/view/not-like-this-the-matrix-panic-neo-angry-gif-5216157')
|
||||||
|
|
||||||
|
async def respond(self, message: discord.Message, client: discord.Client, scan_res):
|
||||||
|
await message.reply(self.url)
|
||||||
|
|
||||||
|
|
||||||
pattern = 'NP: {<DT>?<JJ>*<NN>}'
|
pattern = 'NP: {<DT>?<JJ>*<NN>}'
|
||||||
cp = nltk.RegexpParser(pattern)
|
cp = nltk.RegexpParser(pattern)
|
||||||
|
|
||||||
@@ -103,4 +138,4 @@ def unblack(s):
|
|||||||
if tag.startswith('JJ') or tag.startswith('NN'):
|
if tag.startswith('JJ') or tag.startswith('NN'):
|
||||||
for text, tag, iob in tag_list[i + 1:]:
|
for text, tag, iob in tag_list[i + 1:]:
|
||||||
if tag.startswith('NN'):
|
if tag.startswith('NN'):
|
||||||
return f'Or as I would say, {text.lower()}'
|
return f'Or as I would say, {text.lower()}'
|
||||||
|
|||||||
70
robopage.py
70
robopage.py
@@ -22,7 +22,7 @@ class RoboPage(discord.Client):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(RoboPage, self).__init__(*args, **kwargs)
|
super(RoboPage, self).__init__(*args, **kwargs)
|
||||||
attrs = filter(lambda n: n.endswith('Joke') and not n.startswith('Joke'), dir(jokes))
|
attrs = filter(lambda n: n.endswith('Joke') and n not in ['Joke', 'GifJoke'], dir(jokes))
|
||||||
attrs = map(lambda n: getattr(jokes, n)(), attrs)
|
attrs = map(lambda n: getattr(jokes, n)(), attrs)
|
||||||
self.jokes = list(attrs)
|
self.jokes = list(attrs)
|
||||||
self.most_regex = re.compile(
|
self.most_regex = re.compile(
|
||||||
@@ -56,43 +56,39 @@ class RoboPage(discord.Client):
|
|||||||
# await alive()
|
# await alive()
|
||||||
|
|
||||||
async def handle_message(self, message):
|
async def handle_message(self, message):
|
||||||
if hasattr(self, 'data'):
|
if message.author != self.user:
|
||||||
await self.data.add_msg(message)
|
if hasattr(self, 'data'):
|
||||||
|
await self.data.add_msg(message)
|
||||||
|
|
||||||
async with self.data.lock:
|
if (m := self.leaderboard_regex.match(message.content)) is not None:
|
||||||
if message.author != self.user:
|
try:
|
||||||
if (m := self.leaderboard_regex.match(message.content)) is not None:
|
await message.reply(await self.data.emoji_leaderboard(
|
||||||
try:
|
client=self,
|
||||||
await message.reply(await self.data.emoji_leaderboard(
|
emoji_name=m.group('emoji').lower(),
|
||||||
|
days=14
|
||||||
|
))
|
||||||
|
except KeyError as e:
|
||||||
|
LOGGER.exception(e)
|
||||||
|
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
|
||||||
|
return
|
||||||
|
|
||||||
|
elif (m := self.most_regex.match(message.content)) is not None:
|
||||||
|
days = m.group('days') or 14
|
||||||
|
try:
|
||||||
|
await message.reply(
|
||||||
|
await self.data.biggest_single(
|
||||||
client=self,
|
client=self,
|
||||||
emoji_name=m.group('emoji').lower(),
|
emoji=m.group('emoji').lower(),
|
||||||
days=14
|
days=int(days)
|
||||||
))
|
))
|
||||||
except KeyError as e:
|
except IndexError as e:
|
||||||
LOGGER.exception(e)
|
await message.reply('NObody')
|
||||||
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
|
return
|
||||||
|
|
||||||
elif (m := self.most_regex.match(message.content)) is not None:
|
for joke in self.jokes:
|
||||||
days = m.group('days') or 14
|
if (scan_res := joke.scan(message)):
|
||||||
try:
|
LOGGER.info(f'{joke.__class__.__name__} detected: {message.content}, {scan_res.group()}')
|
||||||
await message.reply(
|
await joke.respond(message, self, scan_res)
|
||||||
await self.data.biggest_single(client=self, emoji=m.group('emoji').lower(), days=int(days))
|
|
||||||
)
|
|
||||||
except IndexError as e:
|
|
||||||
await message.reply('NObody')
|
|
||||||
|
|
||||||
elif 'not like this' in message.content.lower():
|
|
||||||
await message.reply(self.gifs['not like this'])
|
|
||||||
|
|
||||||
elif 'beans' in message.content.lower():
|
|
||||||
await message.reply('Somebody help! I\'ve got beans in my motherboard!\n')
|
|
||||||
await message.channel.send(self.gifs['beans'])
|
|
||||||
|
|
||||||
else:
|
|
||||||
for joke in self.jokes:
|
|
||||||
if (scan_res := joke.scan(message)):
|
|
||||||
print(f'{joke.__class__.__name__} detected:\n{message.content}\n{scan_res}')
|
|
||||||
await joke.respond(message, self, scan_res)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@@ -116,13 +112,15 @@ if __name__ == '__main__':
|
|||||||
@client.event
|
@client.event
|
||||||
async def on_raw_reaction_add(payload):
|
async def on_raw_reaction_add(payload):
|
||||||
LOGGER.info(payload)
|
LOGGER.info(payload)
|
||||||
await client.data.update_reaction(payload=payload, client=client)
|
if hasattr(client, 'data'):
|
||||||
|
await client.data.update_reaction(payload=payload, client=client)
|
||||||
|
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_raw_reaction_remove(payload):
|
async def on_raw_reaction_remove(payload):
|
||||||
LOGGER.info(payload)
|
LOGGER.info(payload)
|
||||||
await client.data.update_reaction(payload=payload, client=client)
|
if hasattr(client, 'data'):
|
||||||
|
await client.data.update_reaction(payload=payload, client=client)
|
||||||
|
|
||||||
|
|
||||||
client.run()
|
client.run()
|
||||||
|
|||||||
Reference in New Issue
Block a user