fix(bot): 🐛 Fix reply argument conversion.

This commit is contained in:
Nyeki.py
2024-10-17 20:42:58 -03:00
parent 15368e4a97
commit 62e2d30fdf
3 changed files with 31 additions and 31 deletions
+3
View File
@@ -337,6 +337,9 @@ class Payphone(commands.AutoShardedBot):
f"❌ You're going too fast! Wait `{int(exception.retry_after)}` seconds before trying again." f"❌ You're going too fast! Wait `{int(exception.retry_after)}` seconds before trying again."
) )
elif isinstance(exception, errors.NotOwner):
await context.send("❌ You're not the owner of this bot.")
elif isinstance(exception, errors.CommandNotFound): elif isinstance(exception, errors.CommandNotFound):
return return
+6 -12
View File
@@ -28,7 +28,7 @@ class Mods(base.Cog):
async def ban( async def ban(
self, self,
ctx: commands.Context, ctx: commands.Context,
user: discord.User | discord.Member | converters.DiscordUser, user: converters.DiscordUserID,
time: converters.Timedelta | None = None, time: converters.Timedelta | None = None,
*, *,
reason: str | None = "", reason: str | None = "",
@@ -42,12 +42,6 @@ class Mods(base.Cog):
- `[<reason>]`: The reason of their ban. - `[<reason>]`: The reason of their ban.
""" """
user_id = None
if isinstance(user, (discord.User, discord.Member)):
user_id = user.id
else:
user_id = user user_id = user
profile = await models.Profile.filter(user_id=user_id).limit(1).first() profile = await models.Profile.filter(user_id=user_id).limit(1).first()
@@ -88,7 +82,7 @@ class Mods(base.Cog):
async def unban( async def unban(
self, self,
ctx: commands.Context, ctx: commands.Context,
user: discord.User | discord.Member | converters.DiscordUser, user: converters.DiscordUserID,
) -> None: ) -> None:
""" """
Unbans a user by their ID, pinging them, or replying to their message. The user will be Unbans a user by their ID, pinging them, or replying to their message. The user will be
@@ -98,7 +92,7 @@ class Mods(base.Cog):
- `<user>`: The user to unban or their ID. - `<user>`: The user to unban or their ID.
""" """
user_id = user if isinstance(user, int) else user.id user_id = user
await self.bot.unban(user_id) await self.bot.unban(user_id)
@@ -121,7 +115,7 @@ class Mods(base.Cog):
async def warn( async def warn(
self, self,
ctx: commands.Context, ctx: commands.Context,
user: discord.User | discord.Member | converters.DiscordUser, user: converters.DiscordUserID,
*, *,
reason: str, reason: str,
) -> None: ) -> None:
@@ -133,7 +127,7 @@ class Mods(base.Cog):
- `<reason>`: The reason of the warn. - `<reason>`: The reason of the warn.
""" """
user_id = user if isinstance(user, int) else user.id user_id = user
user = None user = None
@@ -316,7 +310,7 @@ class Mods(base.Cog):
f"Profiles: {await models.Profile.all().count()}", f"Profiles: {await models.Profile.all().count()}",
f"Servers: {await models.Server.all().count()}", f"Servers: {await models.Server.all().count()}",
f"Shards: {self.bot.shard_count}", f"Shards: {self.bot.shard_count}",
f"Current Shard: {ctx.guild.shard_id + 1}" f"Current Shard: {ctx.guild.shard_id + 1}",
] ]
await ctx.send(f"```\n{"\n".join(stats)}\n```") await ctx.send(f"```\n{"\n".join(stats)}\n```")
+17 -14
View File
@@ -3,6 +3,8 @@ from datetime import timedelta
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import converter from discord.ext.commands import converter
from tortoise import expressions
from payphone.db import models from payphone.db import models
from payphone.core import exceptions from payphone.core import exceptions
@@ -38,10 +40,13 @@ class Timedelta(converter.Converter):
) )
class DiscordUser(converter.Converter): class DiscordUserID(converter.Converter):
"""Converts a URL, ID, or reply into a Discord User ID.""" """Converts a URL, ID, or reply into a Discord User ID."""
async def converter(self, ctx: commands.Context, argument: str) -> int: async def convert(self, ctx: commands.Context, argument: str) -> int:
if len(argument) > 17 and argument[:2] == "<@" and argument[-1] == ">":
return int(argument[2:-1])
if argument.isdecimal() and len(argument) in range(17, 19): if argument.isdecimal() and len(argument) in range(17, 19):
return int(argument) return int(argument)
@@ -50,27 +55,25 @@ class DiscordUser(converter.Converter):
): ):
return int(argument.split("?", 1)[0].rsplit("/", 1)[1]) return int(argument.split("?", 1)[0].rsplit("/", 1)[1])
if argument.lower() in ["reply", "."]:
if ctx.message.reference is not None: if ctx.message.reference is not None:
ref = ctx.message.reference ref = ctx.message.reference
if ref.cached_message is not None: msg = ref.cached_message or await ctx.channel().fetch_message(
msg = ref.cached_message ref.message_id
)
if msg.author.bot: if msg.author.bot:
row = await models.Message.get_or_none(id=msg.id) row = await models.Message.get_or_none(
expressions.Q(id=msg.id, propagation_id=msg.id, join_type="OR")
)
if row is not None: if row is not None:
return row.user_id return row.user_id
raise ValueError() raise converter.BadArgument()
else:
return msg.author.id return msg.author.id
row = await models.Message.get_or_none(id=ref.message_id) raise converter.BadArgument()
if row is not None:
return row.user_id
raise ValueError()
raise ValueError()