add slash commands implementation. remove threads. needs polishing.
This commit is contained in:
61
main.py
61
main.py
@@ -1,65 +1,46 @@
|
||||
# This example requires the 'message_content' intent.
|
||||
|
||||
import asyncio
|
||||
from sys import argv
|
||||
from threading import Thread
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord import app_commands
|
||||
|
||||
import PICable
|
||||
|
||||
if len(argv) != 3:
|
||||
print('Usage:\n\t' + argv[0] + ' <discord_token> <github_token>')
|
||||
print("Usage:\n\t" + argv[0] + " <discord_token> <github_token>")
|
||||
exit(1)
|
||||
discord_token = argv[1]
|
||||
github_token = argv[2]
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
client = discord.Client(intents=intents)
|
||||
client = commands.Bot(command_prefix="!", intents=intents)
|
||||
|
||||
def reply_msg(message, owner, repo):
|
||||
asyncio.run(reply_msg_async(message, owner, repo))
|
||||
|
||||
async def reply_message_async(interaction: discord.Interaction, owner: str, repo: str, event: asyncio.Event):
|
||||
await interaction.followup.send(PICable.PICable(owner, repo, github_token))
|
||||
event.set()
|
||||
return
|
||||
|
||||
async def reply_msg_async(message, owner, repo):
|
||||
await message.reply(PICable.PICable(owner, repo, github_token))
|
||||
return
|
||||
|
||||
async def check_repo_async(message, owner, repo):
|
||||
sent = await message.reply('Checking ' + owner + '/' + repo + '.. /')
|
||||
thread = Thread(target=reply_msg, args=(message, owner, repo))
|
||||
thread.start()
|
||||
counter = 0
|
||||
chars = '-\\|/'
|
||||
while thread.is_alive():
|
||||
char = chars[counter]
|
||||
await sent.edit(content='Checking ' + owner + '/' + repo + '.. ' + char)
|
||||
counter += 1
|
||||
counter %= 4
|
||||
await asyncio.sleep(3)
|
||||
return
|
||||
|
||||
@client.event
|
||||
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"))
|
||||
try:
|
||||
synced = await client.tree.sync()
|
||||
print(f"Synced {len(synced)} command(s)")
|
||||
except Exception as e:
|
||||
print(f"Failed to sync commands: {e}")
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
if message.author == client.user:
|
||||
return
|
||||
if not message.content.startswith('$picable'):
|
||||
return
|
||||
|
||||
msg : str = message.content
|
||||
parts = msg.split()
|
||||
if len(parts) != 3:
|
||||
await message.reply('Usage:\n\t$picable <owner> <repo>')
|
||||
return
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(check_repo_async(message, parts[1], parts[2]))
|
||||
return
|
||||
@client.tree.command(name="picable", description="Check a GitHub repository")
|
||||
@app_commands.describe(owner="The owner of the repository", repo="The name of the repository")
|
||||
async def picable(interaction: discord.Interaction, owner: str, repo: str):
|
||||
await interaction.response.defer()
|
||||
event = asyncio.Event()
|
||||
asyncio.create_task(reply_message_async(interaction, owner, repo, event))
|
||||
|
||||
|
||||
client.run(discord_token)
|
||||
|
||||
Reference in New Issue
Block a user