minetestbot-modules/rainbow.py

50 lines
1.1 KiB
Python
Raw Permalink Normal View History

2013-11-09 09:22:51 -08:00
#!/usr/bin/env python
"""
rainbow.py - Rainbows
Copyright 2013, sfan5
Licensed under GNU General Public License v2.0
2013-11-09 09:22:51 -08:00
"""
import random
2014-01-04 07:06:51 -08:00
rainbowcolors = ["4", "7", "8", "3", "12", "6", "13"]
def colorize(text, cyclelen=3):
if cyclelen == -1: # Auto-detect
if len(text) < 6:
cyclelen = 1
elif len(text) < 13:
cyclelen = 2
elif len(text) < 25:
cyclelen = 3
else:
cyclelen = 4
2013-11-09 09:22:51 -08:00
out = ""
2014-01-04 07:06:51 -08:00
i = 0
2014-01-04 07:19:19 -08:00
j = 0
2013-11-09 09:22:51 -08:00
for c in text:
2014-07-28 10:07:46 -07:00
if j == 0:
if c in list(str(i) for i in range(10)):
c = u"\u200b" + c # 'ZERO WIDTH SPACE' cuz IRC clients are stupid
out += "\x03" + str(rainbowcolors[i])
out += c
2014-01-04 07:19:19 -08:00
j += 1
if j >= cyclelen:
2014-01-04 07:19:19 -08:00
i += 1
j = 0
2014-01-04 07:06:51 -08:00
if i >= len(rainbowcolors):
i = 0
2013-11-09 09:22:51 -08:00
return out
def rainbow(phenny, input):
arg = input.group(2)
if not arg:
return phenny.say(colorize("Rainbow", cyclelen=1) + "\x03 What?")
if arg.startswith("#") and ' ' in arg and input.admin:
ch = arg.split(" ")[0]
arg = " ".join(arg.split(" ")[1:])
phenny.write(['PRIVMSG', ch], colorize(arg, cyclelen=-1))
else:
phenny.say(colorize(arg, cyclelen=-1))
2013-11-09 09:22:51 -08:00
rainbow.commands = ['rainbow']