initial commit
This commit is contained in:
129
robopage.py
Normal file
129
robopage.py
Normal file
@@ -0,0 +1,129 @@
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
import discord
|
||||
import nltk
|
||||
from dotenv import load_dotenv
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
class RoboPage(discord.Client):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(RoboPage, self).__init__(*args, **kwargs)
|
||||
self.jokes = [
|
||||
CumJoke(),
|
||||
BlackJoke(),
|
||||
AssJoke()
|
||||
]
|
||||
|
||||
def run(self):
|
||||
return super().run(os.getenv('DISCORD_TOKEN'))
|
||||
|
||||
async def handle_ready(self):
|
||||
channel: discord.TextChannel = discord.utils.get(self.get_all_channels(), name='robotics-facility')
|
||||
await channel.send(f"I'm aliiiiiive {discord.utils.get(self.emojis, name='kaylon')}")
|
||||
|
||||
async def handle_message(self, message):
|
||||
if message.author != self.user:
|
||||
for joke in self.jokes:
|
||||
if (match := joke.regex.search(message.content)):
|
||||
print(f'{joke.__class__.__name__} detected:\n{message.content}\n{match.group(0)}')
|
||||
await joke.respond(message, self, match)
|
||||
|
||||
class Joke:
|
||||
@property
|
||||
def regex(self) -> re.Pattern:
|
||||
raise NotImplementedError
|
||||
|
||||
async def respond(self, message: discord.Message, client: discord.Client, match: re.Match):
|
||||
raise NotImplementedError
|
||||
|
||||
class CumJoke(Joke):
|
||||
@property
|
||||
def regex(self) -> re.Pattern:
|
||||
return re.compile('|'.join([
|
||||
'come',
|
||||
'coming',
|
||||
'came',
|
||||
'cum',
|
||||
'cumming',
|
||||
'cummed'
|
||||
]), re.IGNORECASE)
|
||||
|
||||
async def respond(self, message: discord.Message, client: discord.Client, match: re.Match):
|
||||
if not match.group(0).startswith('be'):
|
||||
await message.add_reaction(discord.utils.get(client.emojis, name='kaylon'))
|
||||
|
||||
|
||||
class BlackJoke(Joke):
|
||||
@property
|
||||
def regex(self) -> re.Pattern:
|
||||
return re.compile('black (\w+)', re.IGNORECASE)
|
||||
|
||||
async def respond(self, message: discord.Message, client: discord.Client, match: re.Match):
|
||||
msg = await message.reply(unblack(message.content))
|
||||
await msg.add_reaction(discord.utils.get(client.emojis, name='kaylon'))
|
||||
|
||||
|
||||
class AssJoke(Joke):
|
||||
@property
|
||||
def regex(self) -> re.Pattern:
|
||||
return re.compile('ass( |\-)(\w)', re.IGNORECASE)
|
||||
|
||||
async def respond(self, message: discord.Message, client: discord.Client, match: re.Match):
|
||||
res = assify(message.content)
|
||||
if res is not None:
|
||||
await message.reply(f'{res} {discord.utils.get(client.emojis, name="kaylon")}')
|
||||
|
||||
|
||||
pattern = 'NP: {<DT>?<JJ>*<NN>}'
|
||||
cp = nltk.RegexpParser(pattern)
|
||||
|
||||
def token_list(s):
|
||||
return nltk.chunk.tree2conlltags(
|
||||
cp.parse(
|
||||
nltk.pos_tag(
|
||||
nltk.word_tokenize(s)
|
||||
)))
|
||||
|
||||
|
||||
def assify(s):
|
||||
tag_list = token_list(s)
|
||||
for i, (text, tag, iob) in enumerate(tag_list):
|
||||
if text[-3:].lower() == 'ass':
|
||||
try:
|
||||
next_tag = tag_list[i+1][1]
|
||||
if next_tag == 'NN' or next_tag == 'NNS':
|
||||
return f'ass-{tag_list[i+1][0]}'
|
||||
except IndexError as e:
|
||||
return
|
||||
|
||||
|
||||
def unblack(s):
|
||||
tag_list = token_list(s)
|
||||
for i, (text, tag, iob) in enumerate(tag_list):
|
||||
if text.lower() == 'black':
|
||||
if tag.startswith('JJ') or tag.startswith('NN'):
|
||||
for text, tag, iob in tag_list[i+1:]:
|
||||
if tag.startswith('NN'):
|
||||
return f'Or as I would say, {text.lower()}'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_dotenv()
|
||||
TOKEN = os.getenv('DISCORD_TOKEN')
|
||||
|
||||
client = RoboPage()
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print(f'{client.user} has connected to Discord!')
|
||||
await client.handle_ready()
|
||||
|
||||
@client.event
|
||||
async def on_message(message: discord.Message):
|
||||
await client.handle_message(message)
|
||||
|
||||
client.run()
|
||||
Reference in New Issue
Block a user