some cleaning up

This commit is contained in:
Nikolaj
2022-01-28 13:20:15 +01:00
parent 08435d6934
commit 2b21d43627
6 changed files with 320 additions and 325 deletions

View File

@ -15,9 +15,10 @@ class Generators():
yield (corpus[i], corpus[i+1], corpus[i+2])
# Generates a random name
async def nameGen(self, ctx):
async def name_gen(self, ctx):
# Makes a list of all names from "names.txt"
names = open('gwendolyn/resources/names.txt', encoding='utf8').read()
with open("gwendolyn/resources/names.txt", "r", encoding='utf8') as file_pointer:
names = file_pointer.read()
corpus = list(names)
# Makes a list of pairs
@ -28,13 +29,13 @@ class Generators():
# Makes a dictionary of all letters that come after all other letters
for letter_1, letter_2 in pairs:
if letter_1 in letter_dict.keys():
if letter_1 in letter_dict:
letter_dict[letter_1].append(letter_2)
else:
letter_dict[letter_1] = [letter_2]
for letter_1, letter_2, letter_3 in triplets:
if letter_1+letter_2 in letter_dict.keys():
if letter_1+letter_2 in letter_dict:
letter_dict[letter_1+letter_2].append(letter_3)
else:
letter_dict[letter_1+letter_2] = [letter_3]
@ -60,7 +61,7 @@ class Generators():
done = False
# Creates the name one letter at a time
while done == False:
while not done:
if random.randint(1,10) > 1:
try:
new_letter = random.choice(letter_dict[chain[-2]+chain[-1]])
@ -79,15 +80,15 @@ class Generators():
await ctx.send(gen_name)
# Generates a random tavern name
async def tavernGen(self, ctx):
async def tavern_gen(self, ctx):
# _lists first parts, second parts and third parts of tavern names
fp = ["The Silver","The Golden","The Staggering","The Laughing","The Prancing","The Gilded","The Running","The Howling","The Slaughtered","The Leering","The Drunken","The Leaping","The Roaring","The Frowning","The Lonely","The Wandering","The Mysterious","The Barking","The Black","The Gleaming","The Tap-Dancing","The Sad","The Sexy","The Artificial","The Groovy","The Merciful","The Confused","The Pouting","The Horny","The Okay","The Friendly","The Hungry","The Handicapped","The Fire-breathing","The One-Eyed","The Psychotic","The Mad","The Evil","The Idiotic","The Trusty","The Busty"]
sp = ["Eel","Dolphin","Dwarf","Pegasus","Pony","Rose","Stag","Wolf","Lamb","Demon","Goat","Spirit","Horde","Jester","Mountain","Eagle","Satyr","Dog","Spider","Star","Dad","Rat","Jeremy","Mouse","Unicorn","Pearl","Ant","Crab","Penguin","Octopus","Lawyer","Ghost","Toad","Handjob","Immigrant","SJW","Dragon","Bard","Sphinx","Soldier","Salmon","Owlbear","Kite","Frost Giant","Arsonist"]
tp = [" Tavern"," Inn","","","","","","","","",""]
first_part = ["The Silver","The Golden","The Staggering","The Laughing","The Prancing","The Gilded","The Running","The Howling","The Slaughtered","The Leering","The Drunken","The Leaping","The Roaring","The Frowning","The Lonely","The Wandering","The Mysterious","The Barking","The Black","The Gleaming","The Tap-Dancing","The Sad","The Sexy","The Artificial","The Groovy","The Merciful","The Confused","The Pouting","The Horny","The Okay","The Friendly","The Hungry","The Handicapped","The Fire-breathing","The One-Eyed","The Psychotic","The Mad","The Evil","The Idiotic","The Trusty","The Busty"]
second_part = ["Eel","Dolphin","Dwarf","Pegasus","Pony","Rose","Stag","Wolf","Lamb","Demon","Goat","Spirit","Horde","Jester","Mountain","Eagle","Satyr","Dog","Spider","Star","Dad","Rat","Jeremy","Mouse","Unicorn","Pearl","Ant","Crab","Penguin","Octopus","Lawyer","Ghost","Toad","Handjob","Immigrant","SJW","Dragon","Bard","Sphinx","Soldier","Salmon","Owlbear","Kite","Frost Giant","Arsonist"]
third_part = [" Tavern"," Inn","","","","","","","","",""]
# Picks one of each
genTav = random.choice(fp)+" "+random.choice(sp)+random.choice(tp)
self.bot.log("Generated "+genTav)
gen_tav = random.choice(first_part)+" "+random.choice(second_part)+random.choice(third_part)
self.bot.log("Generated "+gen_tav)
# Return the name
await ctx.send(genTav)
await ctx.send(gen_tav)

