Python 3.6 Discord Bot状态命令:冷却与执行反馈提示问题
Hey there! Let's fix those missing feedback messages for your /status command. The issue is twofold: you're not sending a success message after updating the status, and you haven't set up error handling for the cooldown. Here's how to fix both:
First, your original function was missing the ctx (context) parameter—this is critical for sending messages back to the channel where the command was used. We'll add that, plus a tailored success message based on whether the user provided a status or not:
@client.command(name="status") @commands.cooldown(1, 15, commands.BucketType.server) async def client_status(ctx, *, status: str = None): """Change the bot's 'playing' status. Running this command without any arguments will turn the 'playing' status off""" game = Game(name=status) await client.change_presence(game=game) # Send confirmation to the user if status: await ctx.send(f"Successfully updated my playing status to: *{status}*") else: await ctx.send("Successfully turned off my playing status!")
The if/else check ensures the user gets a specific, clear confirmation depending on their input.
Discord.py raises a CommandOnCooldown exception when a user hits the cooldown limit. We need to catch this exception and send a friendly message with the remaining time:
Add this error handler right below your command function:
@client_status.error async def client_status_error(ctx, error): if isinstance(error, commands.CommandOnCooldown): # Round the remaining time to make it look cleaner remaining_time = round(error.retry_after, 1) await ctx.send(f"Slow down! You can use this command again in *{remaining_time} seconds*.") # Re-raise other errors to help with debugging (optional but useful) else: raise error
This decorator links the error handler directly to your /status command. We check if the error is a cooldown issue, then format and send the remaining time to the user.
Putting it all together, your complete code will look like this:
from discord.ext import commands from discord import Game @client.command(name="status") @commands.cooldown(1, 15, commands.BucketType.server) async def client_status(ctx, *, status: str = None): """Change the bot's 'playing' status. Running this command without any arguments will turn the 'playing' status off""" game = Game(name=status) await client.change_presence(game=game) # Send success confirmation if status: await ctx.send(f"Successfully updated my playing status to: *{status}*") else: await ctx.send("Successfully turned off my playing status!") @client_status.error async def client_status_error(ctx, error): if isinstance(error, commands.CommandOnCooldown): remaining_time = round(error.retry_after, 1) await ctx.send(f"Slow down! You can use this command again in *{remaining_time} seconds*.") else: raise error
- The
ctxparameter is essential for interacting with the server/channel where the command was run—never forget it in your command functions! - The
*,in the parameters lets users input multi-word statuses (like/status Playing Minecraft) without needing to wrap the text in quotes. - Rounding the cooldown time makes the message feel more polished instead of showing a long, messy float value.
内容的提问来源于stack exchange,提问作者Lukim

