standarovskid commited on
Commit
d7f86f7
·
verified ·
1 Parent(s): 40f4388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -52
app.py CHANGED
@@ -18,16 +18,24 @@ from aiohttp.resolver import AsyncResolver
18
  import utils
19
 
20
  event = Event()
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Setup Discord bot
23
- logging.basicConfig(level=logging.INFO)
24
  intents = discord.Intents.default()
25
  intents.message_content = True
26
  intents.members = True
27
-
28
- DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
29
- HF_TOKEN=os.getenv("HF_TOKEN")
30
-
31
 
32
  async def main():
33
  # Setup DNS resolver to avoid potential issues
@@ -35,55 +43,37 @@ async def main():
35
  connector = aiohttp.TCPConnector(resolver=resolver)
36
  client = Client("https://standarovskid-bot.hf.space/", token=HF_TOKEN)
37
 
38
- # Instantiate bot
39
- bot = commands.Bot(intents=intents, connector=connector, command_prefix="!")
40
 
41
- @bot.event
42
- async def on_ready():
43
- print(f"Logged in as {bot.user} (ID: {bot.user.id})")
44
- synced = await bot.tree.sync()
45
- print(f"Synced commands: {', '.join([s.name for s in synced])}.")
46
- event.set()
47
- print("------")
48
-
49
- async def response(input_text):
50
- return "fine"
51
 
52
- @bot.hybrid_command(
53
- name="echo",
54
- description="Enter some text to chat with the bot! Like this: /echo Hello, how are you?",
55
- )
56
- async def echo(ctx,*,input_text):
57
- try:
58
- await ctx.send("toto1")
59
- await ctx.channel.send("toto2")
60
- # 调用 Gradio 应用 API
61
- result = client.predict(
62
- message=input_text,
63
- api_name="/slow_echo"
64
- )
65
- await ctx.channel.send(f"yeah right lmfao 🥀🥀")
66
-
67
- return result
68
- except Exception as e:
69
- await ctx.channel.send("issueo")
70
- return f"Erreur:{e}"
71
 
72
- #@bot.event
73
- #async def on_message(message):
74
- # 忽略自己的消息
75
- #if message.author == bot.user:
76
- #return
77
 
78
- #if message.content.startswith("/echo"):
79
- #user_input = message.content[len("/echo "):]
80
- # result = call_huggingface_space(user_input)
81
- #await message.channel.send(f"yeah right lmfao 🥀🥀")
82
- #await message.channel.send(result)
83
-
84
- # Start keepalive server and bot
85
- utils.keepalive_run()
86
- await bot.start(os.getenv('DISCORD_TOKEN'))
 
 
 
 
 
 
 
 
 
87
 
88
- if __name__ == "__main__":
89
- asyncio.run(main())
 
18
  import utils
19
 
20
  event = Event()
21
+ logging.basicConfig(level=logging.INFO)
22
+ DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
23
+ HF_TOKEN=os.getenv("HF_TOKEN")
24
+
25
+ def get_client(session: Optional[str] = None) -> grc.Client:
26
+ # Setup DNS resolver to avoid potential issues
27
+ resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
28
+ connector = aiohttp.TCPConnector(resolver=resolver)
29
+ client = Client("https://standarovskid-bot.hf.space/", token=HF_TOKEN)
30
+ if session:
31
+ client.session_hash = session
32
+ return client
33
 
34
  # Setup Discord bot
 
35
  intents = discord.Intents.default()
36
  intents.message_content = True
37
  intents.members = True
38
+ bot = commands.Bot(intents=intents, connector=connector, command_prefix="!")
 
 
 
39
 
40
  async def main():
41
  # Setup DNS resolver to avoid potential issues
 
43
  connector = aiohttp.TCPConnector(resolver=resolver)
44
  client = Client("https://standarovskid-bot.hf.space/", token=HF_TOKEN)
45
 
 
 
46
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ @bot.event
49
+ async def on_ready():
50
+ print(f"Logged in as {bot.user} (ID: {bot.user.id})")
51
+ synced = await bot.tree.sync()
52
+ print(f"Synced commands: {', '.join([s.name for s in synced])}.")
53
+ event.set()
54
+ print("------")
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ @bot.command()
57
+ async def test(ctx, *, arg):
58
+ await ctx.send(arg)
 
 
59
 
60
+ # running in thread
61
+ def run_bot():
62
+ if not DISCORD_TOKEN:
63
+ print("DISCORD_TOKEN NOT SET")
64
+ event.set()
65
+ else:
66
+ bot.run(DISCORD_TOKEN)
67
+
68
+ threading.Thread(target=run_bot).start()
69
+
70
+ event.wait()
71
+
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown(
74
+ f"""
75
+ # Discord bot of https://standarovskid-bot.hf.space
76
+ """
77
+ )
78
 
79
+ demo.launch()