From 78d8575e15170c9f0406823d800c3915e89a43f6 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Mon, 12 Apr 2021 10:20:25 +0200 Subject: [PATCH] :bug: Change the way defer works, so it's all done by the bot instance --- Gwendolyn.py | 6 ++++++ cogs/MiscCog.py | 2 +- funcs/games/blackjack.py | 30 ++++++------------------------ funcs/games/connectFour.py | 10 ++-------- funcs/games/hangman.py | 2 +- funcs/games/hex.py | 5 +---- funcs/games/invest.py | 5 +---- funcs/games/money.py | 7 ++----- funcs/games/trivia.py | 5 +---- funcs/other/bedreNetflix.py | 6 +++--- funcs/other/nerdShit.py | 10 +++++----- funcs/other/other.py | 4 ++-- 12 files changed, 31 insertions(+), 61 deletions(-) diff --git a/Gwendolyn.py b/Gwendolyn.py index 0154046..5a48420 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -50,6 +50,12 @@ class Gwendolyn(commands.Bot): self.log(f"{ctx.author.display_name} tried to stop me! (error code 201)",str(ctx.channel_id)) await ctx.send(f"I don't think I will, {ctx.author.display_name} (error code 201)") + async def defer(self, ctx): + try: + await ctx.defer() + except: + self.log("defer failed") + if __name__ == "__main__": diff --git a/cogs/MiscCog.py b/cogs/MiscCog.py index ef50213..136c0fb 100644 --- a/cogs/MiscCog.py +++ b/cogs/MiscCog.py @@ -2,7 +2,7 @@ import discord, codecs, string, json from discord.ext import commands from discord_slash import cog_ext -from utils import getParams +from utils import getParams # pylint: disable=import-error params = getParams() diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index e111f10..b3d4379 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -172,10 +172,7 @@ class Blackjack(): # When players try to hit async def hit(self, ctx, handNumber = 0): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" roundDone = False @@ -239,10 +236,7 @@ class Blackjack(): # When players try to double down async def double(self, ctx, handNumber = 0): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" roundDone = False @@ -317,10 +311,7 @@ class Blackjack(): # When players try to stand async def stand(self, ctx, handNumber = 0): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" roundDone = False @@ -378,10 +369,7 @@ class Blackjack(): # When players try to split async def split(self, ctx, handNumber = 0): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" roundDone = False @@ -506,10 +494,7 @@ class Blackjack(): # Player enters the game and draws a hand async def playerDrawHand(self, ctx, bet : int): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" collection = self.bot.database["blackjack games"] @@ -565,10 +550,7 @@ class Blackjack(): # Starts a game of blackjack async def start(self, ctx): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) blackjackMinCards = 50 blackjackDecks = 4 diff --git a/funcs/games/connectFour.py b/funcs/games/connectFour.py index d6fdf9f..a01ec11 100644 --- a/funcs/games/connectFour.py +++ b/funcs/games/connectFour.py @@ -26,10 +26,7 @@ class ConnectFour(): # Starts the game async def start(self, ctx, opponent): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) user = f"#{ctx.author.id}" channel = str(ctx.channel_id) game = self.bot.database["connect 4 games"].find_one({"_id":channel}) @@ -228,10 +225,7 @@ class ConnectFour(): # Parses command async def surrender(self, ctx): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) channel = str(ctx.channel_id) game = self.bot.database["connect 4 games"].find_one({"_id":channel}) diff --git a/funcs/games/hangman.py b/funcs/games/hangman.py index 0bafdbf..df94339 100644 --- a/funcs/games/hangman.py +++ b/funcs/games/hangman.py @@ -10,7 +10,7 @@ class Hangman(): self.draw = DrawHangman(bot) async def start(self, ctx): - await ctx.defer() + await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" game = self.bot.database["hangman games"].find_one({"_id":channel}) diff --git a/funcs/games/hex.py b/funcs/games/hex.py index 6711670..e6b8b5c 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -93,10 +93,7 @@ class HexGame(): # Starts the game async def start(self, ctx, opponent): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) user = f"#{ctx.author.id}" channel = str(ctx.channel_id) game = self.bot.database["hex games"].find_one({"_id":channel}) diff --git a/funcs/games/invest.py b/funcs/games/invest.py index c34310a..75bb33a 100644 --- a/funcs/games/invest.py +++ b/funcs/games/invest.py @@ -106,10 +106,7 @@ class Invest(): return "no" async def parseInvest(self, ctx, parameters): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) user = f"#{ctx.author.id}" if parameters.startswith("check"): diff --git a/funcs/games/money.py b/funcs/games/money.py index ef9458f..dea2812 100644 --- a/funcs/games/money.py +++ b/funcs/games/money.py @@ -15,7 +15,7 @@ class Money(): else: return 0 async def sendBalance(self, ctx): - await ctx.defer() + await self.bot.defer(ctx) response = self.checkBalance("#"+str(ctx.author.id)) if response == 1: new_message = ctx.author.display_name + " has " + str(response) + " GwendoBuck" @@ -36,10 +36,7 @@ class Money(): # Transfers money from one user to another async def giveMoney(self, ctx, user, amount): - try: - await ctx.defer() - except: - self.bot.log("Defer failed") + await self.bot.defer(ctx) username = user.display_name if self.bot.databaseFuncs.getID(username) == None: async for member in ctx.guild.fetch_members(limit=None): diff --git a/funcs/games/trivia.py b/funcs/games/trivia.py index f8d17c0..eeb4003 100644 --- a/funcs/games/trivia.py +++ b/funcs/games/trivia.py @@ -86,10 +86,7 @@ class Trivia(): return None async def triviaParse(self, ctx, answer): - try: - await ctx.defer() - except: - self.bot.log("defer failed") + await self.bot.defer(ctx) if answer == "": question, options, correctAnswer = self.triviaStart(str(ctx.channel_id)) if options != "": diff --git a/funcs/other/bedreNetflix.py b/funcs/other/bedreNetflix.py index a4aceff..6a7d9ec 100644 --- a/funcs/other/bedreNetflix.py +++ b/funcs/other/bedreNetflix.py @@ -13,7 +13,7 @@ class BedreNetflix(): #Returns a list of no more than 5 options when user requests a movie async def requestMovie(self, ctx, movieName): - await ctx.defer() + await self.bot.defer(ctx) self.bot.log("Searching for "+movieName) movieList = imdb.IMDb().search_movie(movieName) @@ -92,7 +92,7 @@ class BedreNetflix(): #Returns a list of no more than 5 options when user requests a show async def requestShow(self, ctx, showName): - await ctx.defer() + await self.bot.defer(ctx) self.bot.log("Searching for "+showName) movies = imdb.IMDb().search_movie(showName) #Replace with tvdb @@ -307,7 +307,7 @@ class BedreNetflix(): await ctx.send("```"+messageText[:cutOffIndex]+"```") await SendLongMessage(ctx,messageText[cutOffIndex+1:]) - await ctx.defer() + await self.bot.defer(ctx) # showDM, showMovies, showShows, episodes params = [False, False, False, False] diff --git a/funcs/other/nerdShit.py b/funcs/other/nerdShit.py index e2e64f8..53f5306 100644 --- a/funcs/other/nerdShit.py +++ b/funcs/other/nerdShit.py @@ -1,4 +1,4 @@ -import discord, wolframalpha, requests, os +import discord, discord_slash, wolframalpha, requests, os from PIL import Image, ImageDraw, ImageFont class NerdShit(): @@ -7,8 +7,8 @@ class NerdShit(): self.bot = bot async def wolfSearch(self,ctx,content): + await self.bot.defer(ctx) fnt = ImageFont.truetype('resources/fonts/times-new-roman.ttf', 20) - await ctx.defer() self.bot.log("Requesting data") bot = wolframalpha.Client(self.bot.credentials.wolfKey) res = bot.query(content) @@ -36,19 +36,19 @@ class NerdShit(): heights = [] for count, pod in enumerate(chunk): heights += [height] - width = max(width,int(list(pod.img)[0]["@width"])) + width = max(width,int(pod.img['@width'])) if titleChucks[x][count] == "": placeForText = 0 else: placeForText = 30 - height += int(list(pod.img)[0]["@height"]) + 10 + placeForText + height += int(pod.img["@height"]) + 10 + placeForText width += 10 height += 5 wolfImage = Image.new("RGB",(width,height),color=(255,255,255)) for count, pod in enumerate(chunk): - response = requests.get(list(pod.img)[0]["@src"]) + response = requests.get(pod.img["@src"]) file = open("resources/wolfTemp.png", "wb") file.write(response.content) file.close() diff --git a/funcs/other/other.py b/funcs/other/other.py index 5239e6c..2955641 100644 --- a/funcs/other/other.py +++ b/funcs/other/other.py @@ -34,7 +34,7 @@ class Other(): # Picks a random movie and returns information about it async def movieFunc(self, ctx): - await ctx.defer() + await self.bot.defer(ctx) self.bot.log("Creating IMDb object") imdbClient = imdb.IMDb() @@ -138,7 +138,7 @@ class Other(): # Finds a page from the Senkulpa Wikia async def findWikiPage(self, ctx, search : str): - await ctx.defer() + await self.bot.defer(ctx) foundPage = False if search != "":