Commit 64bfd2af authored by Sébastien Dufromentel's avatar Sébastien Dufromentel
Browse files

Nickname improvements

parent d3f5fd11
......@@ -21,6 +21,7 @@
##
######
import re
import ssl
import json
import signal
......@@ -53,18 +54,32 @@ def usernick(botnick, lang):
if nick.endswith("|") else nick[:-1])
def translate(message, target):
try: # Don't interrupt.
if message == "\o/":
return message
for nick in bots.keys():
if nick in message:
message = message.replace(nick, usernick(nick, target))
if message == "\o/":
return message
num, words, repls = 1, [], {}
for word in re.split("(\W+)", message):
if irc.strings.IRCFoldedCase(word) in bots.keys():
repls["*"+str(num)] = usernick(word, target)
words.append("*"+str(num))
num += 1
else: # The opposite?
nick = botnick(word, target)
if nick in bots.keys():
repls["*"+str(num)] = nick
words.append("*"+str(num))
num += 1
else: # Normal word.
words.append(word)
message = "".join(words) # Should be good now.
try: # The server may have troubles to translate.
with urllib.request.urlopen(trans, urllib.parse.urlencode({ "source":
"auto", "target": target, "q": message }).encode()) as f:
return json.loads(f.read())["translatedText"]
except Exception: # Probably a server error.
__import__("traceback").print_exc()
return message
message = json.loads(f.read())["translatedText"]
except Exception: # Probably some 500.
pass # Does it help to report it?
for key, repl in repls.items():
message = message.replace(key, repl)
return message # Should be translated?
class data(object):
def __init__(self, **args):
......@@ -98,7 +113,8 @@ class Babel(Bot):
Bot(nick, ev.source.nick) #FIXME read name?
def on_nick(self, conn, ev):
pass #TODO quit + join?
self.on_quit(conn, ev)
self.on_join(conn, data(source=data(nick=ev.target)))
def on_quit(self, conn, ev):
nick = botnick(ev.source.nick, self.botslang)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment