major logic changes. improve performance. simplify code.
This commit is contained in:
47
main.py
47
main.py
@@ -16,15 +16,7 @@ intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
client = commands.Bot(command_prefix="/", intents=intents)
|
||||
|
||||
|
||||
async def reply_message_async(interaction: discord.Interaction, owner: str, repo: str):
|
||||
try:
|
||||
result = await asyncio.to_thread(PICable.PICable, owner, repo, github_token)
|
||||
await interaction.followup.send(result)
|
||||
except Exception as e:
|
||||
print(f"Error processing PICable check: {e}")
|
||||
await interaction.followup.send("An error occurred while processing your request. Please try again later.")
|
||||
return
|
||||
current_repositories = set()
|
||||
|
||||
|
||||
@client.event
|
||||
@@ -38,10 +30,43 @@ async def on_ready():
|
||||
print(f"Failed to sync commands: {e}")
|
||||
|
||||
|
||||
@client.tree.command(name="ping", description="Check the bot's latency.")
|
||||
async def ping(interaction: discord.Interaction):
|
||||
latency = client.latency * 1000
|
||||
await interaction.response.send_message(f"Pong! Latency: {latency:.2f} ms")
|
||||
|
||||
|
||||
@client.tree.command(name="picable", description="Check if a Github repository is eligible for PIC.")
|
||||
@discord.app_commands.describe(owner="The owner of the repository", repository="The name of the repository")
|
||||
async def picable(interaction: discord.Interaction, owner: str, repository: str):
|
||||
await interaction.response.defer(ephemeral=False, thinking=True)
|
||||
asyncio.create_task(reply_message_async(interaction, owner, repository))
|
||||
await interaction.response.defer(thinking=True)
|
||||
|
||||
repository_full_name = f"{owner}/{repository}"
|
||||
if repository_full_name in current_repositories:
|
||||
await interaction.followup.send(
|
||||
f"The repository {repository_full_name} is already being analyzed. Please wait for the current analysis to complete. :warning:"
|
||||
)
|
||||
return
|
||||
|
||||
current_repositories.add(repository_full_name)
|
||||
try:
|
||||
result_future = asyncio.get_running_loop().run_in_executor(
|
||||
None, PICable.PICable, owner, repository, github_token
|
||||
)
|
||||
try:
|
||||
result = await asyncio.wait_for(asyncio.shield(result_future), timeout=600)
|
||||
await interaction.followup.send(result)
|
||||
except asyncio.TimeoutError:
|
||||
await interaction.followup.send(
|
||||
f"The analysis for {repository_full_name} is still taking place. The results will be posted here once the analysis is complete. :clock4:"
|
||||
)
|
||||
result = await result_future
|
||||
await interaction.channel.send(result)
|
||||
except Exception as e:
|
||||
print(f"Error processing PICable check: {e}")
|
||||
await interaction.channel.send("An error occurred while processing your request. Please try again later.")
|
||||
finally:
|
||||
current_repositories.remove(repository_full_name)
|
||||
|
||||
|
||||
client.run(discord_token)
|
||||
|
||||
Reference in New Issue
Block a user