103 lines
3.8 KiB
Python
103 lines
3.8 KiB
Python
import lxml.etree #used by imageFunc
|
|
import re #used by roll_dice
|
|
import datetime #used by helloFunc
|
|
import json #used by spellFunc
|
|
import random #used by imageFunc
|
|
import urllib #used by imageFunc
|
|
import imdb #used by movieFunc
|
|
|
|
from .roll import dice
|
|
from .lookup import lookupFuncs
|
|
from .other import movie, generators
|
|
from .swfuncs import swchar, swroll
|
|
|
|
def roll_dice(author : str, rollStr : str = "1d20"):
|
|
print("Rolling "+str(rollStr))
|
|
if rollStr == '0/0': # easter eggs
|
|
return("What do you expect me to do, destroy the universe?")
|
|
|
|
adv = 0
|
|
if re.search('(^|\s+)(adv|dis)(\s+|$)', rollStr) is not None:
|
|
adv = 1 if re.search('(^|\s+)adv(\s+|$)', rollStr) is not None else -1
|
|
rollStr = re.sub('(adv|dis)(\s+|$)', '', rollStr)
|
|
res = dice.roll(rollStr, adv=adv)
|
|
out = res.result
|
|
outStr = author + ' :game_die:\n' + out
|
|
if len(outStr) > 1999:
|
|
outputs = author + ' :game_die:\n[Output truncated due to length]\n**Result:** ' + str(res.plain)
|
|
else:
|
|
outputs = outStr
|
|
print("Successfully ran !roll")
|
|
print("")
|
|
return(outputs)
|
|
|
|
no_caps_list = ["of","the"]
|
|
|
|
def cap(s):
|
|
word_number = 0
|
|
lst = s.split()
|
|
res = ''
|
|
for word in lst:
|
|
word_number += 1
|
|
if word not in no_caps_list or word_number == 1:
|
|
word = word.capitalize()
|
|
res += word+" "
|
|
res = res[:-1]
|
|
return res
|
|
|
|
def time_in_range(start, end, x):
|
|
"""Return true if x is in the range [start, end]"""
|
|
if start <= end:
|
|
return start <= x <= end
|
|
else:
|
|
return start <= x or x <= end
|
|
|
|
def helloFunc(author):
|
|
print("")
|
|
now = datetime.datetime.now()
|
|
if time_in_range(now.replace(hour=5, minute=0, second=0, microsecond=0),now.replace(hour=10, minute=0, second=0, microsecond=0), now):
|
|
return("Good morning, "+str(author))
|
|
elif time_in_range(now.replace(hour=10, minute=0, second=0, microsecond=0),now.replace(hour=13, minute=0, second=0, microsecond=0), now):
|
|
return("Good day, "+str(author))
|
|
elif time_in_range(now.replace(hour=13, minute=0, second=0, microsecond=0),now.replace(hour=18, minute=0, second=0, microsecond=0), now):
|
|
return("Good afternoon, "+str(author))
|
|
elif time_in_range(now.replace(hour=18, minute=0, second=0, microsecond=0),now.replace(hour=22, minute=0, second=0, microsecond=0), now):
|
|
return("Good evening, "+str(author))
|
|
elif time_in_range(now.replace(hour=22, minute=0, second=0, microsecond=0),now.replace(hour=23, minute=59, second=59, microsecond=0), now):
|
|
return("Good night, "+str(author))
|
|
else:
|
|
return("Why hello, "+str(author))
|
|
|
|
def imageFunc():
|
|
cams = ("one","two","three","four")
|
|
cam = random.choice(cams)
|
|
if cam == "one":
|
|
a = str(random.randint(0 ,9))
|
|
b = str(random.randint(0,9))
|
|
c = str(random.randint(0,9))
|
|
d = str(random.randint(0,9))
|
|
search = ("img_"+a+b+c+d)
|
|
elif cam == "two":
|
|
a = str(random.randint(2012,2016))
|
|
b = str(random.randint(1,12)).zfill(2)
|
|
c = str(random.randint(1,29)).zfill(2)
|
|
search = ("IMG_"+a+b+c)
|
|
elif cam == "three":
|
|
a = str(random.randint(1,500)).zfill(4)
|
|
search = ("IMAG_"+a)
|
|
elif cam == "four":
|
|
a = str(random.randint(0,9))
|
|
b = str(random.randint(0,9))
|
|
c = str(random.randint(0,9))
|
|
d = str(random.randint(0,9))
|
|
search = ("DSC_"+a+b+c+d)
|
|
page = urllib.request.urlopen("https://www.bing.com/images/search?q="+search+"&safesearch=off")
|
|
read = page.read()
|
|
tree = lxml.etree.HTML(read)
|
|
images = tree.xpath('//a[@class = "thumb"]/@href')
|
|
number = random.randint(1,len(images))-1
|
|
image = images[number]
|
|
print("Successfully returned an image")
|
|
print("")
|
|
return(image)
|