From c7ea5e09987575601b83a6409ee2d3f78d662613 Mon Sep 17 00:00:00 2001 From: classabbyamp Date: Fri, 30 Dec 2022 15:01:48 -0500 Subject: [PATCH] migrate to pycord --- common.py | 7 ++++--- dev-requirements.txt | 2 +- exts/base.py | 8 +++++--- main.py | 17 +++++++++-------- requirements.txt | 3 ++- run.sh | 4 ++-- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/common.py b/common.py index 36b9d09..e78825d 100644 --- a/common.py +++ b/common.py @@ -21,7 +21,7 @@ import aiohttp import discord import discord.ext.commands as commands -from discord import Emoji, Reaction, PartialEmoji +from discord import Emoji, PartialEmoji import data.options as opt @@ -161,7 +161,8 @@ class GlobalChannelConverter(commands.IDConverter): def embed_factory(ctx: commands.Context) -> discord.Embed: """Creates an embed with neutral colour and standard footer.""" embed = discord.Embed(timestamp=datetime.utcnow(), colour=colours.neutral) - embed.set_footer(text=str(ctx.author), icon_url=str(ctx.author.avatar_url)) + if ctx.author: + embed.set_footer(text=str(ctx.author), icon_url=str(ctx.author.avatar.url)) return embed @@ -178,7 +179,7 @@ def error_embed_factory(ctx: commands.Context, exception: Exception, debug_mode: return embed -async def add_react(msg: discord.Message, react: Union[Emoji, Reaction, PartialEmoji, str]): +async def add_react(msg: discord.Message, react: Union[Emoji, PartialEmoji, str]): try: await msg.add_reaction(react) except discord.Forbidden: diff --git a/dev-requirements.txt b/dev-requirements.txt index 8c49200..9937b8f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1,3 @@ -r requirements.txt flake8 -discord.py-stubs==1.7.3 +mypy diff --git a/exts/base.py b/exts/base.py index 29a282d..8f4456c 100644 --- a/exts/base.py +++ b/exts/base.py @@ -141,7 +141,8 @@ class QrmHelpCommand(commands.HelpCommand): embed.title = await self.get_command_signature(group) embed.description = group.help for cmd in await self.filter_commands(group.commands, sort=True): - embed.add_field(name=await self.get_command_signature(cmd), value=cmd.help, inline=False) + embed.add_field(name=await self.get_command_signature(cmd), value=cmd.help if cmd.help else "", + inline=False) await self.context.send(embed=embed) @@ -177,7 +178,7 @@ class BaseCog(commands.Cog): @commands.Cog.listener() async def on_ready(self): - if not self.bot_invite: + if not self.bot_invite and self.bot.user: self.bot_invite = (f"https://discordapp.com/oauth2/authorize?client_id={self.bot.user.id}" f"&scope=bot&permissions={opt.invite_perms}") @@ -196,7 +197,8 @@ class BaseCog(commands.Cog): inline=False) if opt.enable_invite_cmd and (await self.bot.application_info()).bot_public: embed.add_field(name="Invite qrm to Your Server", value=self.bot_invite, inline=False) - embed.set_thumbnail(url=str(self.bot.user.avatar_url)) + if self.bot.user and self.bot.user.avatar: + embed.set_thumbnail(url=str(self.bot.user.avatar.url)) await ctx.send(embed=embed) @commands.command(name="ping", aliases=["beep"], category=cmn.BoltCats.INFO) diff --git a/main.py b/main.py index b12ee22..41d8042 100644 --- a/main.py +++ b/main.py @@ -52,6 +52,7 @@ intents.guilds = True intents.guild_messages = True intents.dm_messages = True intents.reactions = True +intents.message_content = True member_cache = discord.MemberCacheFlags.from_intents(intents) @@ -81,7 +82,7 @@ async def _restart_bot(ctx: commands.Context): await cmn.add_react(ctx.message, cmn.emojis.check_mark) print(f"[**] Restarting! Requested by {ctx.author}.") exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted. - await bot.logout() + await bot.close() @bot.command(name="shutdown", aliases=["shut"], category=cmn.BoltCats.ADMIN) @@ -92,7 +93,7 @@ async def _shutdown_bot(ctx: commands.Context): await cmn.add_react(ctx.message, cmn.emojis.check_mark) print(f"[**] Shutting down! Requested by {ctx.author}.") exit_code = 0 # Signals to the wrapper script that the bot should not be restarted. - await bot.logout() + await bot.close() @bot.group(name="extctl", aliases=["ex"], case_insensitive=True, category=cmn.BoltCats.ADMIN) @@ -123,10 +124,10 @@ async def _extctl_load(ctx: commands.Context, extension: str): """Loads an extension.""" try: bot.load_extension(ext_dir + "." + extension) - except commands.ExtensionNotFound as e: + except discord.errors.ExtensionNotFound as e: try: bot.load_extension(plugin_dir + "." + extension) - except commands.ExtensionNotFound: + except discord.errors.ExtensionNotFound: raise e await cmn.add_react(ctx.message, cmn.emojis.check_mark) @@ -140,10 +141,10 @@ async def _extctl_reload(ctx: commands.Context, extension: str): await cmn.add_react(ctx.message, pika) try: bot.reload_extension(ext_dir + "." + extension) - except commands.ExtensionNotLoaded as e: + except discord.errors.ExtensionNotLoaded as e: try: bot.reload_extension(plugin_dir + "." + extension) - except commands.ExtensionNotLoaded: + except discord.errors.ExtensionNotLoaded: raise e await cmn.add_react(ctx.message, cmn.emojis.check_mark) @@ -153,10 +154,10 @@ async def _extctl_unload(ctx: commands.Context, extension: str): """Unloads an extension.""" try: bot.unload_extension(ext_dir + "." + extension) - except commands.ExtensionNotLoaded as e: + except discord.errors.ExtensionNotLoaded as e: try: bot.unload_extension(plugin_dir + "." + extension) - except commands.ExtensionNotLoaded: + except discord.errors.ExtensionNotLoaded: raise e await cmn.add_react(ctx.message, cmn.emojis.check_mark) diff --git a/requirements.txt b/requirements.txt index 3f7922c..e676e37 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ -discord.py~=1.7.3 +py-cord~=2.3.2 +aiohttp[speedups] ctyparser~=2.0 gridtools~=1.0 qrztools[async]~=1.0 diff --git a/run.sh b/run.sh index 7e18840..355dc8b 100644 --- a/run.sh +++ b/run.sh @@ -34,9 +34,9 @@ while [ ! -z "$1" ]; do done -# If $PYTHON_BIN is not defined, default to 'python3.9' +# If $PYTHON_BIN is not defined, default to 'python3.11' if [ $_NO_BOTENV -eq 1 -a -z "$PYTHON_BIN" ]; then - PYTHON_BIN='python3.9' + PYTHON_BIN='python3.11' fi