TextArt: Use asyncio for message throttling.

This commit is contained in:
oddluck 2020-07-18 02:20:48 +00:00
parent 7e305bd92c
commit f45775a42e

View File

@ -42,6 +42,7 @@ from PIL import Image, ImageOps, ImageFont, ImageDraw, ImageEnhance
import numpy as np import numpy as np
import sys, math import sys, math
import re import re
import asyncio
import pexpect import pexpect
import time import time
import random import random
@ -463,6 +464,10 @@ class TextArt(callbacks.Plugin):
png = wrap(png, [getopts({"bg": "int", "fg": "int"}), "text"]) png = wrap(png, [getopts({"bg": "int", "fg": "int"}), "text"])
async def reply(self, irc, text, channel, delay):
irc.sendMsg(ircmsgs.privmsg(channel, text))
await asyncio.sleep(delay)
def artii(self, irc, msg, args, channel, optlist, text): def artii(self, irc, msg, args, channel, optlist, text):
"""[<channel>] [--font <font>] [--color <color1,color2>] [<text>] """[<channel>] [--font <font>] [--color <color1,color2>] [<text>]
Text to ASCII figlet fonts using the artii API Text to ASCII figlet fonts using the artii API
@ -1025,15 +1030,7 @@ class TextArt(callbacks.Plugin):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
if not self.stopped[msg.args[0]]: if not self.stopped[msg.args[0]]:
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("showStats", msg.args[0]): if self.registryValue("showStats", msg.args[0]):
longest = len(max(output, key=len).encode("utf-8")) longest = len(max(output, key=len).encode("utf-8"))
render_time = "{0:.2f}".format(end_time - start_time) render_time = "{0:.2f}".format(end_time - start_time)
@ -1115,15 +1112,7 @@ class TextArt(callbacks.Plugin):
return return
for line in file.split("\n"): for line in file.split("\n"):
if line.strip() and not self.stopped[msg.args[0]]: if line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
scroll = wrap(scroll, [optional("channel"), getopts({"delay": "float"}), "text"]) scroll = wrap(scroll, [optional("channel"), getopts({"delay": "float"}), "text"])
@ -1210,25 +1199,10 @@ class TextArt(callbacks.Plugin):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
if line.strip() and not self.stopped[msg.args[0]]: if line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif not line.strip() and not self.stopped[msg.args[0]]: elif not line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0",
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
else: else:
return return
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
@ -1336,15 +1310,7 @@ class TextArt(callbacks.Plugin):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
if line.strip() and not self.stopped[msg.args[0]]: if line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
irc.reply(self.doPaste(url, paste), private=False, notice=False, to=channel) irc.reply(self.doPaste(url, paste), private=False, notice=False, to=channel)
else: else:
@ -1436,25 +1402,10 @@ class TextArt(callbacks.Plugin):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
if not line.strip() and not self.stopped[msg.args[0]]: if not line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0",
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif not self.stopped[msg.args[0]]: elif not self.stopped[msg.args[0]]:
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
irc.reply( irc.reply(
self.doPaste(text, paste), private=False, notice=False, to=channel self.doPaste(text, paste), private=False, notice=False, to=channel
@ -1536,25 +1487,10 @@ class TextArt(callbacks.Plugin):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
if not line.strip() and not self.stopped[msg.args[0]]: if not line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0",
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif not self.stopped[msg.args[0]]: elif not self.stopped[msg.args[0]]:
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
irc.reply( irc.reply(
self.doPaste(text, paste), private=False, notice=False, to=channel self.doPaste(text, paste), private=False, notice=False, to=channel
@ -1658,15 +1594,7 @@ class TextArt(callbacks.Plugin):
and not self.stopped[msg.args[0]] and not self.stopped[msg.args[0]]
and not line.startswith("Follow") and not line.startswith("Follow")
): ):
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
irc.reply( irc.reply(
self.doPaste(location, paste), private=False, notice=False, to=channel self.doPaste(location, paste), private=False, notice=False, to=channel
@ -1721,15 +1649,8 @@ class TextArt(callbacks.Plugin):
if not line.strip() and not self.stopped[msg.args[0]]: if not line.strip() and not self.stopped[msg.args[0]]:
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0",
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif ( elif (
line.strip() line.strip()
and not self.stopped[msg.args[0]] and not self.stopped[msg.args[0]]
@ -1737,15 +1658,7 @@ class TextArt(callbacks.Plugin):
): ):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
irc.reply( irc.reply(
self.doPaste(coin, paste), private=False, notice=False, to=channel self.doPaste(coin, paste), private=False, notice=False, to=channel
@ -1807,29 +1720,14 @@ class TextArt(callbacks.Plugin):
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
paste += line + "\n" paste += line + "\n"
if not line.strip() and not self.stopped[msg.args[0]]: if not line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0",
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif ( elif (
line.strip() line.strip()
and not self.stopped[msg.args[0]] and not self.stopped[msg.args[0]]
and "Follow @igor_chubin" not in line and "Follow @igor_chubin" not in line
): ):
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
if self.registryValue("pasteEnable", msg.args[0]): if self.registryValue("pasteEnable", msg.args[0]):
irc.reply( irc.reply(
self.doPaste(text, paste), private=False, notice=False, to=channel self.doPaste(text, paste), private=False, notice=False, to=channel
@ -1859,29 +1757,14 @@ class TextArt(callbacks.Plugin):
fortune = random.randrange(0, len(fortunes)) fortune = random.randrange(0, len(fortunes))
for line in fortunes[fortune].splitlines(): for line in fortunes[fortune].splitlines():
if not line.strip() and not self.stopped[msg.args[0]]: if not line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0",
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif ( elif (
line.strip() line.strip()
and not self.stopped[msg.args[0]] and not self.stopped[msg.args[0]]
and "Follow @igor_chubin" not in line and "Follow @igor_chubin" not in line
): ):
time.sleep(delay) asyncio.run(self.reply(irc, line, channel, delay))
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
fortune = wrap(fortune, [optional("channel"), getopts({"delay": "float"})]) fortune = wrap(fortune, [optional("channel"), getopts({"delay": "float"})])
@ -1917,29 +1800,10 @@ class TextArt(callbacks.Plugin):
output = data.content.decode() output = data.content.decode()
for line in output.splitlines(): for line in output.splitlines():
if not line.strip() and not self.stopped[msg.args[0]]: if not line.strip() and not self.stopped[msg.args[0]]:
time.sleep(delay) line = "\xa0"
irc.reply( asyncio.run(self.reply(irc, line, channel, delay))
"\xa0", elif line.strip() and not self.stopped[msg.args[0]]:
prefixNick=False, asyncio.run(self.reply(irc, line, channel, delay))
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
elif (
line.strip()
and not self.stopped[msg.args[0]]
and "Follow @igor_chubin" not in line
):
time.sleep(delay)
irc.reply(
line,
prefixNick=False,
noLengthCheck=True,
private=False,
notice=False,
to=channel,
)
irc.reply(url.get("href")) irc.reply(url.get("href"))
mircart = wrap(mircart, [optional("channel"), getopts({"delay": "float"}), "text"]) mircart = wrap(mircart, [optional("channel"), getopts({"delay": "float"}), "text"])