(attemp to) fix: thread no longer blocking
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
**/__pycache__/
|
**/__pycache__/
|
||||||
**/*_tok
|
**/*_tok
|
||||||
|
**/.idea/
|
||||||
|
|||||||
24
main.py
24
main.py
@@ -1,10 +1,10 @@
|
|||||||
import asyncio
|
from asyncio import create_task, shield, to_thread, wait_for
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
import discord
|
from discord import Game, Intents, Interaction, app_commands
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
import PICable
|
from PICable import PICable
|
||||||
|
|
||||||
if len(argv) != 3:
|
if len(argv) != 3:
|
||||||
print("Usage:\n\t" + argv[0] + " <discord_token> <github_token>")
|
print("Usage:\n\t" + argv[0] + " <discord_token> <github_token>")
|
||||||
@@ -12,7 +12,7 @@ if len(argv) != 3:
|
|||||||
discord_token = argv[1]
|
discord_token = argv[1]
|
||||||
github_token = argv[2]
|
github_token = argv[2]
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = Intents.default()
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
client = commands.Bot(command_prefix="/", intents=intents)
|
client = commands.Bot(command_prefix="/", intents=intents)
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ current_repositories = set()
|
|||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f"We have logged in as {client.user}")
|
print(f"We have logged in as {client.user}")
|
||||||
await client.change_presence(activity=discord.Game("with PIC ideas"))
|
await client.change_presence(activity=Game("with PIC ideas"))
|
||||||
try:
|
try:
|
||||||
synced = await client.tree.sync()
|
synced = await client.tree.sync()
|
||||||
print(f"Synced {len(synced)} command(s)")
|
print(f"Synced {len(synced)} command(s)")
|
||||||
@@ -31,14 +31,14 @@ async def on_ready():
|
|||||||
|
|
||||||
|
|
||||||
@client.tree.command(name="ping", description="Check the bot's latency.")
|
@client.tree.command(name="ping", description="Check the bot's latency.")
|
||||||
async def ping(interaction: discord.Interaction):
|
async def ping(interaction: Interaction):
|
||||||
latency = client.latency * 1000
|
latency = client.latency * 1000
|
||||||
await interaction.response.send_message(f"Pong! Latency: {latency:.2f}ms")
|
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.")
|
@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")
|
@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):
|
async def picable(interaction: Interaction, owner: str, repository: str):
|
||||||
await interaction.response.defer(thinking=True)
|
await interaction.response.defer(thinking=True)
|
||||||
|
|
||||||
repository_full_name = f"{owner}/{repository}"
|
repository_full_name = f"{owner}/{repository}"
|
||||||
@@ -50,13 +50,11 @@ async def picable(interaction: discord.Interaction, owner: str, repository: str)
|
|||||||
|
|
||||||
current_repositories.add(repository_full_name)
|
current_repositories.add(repository_full_name)
|
||||||
try:
|
try:
|
||||||
result_future = asyncio.get_running_loop().run_in_executor(
|
result_future = create_task(to_thread(PICable, owner, repository, github_token))
|
||||||
None, PICable.PICable, owner, repository, github_token
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
result = await asyncio.wait_for(asyncio.shield(result_future), timeout=600)
|
result = await wait_for(shield(result_future), timeout=5)
|
||||||
await interaction.followup.send(result)
|
await interaction.followup.send(result)
|
||||||
except asyncio.TimeoutError:
|
except TimeoutError:
|
||||||
await interaction.followup.send(
|
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:"
|
f"The analysis for {repository_full_name} is still taking place. The results will be posted here once the analysis is complete. :clock4:"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user