m4pl1mp/plugins/maple_plugin.py
2021-12-01 03:23:20 -06:00

185 lines
9.2 KiB
Python

# -*- coding: utf-8 -*-
###############################################################################################
###############################################################################################
import irc3
from irc3.plugins.cron import cron
###############################################################################################
###############################################################################################
import re
import os
from difflib import SequenceMatcher
import random
###############################################################################################
###############################################################################################
TELL_RE = re.compile('maple.tell\s(.+?)\s(.+)')
dir_path = os.path.dirname(os.path.realpath(__file__))
###############################################################################################
###############################################################################################
class MESSAGE_HISTORY():
###########################################################################################
###########################################################################################
maple_messages = []
user_messages = []
user_users = []
###########################################################################################
###########################################################################################
def __init__(self):
self.processing=0
return
###########################################################################################
###########################################################################################
def push_maple_messages(self,data):
self.maple_messages = self.maple_messages[-1:] + self.maple_messages[:-1]
self.maple_messages[0] = data
###########################################################################################
###########################################################################################
def push_user_messages(self,user,data):
self.user_users.append(user)
self.user_messages.append(data)
###########################################################################################
###########################################################################################
def similar(self,a,b):
return SequenceMatcher(None,a,b).ratio()
###########################################################################################
###########################################################################################
def log(self, s):
print(f'maple > {s}')
###########################################################################################
###########################################################################################
def out(self, s):
_path = os.path.dirname(os.path.realpath(__file__))
_file = _path + "/maple_i_process.txt"
MSG=s
f = open(_file,'w')
f.write(f'{MSG}')
f.close()
###############################################################################################
###############################################################################################
@irc3.plugin
class Plugin:
###########################################################################################
###########################################################################################
def __init__(self, bot):
self.bot=bot
self.bot.history=MESSAGE_HISTORY()
self.bot.present=0
for _ in range(5):
self.bot.history.maple_messages.append(_)
###########################################################################################
###########################################################################################
def log(self, s):
print(f'maple > {s}')
###########################################################################################
###########################################################################################
def out(self, s):
_path = os.path.dirname(os.path.realpath(__file__))
_file = _path + "/maple_i_process.txt"
MSG=s
f = open(_file,'w')
f.write(f'{MSG}')
f.close()
###########################################################################################
###########################################################################################
@irc3.event(irc3.rfc.PRIVMSG)
async def on_privmsg_search_for_maple(self, mask=None, target=None, data=None, **kw):
##############################################
if mask.nick == 'maple':
return
if not target.lower() == "#tcpdirect".lower():
return
##############################################
DICE=0
##############################################
if self.bot.present == 0:
if not data.lower().find('maple') > -1:
if not random.randint(0,100) == 13:
DICE=0
else:
DICE=1
if not data.lower().find('joke') == -1:
self.bot.joke(mask,target,'')
DICE=1
if mask.nick == 'd':
if not data.lower().find('hang with us') == -1:
self.bot.present=1
msg="i don't mind hanging out for a bit"
self.bot.privmsg(target,self.bot.emo(msg))
DICE=1
if DICE == 0: return
log = self.log
log(data)
data = data.lower().replace('maple','')
self.bot.history.push_user_messages(mask.nick.lower(),data)
##############################################
elif self.bot.present == 1:
if mask.nick == 'd':
if not data.lower().find('leave us alone') == -1:
self.bot.present=0
msg="i gotta go, i'll ttyl"
self.bot.privmsg(target,self.bot.emo(msg))
log = self.log
log(data)
data = data.lower().replace('maple','')
self.bot.history.push_user_messages(mask.nick.lower(),data)
###############################################################################################
###############################################################################################
@cron('* * * * * */1')
def _maple(bot):
###########################################################################################
###########################################################################################
_path = os.path.dirname(os.path.realpath(__file__))
_file = _path + "/maple_o_processed.txt"
###########################################################################################
###########################################################################################
if bot.history.processing==0:
try:
user=bot.history.user_users[0]
message=bot.history.user_messages[0]
bot.history.processing=1
bot.history.out(message)
except:
pass
else:
f = open(_file,'r')
s = f.read()
f.close()
if len(s) > 0:
for _ in bot.history.maple_messages:
if _ == s:
f = open(_file,'w')
s = f.write('')
f.close()
bot.history.log('*** REJECTED // DUPE ***')
bot.history.out(bot.history.user_messages[0])
return
if bot.history.similar(s,str(bot.history.maple_messages[1])) > 0.9:
f = open(_file,'w')
s = f.write('')
f.close()
bot.history.log('*** REJECTED // SIMILAR ***')
bot.history.out(bot.history.user_messages[0])
return
if bot.history.similar(s,str(bot.history.user_messages[0])) > 0.9:
f = open(_file,'w')
s = f.write('')
f.close()
bot.history.log('*** REJECTED // MOCK ***')
bot.history.out(bot.history.user_messages[0])
return
s = s.strip()
bot.history.push_maple_messages(s)
###################################################################################
TARGET1="#tcpdirect"
bot.privmsg(TARGET1,f'{bot.history.user_users[0]}' + ': ' + f'{s.strip()}')
bot.history.user_users.remove(bot.history.user_users[0])
bot.history.user_messages.remove(bot.history.user_messages[0])
bot.history.processing=0
###################################################################################
f = open(_file,'w')
s = f.write('')
f.close()
###################################################################################
###################################################################################
###############################################################################################
###############################################################################################