Files
Gwendolyn/funcs/gwendolynFuncs.py
2020-03-31 19:39:44 +02:00

111 lines
4.0 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
# I stole this. It rolls dice. I am not qualified to comment it
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)
# Capitalizes all words except some of them
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
# Responds with a greeting of a time-aprpriate maner
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))
# Finds a random picture online
def imageFunc():
# Picks a type of camera, which decides the naming scheme
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)
# Searches for the image and reads the resulting web page
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')
# Picks an image
number = random.randint(1,len(images))-1
image = images[number]
# Returns the image
print("Successfully returned an image\n")
return(image)