joytou commited on
Commit
4b6b17e
1 Parent(s): 9c57190

Support for horde api

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -1
  2. discord_bot.py +44 -4
  3. horde.py +46 -0
  4. requirements.txt +4 -0
Dockerfile CHANGED
@@ -12,7 +12,6 @@ RUN apt-get install wget -y
12
  RUN apt-get install ffmpeg -y
13
 
14
  COPY ./requirements.txt /discordbot/requirements.txt
15
- RUN pip install python-dotenv typing-extensions
16
  RUN pip install -U -r /discordbot/requirements.txt
17
 
18
  COPY ./discord_bot.py /discordbot/discord_bot.py
 
12
  RUN apt-get install ffmpeg -y
13
 
14
  COPY ./requirements.txt /discordbot/requirements.txt
 
15
  RUN pip install -U -r /discordbot/requirements.txt
16
 
17
  COPY ./discord_bot.py /discordbot/discord_bot.py
discord_bot.py CHANGED
@@ -2,20 +2,60 @@
2
  # https://discordpy.readthedocs.io/en/stable/quickstart.html#a-minimal-bot
3
 
4
  import os
5
-
6
  import discord
7
-
8
  from discord.ext import commands
9
-
10
  from threading import Thread
11
-
12
  import json
 
13
 
14
 
15
 
16
  intents = discord.Intents.default()
17
  intents.message_content = True
18
  bot = commands.Bot(command_prefix='>', intents=intents)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  @bot.command()
21
  async def ping(ctx):
 
2
  # https://discordpy.readthedocs.io/en/stable/quickstart.html#a-minimal-bot
3
 
4
  import os
 
5
  import discord
6
+ from discord import app_commands
7
  from discord.ext import commands
 
8
  from threading import Thread
 
9
  import json
10
+ import horde
11
 
12
 
13
 
14
  intents = discord.Intents.default()
15
  intents.message_content = True
16
  bot = commands.Bot(command_prefix='>', intents=intents)
17
+ tree = bot.tree
18
+
19
+ @tree.command(name="hello", description="Sends a greeting!")
20
+ async def hello(interaction: discord.Interaction):
21
+ await interaction.response.send_message(f"Hello, {interaction.user.mention}!")
22
+
23
+ @tree.command(name="greet", description="Greets the specified user")
24
+ async def greet(interaction: discord.Interaction, name: str):
25
+ await interaction.response.send_message(f"Hello, {name}!")
26
+
27
+ @tree.command(name="get kudos", description="The amount of Kudos this user has.")
28
+ async def getKudos(interaction: discord.Interaction):
29
+ details = horde.getUserDetails()
30
+ if "kudos" not in details:
31
+ await interaction.response.send_message(f'Error: {details["code"]} {details["reason"]}')
32
+ return
33
+ await interaction.response.send_message(f'The amount of Kudos this user has is {details["kudos"]}')
34
+
35
+ @tree.command(name="generate status", description="Retrieve the status of an Asynchronous generation request.")
36
+ async def generateStatus(interaction: discord.Interaction, id: str):
37
+ details = horde.generateCheck(id)
38
+ if "kudos" not in details:
39
+ await interaction.response.send_message(f'Error: {details["code"]} {details["reason"]}')
40
+ return
41
+ if bool(details["is_possible"]) == False:
42
+ await interaction.response.send_message("This generation is impossible.")
43
+ return
44
+ if bool(details["faulted"]) == True:
45
+ await interaction.response.send_message("This generation is faulted.")
46
+ return
47
+ if bool(details["done"]) == True:
48
+ generationDetail = horde.generateStatus(id)
49
+ if "generations" not in generationDetail:
50
+ await interaction.response.send_message(f'Error: {details["code"]} {details["reason"]}')
51
+ for i in range(len(generationDetail["generations"])):
52
+ await interaction.response.send_message(generationDetail["generations"][i]["img"])
53
+ return
54
+ if int(details["processing"]) > 0:
55
+ total = int(details["finished"]) + int(details["processing"]) + int(details["queue_position"]) + int(details["restarted"]) + int(details["waiting"])
56
+ await interaction.response.send_message(f'Processing image: {details["processing"]}/{total}')
57
+ return
58
+ await interaction.response.send_message(f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s')
59
 
60
  @bot.command()
61
  async def ping(ctx):
horde.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+
4
+ hordeApiKey = os.environ.get("HORDE_API_KEY")
5
+ baseUrl = "https://aihorde.net/api/v2/"
6
+
7
+ def getResponseByGet(url):
8
+ headers = {
9
+ "Content-Type": "application/json",
10
+ "apikey": f"{hordeApiKey}"
11
+ }
12
+ response = requests.get(url, headers=headers)
13
+ if response.status_code == 200:
14
+ return response.json()
15
+ else:
16
+ return {
17
+ "code": response.status_code,
18
+ "reason": response.reason
19
+ }
20
+
21
+ def getResponseByPost(url):
22
+ headers = {
23
+ "Content-Type": "application/json",
24
+ "apikey": f"{hordeApiKey}"
25
+ }
26
+ response = requests.post(url, headers=headers)
27
+ if response.status_code == 200:
28
+ return response.json()
29
+ else:
30
+ return {
31
+ "code": response.status_code,
32
+ "reason": response.reason
33
+ }
34
+
35
+ def getUserDetails():
36
+ url = f"{baseUrl}find_user"
37
+ return getResponseByPost(url)
38
+
39
+ def generateCheck(id):
40
+ url = f"{baseUrl}generate/check/{id}"
41
+ return getResponseByGet(url)
42
+
43
+ def generateStatus(id):
44
+ url = f"{baseUrl}generate/status/{id}"
45
+ return getResponseByGet(url)
46
+
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  flask[async]
2
  typing-extensions
3
  quart
@@ -5,3 +6,6 @@ discord
5
  discord.py
6
  urllib3==1.26.5
7
  requests
 
 
 
 
1
+ python-dotenv
2
  flask[async]
3
  typing-extensions
4
  quart
 
6
  discord.py
7
  urllib3==1.26.5
8
  requests
9
+ py-cord
10
+ discord-py-interactions
11
+ nextcord