View File

@ -1,4 +1,8 @@
import discord, discord_slash, wolframalpha, requests, os
import os
import requests
import discord
import wolframalpha
from PIL import Image, ImageDraw, ImageFont
class NerdShit():
@ -6,9 +10,9 @@ class NerdShit():
"""Runs misc commands."""
self.bot = bot
async def wolfSearch(self,ctx,content):
async def wolf_search(self,ctx,content):
await self.bot.defer(ctx)
fnt = ImageFont.truetype('gwendolyn/resources/fonts/times-new-roman.ttf', 20)
font = ImageFont.truetype('gwendolyn/resources/fonts/times-new-roman.ttf', 20)
self.bot.log("Requesting data")
bot = wolframalpha.Client(self.bot.credentials["wolfram_alpha_key"])
res = bot.query(content)
@ -19,33 +23,33 @@ class NerdShit():
if int(res.numpods) > 0:
for pod in res.pods:
titles += [pod.title]
for x, sub in enumerate(pod.subpods):
for i, sub in enumerate(pod.subpods):
pods += [sub]
if x > 0:
if i > 0:
titles += [""]
podChunks = [pods[x:x+2] for x in range(0, len(pods), 2)]
titleChucks = [titles[x:x+2] for x in range(0, len(titles), 2)]
pod_chunks = [pods[x:x+2] for x in range(0, len(pods), 2)]
title_chunks = [titles[x:x+2] for x in range(0, len(titles), 2)]
await ctx.send(f"Response for \"{content}\"")
for x, chunk in enumerate(podChunks):
for i, chunk in enumerate(pod_chunks):
width = 0
for title in titleChucks[x]:
width = max(width,fnt.getsize(title)[0])
for title in title_chunks[i]:
width = max(width,font.getsize(title)[0])
height = 5
heights = []
for count, pod in enumerate(chunk):
heights += [height]
width = max(width,int(pod.img['@width']))
if titleChucks[x][count] == "":
placeFor_text = 0
if title_chunks[i][count] == "":
place_for_text = 0
else:
placeFor_text = 30
height += int(pod.img["@height"]) + 10 + placeFor_text
place_for_text = 30
height += int(pod.img["@height"]) + 10 + place_for_text
width += 10
height += 5
wolfImage = Image.new("RGB",(width,height),color=(255,255,255))
wolf_image = Image.new("RGB",(width,height),color=(255,255,255))
for count, pod in enumerate(chunk):
response = requests.get(pod.img["@src"])
@ -53,29 +57,29 @@ class NerdShit():
file.write(response.content)
file.close()
old_image = Image.open("gwendolyn/resources/wolfTemp.png")
oldSize = old_image.size
if titleChucks[x][count] == "":
placeFor_text = 0
old_size = old_image.size
if title_chunks[i][count] == "":
place_for_text = 0
else:
placeFor_text = 30
newSize = (width,int(oldSize[1]+10+placeFor_text))
new_image = Image.new("RGB",newSize,color=(255,255,255))
new_image.paste(old_image, (int((int(oldSize[0]+10)-oldSize[0])/2),int(((newSize[1]-placeFor_text)-oldSize[1])/2)+placeFor_text))
if titleChucks[x][count] != "":
d = ImageDraw.Draw(new_image,"RGB")
d.text((5,7),titleChucks[x][count],font=fnt,fill=(150,150,150))
place_for_text = 30
new_size = (width,int(old_size[1]+10+place_for_text))
new_image = Image.new("RGB",new_size,color=(255,255,255))
new_image.paste(old_image, (int((int(old_size[0]+10)-old_size[0])/2),int(((new_size[1]-place_for_text)-old_size[1])/2)+place_for_text))
if title_chunks[i][count] != "":
drawer = ImageDraw.Draw(new_image,"RGB")
drawer.text((5,7),title_chunks[i][count],font=font,fill=(150,150,150))
wolfImage.paste(new_image,(0,heights[count]))
wolf_image.paste(new_image,(0,heights[count]))
new_image.close()
old_image.close()
count += 1
wolfImage.save("gwendolyn/resources/wolf.png")
wolfImage.close()
wolf_image.save("gwendolyn/resources/wolf.png")
wolf_image.close()
await ctx.channel.send(file = discord.File("gwendolyn/resources/wolf.png"))
os.remove("gwendolyn/resources/wolf.png")
os.remove("gwendolyn/resources/wolfTemp.png")
else:
self.bot.log("No returned data")
await ctx.send("Could not find anything relating to your search")
await ctx.send("Could not find anything relating to your search")

