Skip to content

Instantly share code, notes, and snippets.

@theelous3
Created February 9, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theelous3/7924006180611c8043f29cb4f39e1b33 to your computer and use it in GitHub Desktop.
Save theelous3/7924006180611c8043f29cb4f39e1b33 to your computer and use it in GitHub Desktop.
import re
import string
from random import choice
import curio
from curio.socket import *
from talkology import *
class Ploblin:
splitters = re.compile(r"[~`!@#$%^&*()_+{}|:\"<>?[\]\\\;,.\/]+")
mood_values = [(5, 'dejected'),
(20, 'depressed'),
(40, 'sad'),
(60, 'neutral'),
(85, 'happy'),
(100, 'ecstatic')]
def __init__(self, host, port, nick):
self.host = host
self.port = port
self.nick = nick
self.sock = None
self.incoming_buffer = None
self.mental_health = 80
self.mood = 'happy'
self.physical_health = 100
self.hunger_value = 70
self.hunger = 'peckish'
self.thirst_value = 70
self.thirst = 'not not thirsty'
async def main(self, *args):
await self._connector()
for chan in args:
await self.send('JOIN ', chan)
sock_data = await curio.spawn(self.recv_from_sock())
async def _connector(self):
while True:
try:
self.sock = await curio.open_connection(self.host, self.port)
break
except Exception as e:
print(e)
await self.send('USER ', *(self.nick + '_'*i for i in range(0, 4)))
await self.send('NICK ', self.nick)
async def recv_from_sock(self):
while True:
msg = await self.sock.recv(1024)
msg = msg.decode('utf-8')
print(msg)
if msg.startswith('PING'):
await self.send('PONG ' + msg.split()[1])
try:
msg_contents = msg.split(':', 2)[2]
chan = msg.split(':', 2)[1].split()[-1]
except IndexError:
pass
else:
if msg_contents.lower().startswith(self.nick.lower()):
await self.mood_update()
await curio.spawn(self.message_constructor(msg_contents,
chan,
self.mood))
async def send(self, *data):
data = ' '.join(data) + '\r\n'
await self.sock.send(bytes((data), 'utf-8'))
async def message_constructor(self, msg, chan, mood):
response = []
msg = msg[8:].lower().strip()
msg = msg.replace('\'', ' ')
msg = re.split(self.splitters, msg)
msg = [i.strip() for i in msg]
if msg[-1] == '':
msg = msg[:-1]
try:
if msg[1].startswith(chan.strip('#')):
return None
except IndexError:
pass
keyphrases = [(greetings['inbound'],
greetings['responses']),
(casual_status_inter['inbound'],
casual_status_inter['responses'][self.mood]),
(status_inter['hunger']['inbound'],
status_inter['hunger']['responses'][self.hunger]),
(status_inter['thirst']['inbound'],
status_inter['thirst']['responses'][self.thirst])]
for interaction in keyphrases:
if msg:
try:
msg, resp_phrase = await self._msg_parser(msg,
*interaction)
except TypeError:
pass
else:
response.append(resp_phrase)
else:
break
await self.send('PRIVMSG ',
chan,
' :{}'.format(', '.join(response)),
choice(emoji_moods[mood]))
async def mood_update(self):
for value, mood in self.mood_values:
if self.mental_health < value:
self.mood = mood
break
async def _msg_parser(self, msg, search_loc, response_loc):
for phrase in search_loc:
for index, target in enumerate(msg):
if re.search(r'{}\b'.format(phrase), target):
response = choice(response_loc)
del msg[index]
return msg, response
ploblin = Ploblin('irc.freenode.net', 6667, 'Ploblin')
curio.run(ploblin.main('##theelous3', '#8banana'))
"""
found_phrase, index = next(((phrase, index) for phrase in search_loc
for index, target in enumerate(msg)
if phrase in target), (None, None))
if found_phrase:
response = choice(response_loc)
del msg[index]
return msg, response
"""
emoji_moods = {'ecstatic': ['(⌒▽⌒)♡'],
'happy': ['(◕ ‿ ◕)'],
'neutral': ['(・_・)'],
'sad': ['(ノ_<。)'],
'depressed': ['(╯︵╰,)'],
'dejected': ['(*-_-)']}
greetings = {'inbound': ['hi',
'hey',
'hello',
'ello',
'ello ello',
'ola',
'oi',
'oi oi',
'yo',
'ey',
'greetings',
'howdy',
'good morning',
'morning',
'good afternoon',
'afternoon',
'good evening',
'evening'],
'responses': ['hi',
'hey',
'hello',
'ello',
'ello guv\'nor',
'ola',
'oi',
'oi oi',
'yo',
'ey',
'greetings',
'howdy']
}
casual_status_inter = {'inbound': ['how are you',
'what s up',
'whats up',
'sup',
'how re you',
'are you ok',
'are you alright'
'are you doing ok',
'are you doing alright',
'is everything ok'
'is everything alright',
'what s the story',
'how s things'],
'responses': {'ecstatic': ['I\'m wonderful thanks!',
'I feel fantastic!',
'Feeling amazing!',
'I haven\'t felt this good in life-cycles!'],
'happy': ['I\'m great!',
'I\'m tip top boss, thanks!',
'I feel super'],
'neutral': ['I\'m good',
'I\'m ok',
'I feel alright'],
'sad': ['I\'ve been better',
'Not feeling the best',
'I\'m having a hard time'],
'depressed': ['I\'m feeling quite sad',
'I feel depressed',
'I feel sad and alone'],
'dejected': ['I feel hopeless',
'I feel like killing myself',
'I\'ve hit rock bottom and there\'s no end in sight']
}
}
status_inter = {'hunger': {'inbound': ['you hungry',
'want food',
'would you like food',
'would you like some food',
'do you want something to eat',
'would you like something to eat',
'would you like a bite to eat',
'do you want a bite to eat',
'want to eat'],
'responses': {'not hungry': ['no thanks! I\'m ok for food',
'no thanks! I\'m not hungry',
'naaah! I\'m stuffed',
'nope! I couldn\'nt eat another bite'],
'peckish': ['I\'d eat something small',
'I could eat I suppose',
'I\'ll munch a little something'],
'hungry': ['I could eat a moose',
'I need to eat',
'woah yeah, I\'m hungry'],
'starving': ['I am starving to death',
'if I don\'t eat soon I might die',
'I can feel myself fading away with starvation',
'I\'m so hungry my circuts are consuming themselves, and I am dying']
}
},
'thirst': {'inbound': ['you thirsty',
'want water',
'would you like water',
'would you like some water',
'do you want something to drink',
'would you like something to drink',
'would you like a drink',
'do you want a drink',
'want to drink',
'want some water'],
'responses': {'not thirsty': ['no thanks! I\'m ok for water',
'no thanks! I\'m not thirsty',
'naaah! I\'m nearly drowning here',
'nope! I couldn\'nt swallow another sip'],
'not not thirsty': ['I\'d have a sip',
'I could drink I suppose',
'I\'ll drink a little something'],
'thirsty': ['I could chug an ocean',
'I need to drink something',
'woah yeah, I\'m thirsty'],
'dehydrated': ['I am dying of dehydration',
'if I don\'t drink something soon I might die',
'I my tongue feels like sandpaper',
'I\'m so thirsty, I am dying',
'please, some water friend, I haven\'t got long to live']
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment