From ffda1f7069065fd4ce2b2fe781fbcb5793406f99 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 31 Aug 2021 15:01:10 +0200 Subject: Migrate to Discord.py 2.0a0 Since the Discord.py repository has been archived, we can switch to the latest commit of 2.0a0, knowing no breaking change will occur (still pinned to the commit just in case). This commit also solves two small problems due to that fix, the avatar interface changing and Embed.name disappearing. Quite a painless migration. --- bot/bot.py | 2 +- bot/exts/easter/egg_decorating.py | 2 +- .../evergreen/avatar_modification/avatar_modify.py | 26 +++++++++++----------- bot/exts/evergreen/bookmark.py | 2 +- bot/exts/evergreen/recommend_game.py | 2 +- bot/exts/evergreen/snakes/_converter.py | 2 +- bot/exts/evergreen/snakes/_snakes_cog.py | 14 ++++-------- bot/exts/evergreen/snakes/_utils.py | 2 +- bot/exts/utils/extensions.py | 2 +- 9 files changed, 24 insertions(+), 30 deletions(-) (limited to 'bot') diff --git a/bot/bot.py b/bot/bot.py index b8de97aa..8e82d0c4 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -120,7 +120,7 @@ class Bot(commands.Bot): return if not icon: - icon = self.user.avatar_url_as(format="png") + icon = self.user.display_avatar.url embed = Embed(description=details) embed.set_author(name=title, icon_url=icon) diff --git a/bot/exts/easter/egg_decorating.py b/bot/exts/easter/egg_decorating.py index fd7620d4..6201d424 100644 --- a/bot/exts/easter/egg_decorating.py +++ b/bot/exts/easter/egg_decorating.py @@ -108,7 +108,7 @@ class EggDecorating(commands.Cog): description="Here is your pretty little egg. Hope you like it!" ) embed.set_image(url="attachment://egg.png") - embed.set_footer(text=f"Made by {ctx.author.display_name}", icon_url=ctx.author.avatar_url) + embed.set_footer(text=f"Made by {ctx.author.display_name}", icon_url=ctx.author.display_avatar.url) await ctx.send(file=file, embed=embed) return new_im diff --git a/bot/exts/evergreen/avatar_modification/avatar_modify.py b/bot/exts/evergreen/avatar_modification/avatar_modify.py index 7b4ae9c7..fd586613 100644 --- a/bot/exts/evergreen/avatar_modification/avatar_modify.py +++ b/bot/exts/evergreen/avatar_modification/avatar_modify.py @@ -100,7 +100,7 @@ class AvatarModify(commands.Cog): await ctx.send(f"{Emojis.cross_mark} Could not get user info.") return - image_bytes = await user.avatar_url_as(size=1024).read() + image_bytes = await user.display_avatar.replace(size=1024).read() file_name = file_safe_name("eightbit_avatar", ctx.author.display_name) file = await in_executor( @@ -116,7 +116,7 @@ class AvatarModify(commands.Cog): ) embed.set_image(url=f"attachment://{file_name}") - embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=user.avatar_url) + embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=user.display_avatar.url) await ctx.send(embed=embed, file=file) @@ -137,7 +137,7 @@ class AvatarModify(commands.Cog): await ctx.send(f"{Emojis.cross_mark} Could not get user info.") return - image_bytes = await user.avatar_url_as(size=1024).read() + image_bytes = await user.display_avatar.replace(size=1024).read() filename = file_safe_name("reverse_avatar", ctx.author.display_name) file = await in_executor( @@ -153,7 +153,7 @@ class AvatarModify(commands.Cog): ) embed.set_image(url=f"attachment://{filename}") - embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=user.avatar_url) + embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=user.display_avatar.url) await ctx.send(embed=embed, file=file) @@ -193,7 +193,7 @@ class AvatarModify(commands.Cog): return ctx.send = send_message # Reassigns ctx.send - image_bytes = await user.avatar_url_as(size=256).read() + image_bytes = await user.display_avatar.replace(size=256).read() file_name = file_safe_name("easterified_avatar", ctx.author.display_name) file = await in_executor( @@ -205,11 +205,11 @@ class AvatarModify(commands.Cog): ) embed = discord.Embed( - name="Your Lovely Easterified Avatar!", + title="Your Lovely Easterified Avatar!", description="Here is your lovely avatar, all bright and colourful\nwith Easter pastel colours. Enjoy :D" ) embed.set_image(url=f"attachment://{file_name}") - embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=user.avatar_url) + embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=user.display_avatar.url) await ctx.send(file=file, embed=embed) @@ -235,7 +235,7 @@ class AvatarModify(commands.Cog): ) embed = discord.Embed( - name="Your Lovely Pride Avatar!", + title="Your Lovely Pride Avatar!", description=f"Here is your lovely avatar, surrounded by\n a beautiful {option} flag. Enjoy :D" ) embed.set_image(url=f"attachment://{file_name}") @@ -268,7 +268,7 @@ class AvatarModify(commands.Cog): if not user: await ctx.send(f"{Emojis.cross_mark} Could not get user info.") return - image_bytes = await user.avatar_url_as(size=1024).read() + image_bytes = await user.display_avatar.replace(size=1024).read() await self.send_pride_image(ctx, image_bytes, pixels, flag, option) @prideavatar.command() @@ -296,7 +296,7 @@ class AvatarModify(commands.Cog): return async with ctx.typing(): - image_bytes = await user.avatar_url_as(size=1024).read() + image_bytes = await user.display_avatar.replace(size=1024).read() file_name = file_safe_name("spooky_avatar", ctx.author.display_name) @@ -312,7 +312,7 @@ class AvatarModify(commands.Cog): colour=Colours.soft_red ) embed.set_image(url=f"attachment://{file_name}") - embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=ctx.author.avatar_url) + embed.set_footer(text=f"Made by {ctx.author.display_name}.", icon_url=ctx.author.display_avatar.url) await ctx.send(file=file, embed=embed) @@ -335,7 +335,7 @@ class AvatarModify(commands.Cog): file_name = file_safe_name("mosaic_avatar", ctx.author.display_name) - img_bytes = await user.avatar_url_as(size=1024).read() + img_bytes = await user.display_avatar.replace(size=1024).read() file = await in_executor( PfpEffects.apply_effect, @@ -362,7 +362,7 @@ class AvatarModify(commands.Cog): ) embed.set_image(url=f"attachment://{file_name}") - embed.set_footer(text=f"Made by {ctx.author.display_name}", icon_url=user.avatar_url) + embed.set_footer(text=f"Made by {ctx.author.display_name}", icon_url=user.display_avatar.url) await ctx.send(file=file, embed=embed) diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index f93371a6..98b0f2bf 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -37,7 +37,7 @@ class Bookmark(commands.Cog): name="Wanna give it a visit?", value=f"[Visit original message]({target_message.jump_url})" ) - embed.set_author(name=target_message.author, icon_url=target_message.author.avatar_url) + embed.set_author(name=target_message.author, icon_url=target_message.author.display_avatar.url) embed.set_thumbnail(url=Icons.bookmark) return embed diff --git a/bot/exts/evergreen/recommend_game.py b/bot/exts/evergreen/recommend_game.py index 35d60128..66597b59 100644 --- a/bot/exts/evergreen/recommend_game.py +++ b/bot/exts/evergreen/recommend_game.py @@ -39,7 +39,7 @@ class RecommendGame(commands.Cog): # Creating and formatting Embed embed = discord.Embed(color=discord.Colour.blue()) if author is not None: - embed.set_author(name=author.name, icon_url=author.avatar_url) + embed.set_author(name=author.name, icon_url=author.display_avatar.url) embed.set_image(url=game["image"]) embed.add_field(name=f"Recommendation: {game['title']}\n{game['link']}", value=game["description"]) diff --git a/bot/exts/evergreen/snakes/_converter.py b/bot/exts/evergreen/snakes/_converter.py index c8d1909b..75212107 100644 --- a/bot/exts/evergreen/snakes/_converter.py +++ b/bot/exts/evergreen/snakes/_converter.py @@ -53,7 +53,7 @@ class Snake(Converter): embed = discord.Embed( title="Found multiple choices. Please choose the correct one.", colour=0x59982F) - embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url) + embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.display_avatar.url) name = await disambiguate(ctx, get_potential(all_names), timeout=timeout, embed=embed) return names.get(name, name) diff --git a/bot/exts/evergreen/snakes/_snakes_cog.py b/bot/exts/evergreen/snakes/_snakes_cog.py index 07d3c363..225df948 100644 --- a/bot/exts/evergreen/snakes/_snakes_cog.py +++ b/bot/exts/evergreen/snakes/_snakes_cog.py @@ -486,7 +486,7 @@ class Snakes(Cog): win = False antidote_embed = Embed(color=SNAKE_COLOR, title="Antidote") - antidote_embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) + antidote_embed.set_author(name=ctx.author.name, icon_url=ctx.author.display_avatar.url) # Generate answer antidote_answer = list(ANTIDOTE_EMOJI) # Duplicate list, not reference it @@ -569,7 +569,7 @@ class Snakes(Cog): # Winning / Ending Screen if win is True: antidote_embed = Embed(color=SNAKE_COLOR, title="Antidote") - antidote_embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) + antidote_embed.set_author(name=ctx.author.name, icon_url=ctx.author.display_avatar.url) antidote_embed.set_image(url="https://i.makeagif.com/media/7-12-2015/Cj1pts.gif") antidote_embed.add_field(name="You have created the snake antidote!", value=f"The solution was: {' '.join(antidote_answer)}\n" @@ -577,7 +577,7 @@ class Snakes(Cog): await board_id.edit(embed=antidote_embed) else: antidote_embed = Embed(color=SNAKE_COLOR, title="Antidote") - antidote_embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) + antidote_embed.set_author(name=ctx.author.name, icon_url=ctx.author.display_avatar.url) antidote_embed.set_image(url="https://media.giphy.com/media/ceeN6U57leAhi/giphy.gif") antidote_embed.add_field( name=EMPTY_UNICODE, @@ -1063,16 +1063,10 @@ class Snakes(Cog): message = self._get_random_long_message(messages) - # Set the avatar - if user.avatar is not None: - avatar = f"https://cdn.discordapp.com/avatars/{user.id}/{user.avatar}" - else: - avatar = ctx.author.default_avatar_url - # Build and send the embed embed.set_author( name=f"{user.name}#{user.discriminator}", - icon_url=avatar, + icon_url=user.display_avatar.url, ) embed.description = f"*{self._snakify(message)}*" diff --git a/bot/exts/evergreen/snakes/_utils.py b/bot/exts/evergreen/snakes/_utils.py index f996d7f8..55acaf1f 100644 --- a/bot/exts/evergreen/snakes/_utils.py +++ b/bot/exts/evergreen/snakes/_utils.py @@ -461,7 +461,7 @@ class SnakeAndLaddersGame: self.players.append(user) self.player_tiles[user.id] = 1 - avatar_bytes = await user.avatar_url_as(format="jpeg", size=PLAYER_ICON_IMAGE_SIZE).read() + avatar_bytes = await user.display_avatar.read() im = Image.open(io.BytesIO(avatar_bytes)).resize((BOARD_PLAYER_SIZE, BOARD_PLAYER_SIZE)) self.avatar_images[user.id] = im diff --git a/bot/exts/utils/extensions.py b/bot/exts/utils/extensions.py index 64e404d2..a95def4e 100644 --- a/bot/exts/utils/extensions.py +++ b/bot/exts/utils/extensions.py @@ -155,7 +155,7 @@ class Extensions(commands.Cog): embed.set_author( name="Extensions List", url=Client.github_bot_repo, - icon_url=str(self.bot.user.avatar_url) + icon_url=str(self.bot.user.display_avatar.url) ) lines = [] -- cgit v1.2.3 From 9c02adc04a7949b1ed8a979420e513531d46e384 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 31 Aug 2021 15:10:01 +0200 Subject: Trigger the bot on mentions Since it is the only way to add bots to threads, it would make sense to be able to execute that command. --- bot/bot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/bot.py b/bot/bot.py index 8e82d0c4..b877233e 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -8,6 +8,7 @@ from aiohttp import AsyncResolver, ClientSession, TCPConnector from async_rediscache import RedisSession from discord import DiscordException, Embed from discord.ext import commands +from discord.ext.commands import when_mentioned_or from bot import constants @@ -203,7 +204,7 @@ loop.run_until_complete(redis_session.connect()) bot = Bot( redis_session=redis_session, - command_prefix=constants.Client.prefix, + command_prefix=when_mentioned_or(constants.Client.prefix), activity=discord.Game(name=f"Commands: {constants.Client.prefix}help"), allowed_mentions=discord.AllowedMentions(everyone=False, roles=_allowed_roles), intents=_intents, -- cgit v1.2.3 From 3f90389d76b4666868cbb24e7958d5cf236207c5 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 31 Aug 2021 15:23:26 +0200 Subject: Make the bot automatically join threads This is just a QoL of life thing, avoiding having us to check if the bot is here or not before issuing a command Co-authored-by: ChrisJL --- bot/bot.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/bot.py b/bot/bot.py index b877233e..c7b87a65 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,14 +1,15 @@ import asyncio import logging import socket +from contextlib import suppress from typing import Optional import discord from aiohttp import AsyncResolver, ClientSession, TCPConnector from async_rediscache import RedisSession -from discord import DiscordException, Embed +from discord import DiscordException, Embed, Forbidden, Thread from discord.ext import commands -from discord.ext.commands import when_mentioned_or +from discord.ext.commands import Cog, when_mentioned_or from bot import constants @@ -46,6 +47,21 @@ class Bot(commands.Bot): return None return guild.me + @Cog.listener() + async def on_thread_join(self, thread: Thread) -> None: + """ + Try to join newly created threads. + + Despite the event name being misleading, this is dispatched when new threads are created. + We want our bots to automatically join threads in order to answer commands using their prefixes. + """ + if thread.me: + # Already in this thread, return early + return + + with suppress(Forbidden): + await thread.join() + async def close(self) -> None: """Close Redis session when bot is shutting down.""" await super().close() -- cgit v1.2.3 From e004e3a048b80ed94410fc7a2703c8d8735b1a54 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 31 Aug 2021 15:37:59 +0200 Subject: Snakes: make use of PLAYER_ICON_IMAGE_SIZE again --- bot/exts/evergreen/snakes/_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/snakes/_utils.py b/bot/exts/evergreen/snakes/_utils.py index 55acaf1f..2c825c7c 100644 --- a/bot/exts/evergreen/snakes/_utils.py +++ b/bot/exts/evergreen/snakes/_utils.py @@ -461,7 +461,7 @@ class SnakeAndLaddersGame: self.players.append(user) self.player_tiles[user.id] = 1 - avatar_bytes = await user.display_avatar.read() + avatar_bytes = await user.display_avatar.replace(size=PLAYER_ICON_IMAGE_SIZE).read() im = Image.open(io.BytesIO(avatar_bytes)).resize((BOARD_PLAYER_SIZE, BOARD_PLAYER_SIZE)) self.avatar_images[user.id] = im -- cgit v1.2.3