View File

@ -1,24 +1,24 @@
import imdb # Used in movieFunc
import random # Used in movieFunc
import discord # Used in movieFunc
import datetime # Used in helloFunc
import urllib # Used in imageFunc
import ast
import imdb # Used in movieFunc
import discord # Used in movieFunc
import lxml # Used in imageFunc
import fandom # Used in findWikiPage
import d20 # Used in rollDice
import ast
from .plex import Plex
from .nerd_shit import NerdShit
from .generators import Generators
from gwendolyn.utils import cap
fandom.set_lang("da")
fandom.set_wiki("senkulpa")
class MyStringifier(d20.MarkdownStringifier):
def _str_expression(self, node):
if node.comment == None:
if node.comment is None:
result_text = "Result"
else:
result_text = node.comment.capitalize()
@ -33,23 +33,23 @@ class Other():
self.generators = Generators(self.bot)
# Picks a random movie and returns information about it
async def movieFunc(self, ctx):
async def movie_func(self, ctx):
await self.bot.defer(ctx)
self.bot.log("Creating IMDb object")
imdbClient = imdb.IMDb()
imdb_client = imdb.IMDb()
self.bot.log("Picking a movie")
with open("gwendolyn/resources/movies.txt", "r") as f:
movie_list = f.read().split("\n")
with open("gwendolyn/resources/movies.txt", "r") as file_pointer:
movie_list = file_pointer.read().split("\n")
movie_name = random.choice(movie_list)
self.bot.log(f"Searching for {movie_name}")
searchResult = imdbClient.search_movie(movie_name)
search_result = imdb_client.search_movie(movie_name)
self.bot.log("Getting the data")
movie = searchResult[0]
imdbClient.update(movie)
movie = search_result[0]
imdb_client.update(movie)
self.bot.log("Successfully ran /movie")
@ -63,13 +63,13 @@ class Other():
await ctx.send(embed = embed)
# Responds with a greeting of a time-appropriate maner
async def helloFunc(self, ctx):
def time_in_range(start, end, x):
# Return true if x is in the range [start, end]
async def hello_func(self, ctx):
def time_in_range(start, end, i):
# Return true if i is in the range [start, end]
if start <= end:
return start <= x <= end
return start <= i <= end
else:
return start <= x or x <= end
return start <= i or i <= end
author = ctx.author.display_name
now = datetime.datetime.now()
@ -87,31 +87,22 @@ class Other():
await ctx.send(send_message)
# Finds a random picture online
async def imageFunc(self, ctx):
async def image_func(self, ctx):
# Picks a type of camera, which decides the naming scheme
cams = ("one","two","three","four")
cam = random.choice(cams)
self.bot.log("Chose cam type "+cam)
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)
search = f"img_{''.join([random.randint(0,9) for _ in range(4)])}"
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)
year = str(random.randint(2012,2016))
month = str(random.randint(1,12)).zfill(2)
day = str(random.randint(1,29)).zfill(2)
search = f"IMG_{year}{month}{day}"
elif cam == "three":
a = str(random.randint(1,500)).zfill(4)
search = ("IMAG_"+a)
search = f"IMAG_{str(random.randint(1,500)).zfill(4)}"
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)
search = f"DSC_{''.join([random.randint(0,9) for _ in range(4)])}"
self.bot.log("Searching for "+search)
@ -127,37 +118,37 @@ class Other():
# Picks an image
number = random.randint(1,len(images))-1
image = ast.literal_eval(str(images[number]))
imageUrl = image["murl"]
image_url = image["murl"]
self.bot.log("Picked image number "+str(number))
# Returns the image
self.bot.log("Successfully returned an image")
await ctx.send(imageUrl)
await ctx.send(image_url)
# Finds a page from the Senkulpa Wikia
async def findWikiPage(self, ctx, search : str):
async def find_wiki_page(self, ctx, search : str):
await self.bot.defer(ctx)
foundPage = False
found_page = False
if search != "":
self.bot.log("Trying to find wiki page for "+search)
searchResults = fandom.search(search)
if len(searchResults) > 0:
foundPage = True
searchResult = searchResults[0]
search_results = fandom.search(search)
if len(search_results) > 0:
found_page = True
search_result = search_results[0]
else:
self.bot.log("Couldn't find the page")
await ctx.send("Couldn't find page")
else:
foundPage = True
found_page = True
self.bot.log("Searching for a random page")
searchResult = fandom.random()
search_result = fandom.random()
if foundPage:
self.bot.log(f"Found page \"{searchResult[0]}\"")
page = fandom.page(pageid = searchResult[1])
if found_page:
self.bot.log(f"Found page \"{search_result[0]}\"")
page = fandom.page(pageid = search_result[1])
content = page.summary
images = page.images
@ -173,24 +164,24 @@ class Other():
await ctx.send(embed = embed)
async def rollDice(self, ctx, rollString):
async def roll_dice(self, ctx, roll_string):
user = ctx.author.display_name
while len(rollString) > 1 and rollString[0] == " ":
rollString = rollString[1:]
while len(roll_string) > 1 and roll_string[0] == " ":
roll_string = roll_string[1:]
roll = d20.roll(rollString, allow_comments=True, stringifier=MyStringifier())
roll = d20.roll(roll_string, allow_comments=True, stringifier=MyStringifier())
await ctx.send(f"{user} :game_die:\n{roll}")
async def helpFunc(self, ctx, command):
async def help_func(self, ctx, command):
if command == "":
with open("gwendolyn/resources/help/help.txt",encoding="utf-8") as f:
text = f.read()
em = discord.Embed(title = "Help", description = text,colour = 0x59f442)
await ctx.send(embed = em)
with open("gwendolyn/resources/help/help.txt",encoding="utf-8") as file_pointer:
text = file_pointer.read()
embed = discord.Embed(title = "Help", description = text,colour = 0x59f442)
await ctx.send(embed = embed)
else:
self.bot.log(f"Looking for help-{command}.txt",str(ctx.channel_id))
with open(f"gwendolyn/resources/help/help-{command}.txt",encoding="utf-8") as f:
text = f.read()
em = discord.Embed(title = command.capitalize(), description = text,colour = 0x59f442)
await ctx.send(embed = em)
with open(f"gwendolyn/resources/help/help-{command}.txt",encoding="utf-8") as file_pointer:
text = file_pointer.read()
embed = discord.Embed(title = command.capitalize(), description = text,colour = 0x59f442)
await ctx.send(embed = embed)