File size: 2,316 Bytes
9b766b8
2b742c9
9b766b8
639fabb
9b766b8
f3b9132
9b766b8
 
 
 
86bf307
9b766b8
 
a34608a
9b766b8
f0b176c
80a30d9
ea1c14b
80a30d9
3229545
d7f86f7
0af8f16
 
 
d7f86f7
 
 
d069bca
80e69ee
d7f86f7
 
 
3229545
0af8f16
 
 
 
 
 
4c49647
0af8f16
 
 
 
 
d7f86f7
0af8f16
 
 
 
 
 
 
 
 
 
d9dba97
 
 
 
 
 
7fdf557
 
0af8f16
 
 
 
 
 
 
 
 
 
 
9b766b8
0af8f16
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import asyncio
import uvloop
import os
import logging
import threading
from threading import Event
from typing import Optional
import discord
import gradio as gr
from discord import Permissions
import discord
from discord.ext import commands
from discord.utils import oauth_url
from gradio_client import Client
from gradio_client.utils import QueueError
import aiohttp
from aiohttp.resolver import AsyncResolver
import utils

event = Event()
logging.basicConfig(level=logging.INFO)
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
HF_TOKEN=os.getenv("HF_TOKEN")

def get_client(session: Optional[str] = None) -> Client:
    client = Client("https://standarovskid-bot.hf.space/", token=HF_TOKEN)
    if session:
        client.session_hash = session
    return client

async def main():
    # Setup DNS resolver to avoid potential issues
    resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
    connector = aiohttp.TCPConnector(resolver=resolver)
    # Setup Discord bot 
    bot = commands.Bot(intents=intents, connector=connector, command_prefix="!")
  
    @bot.event
    async def on_ready():
        print(f"Logged in as {bot.user} (ID: {bot.user.id})")
        synced = await bot.tree.sync()
        print(f"Synced commands: {', '.join([s.name for s in synced])}.")
        event.set()
        print("------")    
    
    @bot.command()
    async def test(ctx, *, arg):
        thread = await ctx.create_thread(name=prompt, auto_archive_duration=60)
        loop = asyncio.get_running_loop()
        client = await loop.run_in_executor(None, get_client, None)
        job = client.submit(prompt, api_name="/slow_echo")
        wait([job])
        await ctx.send(arg)

    @bot.event
    async def on_message(message):
        try:
            if not message.author.bot:
                await bot.process_commands(message)
        except Exception as e:
            print(f"Error: {e}")
        
    # running in thread
    def run_bot():
        if not DISCORD_TOKEN:
            print("DISCORD_TOKEN NOT SET")
            event.set()
        else:
            bot.run(DISCORD_TOKEN)
    
    utils.keepalive_run()
    await bot.start(os.getenv('DISCORD_TOKEN'))

if __name__ == "__main__":
    asyncio.run(main())