Fix timeout command
This commit is contained in:
parent
5f575e49d2
commit
d94cea9c0c
@ -429,46 +429,61 @@ class ModerationCog(commands.Cog):
|
||||
|
||||
async def moderate_timeout_callback(self, interaction: discord.Interaction, member: discord.Member, duration: str, reason: str = None):
|
||||
"""Timeout a member in the server."""
|
||||
|
||||
# Defer the response immediately to prevent expiration
|
||||
try:
|
||||
if not interaction.response.is_done():
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to defer interaction: {e}")
|
||||
|
||||
# Helper for always using followup after defer
|
||||
async def safe_followup(content=None, *, ephemeral=False, embed=None):
|
||||
try:
|
||||
await interaction.followup.send(content, ephemeral=ephemeral, embed=embed)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to send followup response: {e}")
|
||||
|
||||
# Check if the user has permission to moderate members
|
||||
if not interaction.user.guild_permissions.moderate_members:
|
||||
await interaction.response.send_message("❌ You don't have permission to timeout members.", ephemeral=True)
|
||||
await safe_followup("❌ You don't have permission to timeout members.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if the bot has permission to moderate members
|
||||
if not interaction.guild.me.guild_permissions.moderate_members:
|
||||
await interaction.response.send_message("❌ I don't have permission to timeout members.", ephemeral=True)
|
||||
await safe_followup("❌ I don't have permission to timeout members.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if the user is trying to timeout themselves
|
||||
if member.id == interaction.user.id:
|
||||
await interaction.response.send_message("❌ You cannot timeout yourself.", ephemeral=True)
|
||||
await safe_followup("❌ You cannot timeout yourself.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if the user is trying to timeout the bot
|
||||
if member.id == self.bot.user.id:
|
||||
await interaction.response.send_message("❌ I cannot timeout myself.", ephemeral=True)
|
||||
await safe_followup("❌ I cannot timeout myself.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if the user is trying to timeout someone with a higher role
|
||||
if interaction.user.top_role <= member.top_role and interaction.user.id != interaction.guild.owner_id:
|
||||
await interaction.response.send_message("❌ You cannot timeout someone with a higher or equal role.", ephemeral=True)
|
||||
await safe_followup("❌ You cannot timeout someone with a higher or equal role.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if the bot can timeout the member (role hierarchy)
|
||||
if interaction.guild.me.top_role <= member.top_role:
|
||||
await interaction.response.send_message("❌ I cannot timeout someone with a higher or equal role than me.", ephemeral=True)
|
||||
await safe_followup("❌ I cannot timeout someone with a higher or equal role than me.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Parse the duration
|
||||
delta = self._parse_duration(duration)
|
||||
if not delta:
|
||||
await interaction.response.send_message("❌ Invalid duration format. Please use formats like '1d', '2h', '30m', or '60s'.", ephemeral=True)
|
||||
await safe_followup("❌ Invalid duration format. Please use formats like '1d', '2h', '30m', or '60s'.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Check if the duration is within Discord's limits (max 28 days)
|
||||
max_timeout = datetime.timedelta(days=28)
|
||||
if delta > max_timeout:
|
||||
await interaction.response.send_message("❌ Timeout duration cannot exceed 28 days.", ephemeral=True)
|
||||
await safe_followup("❌ Timeout duration cannot exceed 28 days.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Calculate the end time
|
||||
@ -518,11 +533,11 @@ class ModerationCog(commands.Cog):
|
||||
|
||||
# Send confirmation message with DM status
|
||||
dm_status = "✅ DM notification sent" if dm_sent else "❌ Could not send DM notification (user may have DMs disabled)"
|
||||
await interaction.response.send_message(f"⏰ **Timed out {member.mention}** for {duration}! Reason: {reason or 'No reason provided'}\n{dm_status}")
|
||||
await safe_followup(f"⏰ **Timed out {member.mention}** for {duration}! Reason: {reason or 'No reason provided'}\n{dm_status}")
|
||||
except discord.Forbidden:
|
||||
await interaction.response.send_message("❌ I don't have permission to timeout this member.", ephemeral=True)
|
||||
await safe_followup("❌ I don't have permission to timeout this member.", ephemeral=True)
|
||||
except discord.HTTPException as e:
|
||||
await interaction.response.send_message(f"❌ An error occurred while timing out the member: {e}", ephemeral=True)
|
||||
await safe_followup(f"❌ An error occurred while timing out the member: {e}", ephemeral=True)
|
||||
|
||||
async def moderate_remove_timeout_callback(self, interaction: discord.Interaction, member: discord.Member, reason: str = None):
|
||||
"""Remove a timeout from a member."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user