Thanks to some help earlier I have successfully migrated my bot's commands from prefixed to slash commands, however I've come to an issue I haven't been able to find the solution for, as mentioned in the title - it's the "bot is thinking..." message that comes up after a command - I can't get it to go away and eventually (even though commands are processing fine) it will come back and give an error message to say "The application did not respond" regardless of the outcome.
I have tried putting a ctx.defer() into it to acknowledge the command right away but maybe the way I've done it is part of the issue? I have a feeling that for whatever reason discord thinks that the interaction hasn't been responded to, so it's entirely possible I've done it in the wrong way.
Here's a sample command to show how it's being handled currently:
@discord.option('player_name', description='Name of the player', required=True)
async def combine(ctx: discord.ApplicationContext, player_name: str):
# Acknowledge the command immediately
await ctx.defer()
response_message = await ctx.send("CombineBot is processing your request...")
refresh_client()
# Split the player name into first and last names
names = player_name.split()
firstname = names[0]
lastname = ' '.join(names[1:])
logging.info(f"Command combine invoked by {ctx.author} for player: {firstname} {lastname}")
# Convert the name to lowercase for a case-insensitive search
search_name = f"{firstname} {lastname}".lower()
# Searching for the name in Google Sheets
for _ in range(3): # Try up to 3 times
try:
# Use a list comprehension to iterate through all rows and find the first match
matching_rows = [row for row in sheet.get_all_values() if search_name == row[0].lower().strip()]
if not matching_rows:
raise ValueError("Player not found")
headers = sheet.row_values(1) # Assuming headers are in the first row
row_values = matching_rows[0]
# Exclude headers "SortName" and "WikiLine" from the data
data_pairs = [
(header, value) for header, value in zip(headers, row_values) if header not in ["SortName", "WikiLine"]
]
# Create an embed
embed = Embed(title=f"ISFL Combine Data for {firstname} {lastname}", color=0x00ff00) # Green color
for header, value in data_pairs:
embed.add_field(name=header, value=value, inline=True)
await response_message.edit(content="", embed=embed)
logging.info(f"Successfully sent combine data for player: {firstname} {lastname}")
return # Exit the loop if successful
except ValueError as ve:
if str(ve) == "Player not found":
logging.warning(f"Player {firstname} {lastname} not found in the sheet.")
await response_message.edit(content="Player not found.")
return
except Exception as e: # Catch all other exceptions
logging.error(f"Error encountered while processing combine data for player {firstname} {lastname}: {e}")
time.sleep(5) # Wait for 5 seconds before trying again
logging.warning(f"Failed to fetch data for player {firstname} {lastname} after multiple attempts.")
await response_message.edit(content=f"Failed to fetch data for {firstname} {lastname}. Please try again later.")