Spaces:
Running
Running
Add discord dynamic command and message
Browse files- Dockerfile +7 -5
- discord.json +57 -0
- discord_bot.py +106 -0
Dockerfile
CHANGED
@@ -14,16 +14,18 @@ RUN apt-get install ffmpeg -y
|
|
14 |
COPY ./requirements.txt /discordbot/requirements.txt
|
15 |
RUN pip install -U -r /discordbot/requirements.txt
|
16 |
|
17 |
-
|
18 |
-
COPY ./
|
19 |
-
COPY ./
|
20 |
COPY ./index.html /discordbot/templates/index.html
|
21 |
COPY ./disclaimer.html /discordbot/templates/disclaimer.html
|
22 |
COPY ./eula.html /discordbot/templates/eula.html
|
23 |
COPY ./privacy_policy.html /discordbot/templates/privacy_policy.html
|
24 |
COPY ./terms_of_service.html /discordbot/templates/terms_of_service.html
|
25 |
-
COPY ./
|
26 |
-
COPY ./
|
|
|
27 |
COPY ./horde.py /discordbot/horde.py
|
|
|
28 |
|
29 |
CMD ["python", "/discordbot/server.py"]
|
|
|
14 |
COPY ./requirements.txt /discordbot/requirements.txt
|
15 |
RUN pip install -U -r /discordbot/requirements.txt
|
16 |
|
17 |
+
|
18 |
+
COPY ./client.js /discordbot/static/client.js
|
19 |
+
COPY ./style.css /discordbot/static/style.css
|
20 |
COPY ./index.html /discordbot/templates/index.html
|
21 |
COPY ./disclaimer.html /discordbot/templates/disclaimer.html
|
22 |
COPY ./eula.html /discordbot/templates/eula.html
|
23 |
COPY ./privacy_policy.html /discordbot/templates/privacy_policy.html
|
24 |
COPY ./terms_of_service.html /discordbot/templates/terms_of_service.html
|
25 |
+
COPY ./server.py /discordbot/server.py
|
26 |
+
COPY ./pingpong.py /discordbot/pingpong.py
|
27 |
+
COPY ./discord_bot.py /discordbot/discord_bot.py
|
28 |
COPY ./horde.py /discordbot/horde.py
|
29 |
+
COPY ./discord.json /discordbot/discord.json
|
30 |
|
31 |
CMD ["python", "/discordbot/server.py"]
|
discord.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"message": [
|
3 |
+
{
|
4 |
+
"type": "equals",
|
5 |
+
"content": "ping",
|
6 |
+
"response": "pong"
|
7 |
+
},
|
8 |
+
{
|
9 |
+
"type": "startswith",
|
10 |
+
"content": "$hello",
|
11 |
+
"response": "Hello!"
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"type": "equals",
|
15 |
+
"get kudos",
|
16 |
+
"function": "getKudos"
|
17 |
+
}
|
18 |
+
],
|
19 |
+
"command": [
|
20 |
+
{
|
21 |
+
"name": "hello",
|
22 |
+
"description": "Sends a greeting!",
|
23 |
+
"function": "hello",
|
24 |
+
"parameters": []
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"name": "greet",
|
28 |
+
"description": "Greets the specified user",
|
29 |
+
"function": "greet",
|
30 |
+
"parameters": [
|
31 |
+
{
|
32 |
+
"name": "name",
|
33 |
+
"type": "str",
|
34 |
+
"description": "The user to greet"
|
35 |
+
}
|
36 |
+
]
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"name": "get-kudos",
|
40 |
+
"description": "The amount of Kudos this user has.",
|
41 |
+
"function": "getKudos",
|
42 |
+
"parameters": []
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"name": "generate-status",
|
46 |
+
"description" "Retrieve the status of an Asynchronous generation request.",
|
47 |
+
"function": "generateStatus",
|
48 |
+
"parameters": [
|
49 |
+
{
|
50 |
+
"name": "id",
|
51 |
+
"type": "str",
|
52 |
+
"description": "The ID of asyncronous generation request."
|
53 |
+
}
|
54 |
+
]
|
55 |
+
}
|
56 |
+
]
|
57 |
+
}
|
discord_bot.py
CHANGED
@@ -10,12 +10,33 @@ import json
|
|
10 |
from horde import HordeAPI
|
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}!")
|
@@ -58,11 +79,70 @@ async def generateStatus(interaction: discord.Interaction, id: str):
|
|
58 |
await interaction.followup.send(f'Processing image: {details["processing"]}/{total}')
|
59 |
return
|
60 |
await interaction.followup.send(f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s')
|
|
|
61 |
|
62 |
@bot.command()
|
63 |
async def ping(ctx):
|
64 |
await ctx.send('pong')
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
@bot.event
|
67 |
async def on_ready():
|
68 |
await tree.sync()
|
@@ -74,11 +154,34 @@ async def on_message(message):
|
|
74 |
if message.author == bot.user:
|
75 |
return
|
76 |
|
|
|
77 |
if message.content == 'ping':
|
78 |
await message.channel.send('pong')
|
79 |
|
80 |
if message.content.startswith('$hello'):
|
81 |
await message.channel.send('Hello!')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
async def sendMessageToChannelHelper(data):
|
84 |
channel = await bot.fetch_channel(os.environ.get("CHANNEL_ID"))
|
@@ -106,9 +209,11 @@ async def sendMessageToChannelHelper(data):
|
|
106 |
# 发送 embed 消息
|
107 |
await channel.send(embed=embed)
|
108 |
|
|
|
109 |
def sendMessageToChannel(data):
|
110 |
bot.loop.create_task(sendMessageToChannelHelper(data))
|
111 |
|
|
|
112 |
def run():
|
113 |
try:
|
114 |
token = os.environ.get("TOKEN") or ""
|
@@ -126,6 +231,7 @@ def run():
|
|
126 |
else:
|
127 |
raise e
|
128 |
|
|
|
129 |
def discord_bot():
|
130 |
print("Running discord_bot")
|
131 |
run()
|
|
|
10 |
from horde import HordeAPI
|
11 |
|
12 |
|
13 |
+
TYPE_MAPPING = {
|
14 |
+
"str": str,
|
15 |
+
"int": int,
|
16 |
+
"float": float
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
# 创建一个字典,将规则类型映射到相应的条件检查函数
|
21 |
+
check_functions = {
|
22 |
+
"equals": lambda content, message: message.content == content,
|
23 |
+
"contains": lambda content, message: content in message.content,
|
24 |
+
"startswith": lambda content, message: message.content.startswith(content),
|
25 |
+
"endswith": lambda content, message: message.content.endswith(content)
|
26 |
+
}
|
27 |
+
|
28 |
|
29 |
intents = discord.Intents.default()
|
30 |
intents.message_content = True
|
31 |
bot = commands.Bot(command_prefix='>', intents=intents)
|
32 |
tree = bot.tree
|
33 |
|
34 |
+
|
35 |
+
with open("discord.json", "r") as f:
|
36 |
+
json_data = json.load(f)
|
37 |
+
|
38 |
+
|
39 |
+
"""
|
40 |
@tree.command(name="hello", description="Sends a greeting!")
|
41 |
async def hello(interaction: discord.Interaction):
|
42 |
await interaction.response.send_message(f"Hello, {interaction.user.mention}!")
|
|
|
79 |
await interaction.followup.send(f'Processing image: {details["processing"]}/{total}')
|
80 |
return
|
81 |
await interaction.followup.send(f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s')
|
82 |
+
"""
|
83 |
|
84 |
@bot.command()
|
85 |
async def ping(ctx):
|
86 |
await ctx.send('pong')
|
87 |
|
88 |
+
async def hello():
|
89 |
+
return f"Hello, {interaction.user.mention}!"
|
90 |
+
|
91 |
+
async def greet(name: str):
|
92 |
+
return f"Hello, {name}!"
|
93 |
+
|
94 |
+
async def getKudos():
|
95 |
+
async with HordeAPI.getUserDetails() as details:
|
96 |
+
if "kudos" not in details:
|
97 |
+
return f'Error: {details["code"]} {details["reason"]}'
|
98 |
+
return f'The amount of Kudos this user has is {details["kudos"]}'
|
99 |
+
|
100 |
+
async def generateStatus(id: str):
|
101 |
+
async with HordeAPI.generateCheck(id) as details:
|
102 |
+
if "kudos" not in details:
|
103 |
+
return f'Check Error: {details["code"]} {details["reason"]}'
|
104 |
+
if bool(details["is_possible"]) == False:
|
105 |
+
return "This generation is impossible."
|
106 |
+
if bool(details["faulted"]) == True:
|
107 |
+
return "This generation is faulted."
|
108 |
+
if bool(details["done"]) == True:
|
109 |
+
async with HordeAPI.generateStatus(id) as generationDetail:
|
110 |
+
if "generations" not in generationDetail:
|
111 |
+
return f'Status Error: {generationDetail["code"]} {generationDetail["reason"]}'
|
112 |
+
for i in range(len(generationDetail["generations"])):
|
113 |
+
return generationDetail["generations"][i]["img"]
|
114 |
+
if int(details["processing"]) > 0:
|
115 |
+
total = int(details["finished"]) + int(details["processing"]) + int(details["queue_position"]) + int(details["restarted"]) + int(details["waiting"])
|
116 |
+
return f'Processing image: {details["processing"]}/{total}'
|
117 |
+
return f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s'
|
118 |
+
|
119 |
+
|
120 |
+
# 根据 json 数据动态创建命令
|
121 |
+
for command in json_data["command"]:
|
122 |
+
async def dynamic_command(interaction: discord.Interaction, **kwargs):
|
123 |
+
await interaction.response.defer()
|
124 |
+
# 动态调用命令对应的函数
|
125 |
+
function_name = command["function"]
|
126 |
+
function = globals()[function_name]
|
127 |
+
result = await function(**kwargs)
|
128 |
+
await interaction.followup.send(result)
|
129 |
+
|
130 |
+
# 动态创建参数
|
131 |
+
params = [app_commands.Parameter(name=param["name"], description=param["description"], type=TYPE_MAPPING[param["type"]])
|
132 |
+
for param in command["parameters"]]
|
133 |
+
|
134 |
+
# 动态创建命令并注册
|
135 |
+
tree_command = app_commands.Command(
|
136 |
+
name=command["name"],
|
137 |
+
description=command["description"],
|
138 |
+
callback=dynamic_command,
|
139 |
+
params=params
|
140 |
+
)
|
141 |
+
|
142 |
+
# 将命令添加到 bot 的 command tree
|
143 |
+
bot.tree.add_command(tree_command)
|
144 |
+
|
145 |
+
|
146 |
@bot.event
|
147 |
async def on_ready():
|
148 |
await tree.sync()
|
|
|
154 |
if message.author == bot.user:
|
155 |
return
|
156 |
|
157 |
+
"""
|
158 |
if message.content == 'ping':
|
159 |
await message.channel.send('pong')
|
160 |
|
161 |
if message.content.startswith('$hello'):
|
162 |
await message.channel.send('Hello!')
|
163 |
+
"""
|
164 |
+
for rule in json_data["message"]:
|
165 |
+
rule_type = rule["type"]
|
166 |
+
content = rule["content"]
|
167 |
+
response = rule["response"]
|
168 |
+
|
169 |
+
# 根据规则类型动态调用对应的判断函数
|
170 |
+
if check_functions.get(rule_type, lambda c, m: False)(content, message):
|
171 |
+
# 如果规则指定了函数,则调用对应的函数
|
172 |
+
if "function" in rule:
|
173 |
+
function_name = rule["function"]
|
174 |
+
function = globals()[function_name]
|
175 |
+
await function(message)
|
176 |
+
break
|
177 |
+
# 否则发送预定义的响应消息
|
178 |
+
elif "response" in rule:
|
179 |
+
await message.channel.send(rule["response"])
|
180 |
+
break
|
181 |
+
|
182 |
+
# 确保命令系统正常工作
|
183 |
+
await bot.process_commands(message)
|
184 |
+
|
185 |
|
186 |
async def sendMessageToChannelHelper(data):
|
187 |
channel = await bot.fetch_channel(os.environ.get("CHANNEL_ID"))
|
|
|
209 |
# 发送 embed 消息
|
210 |
await channel.send(embed=embed)
|
211 |
|
212 |
+
|
213 |
def sendMessageToChannel(data):
|
214 |
bot.loop.create_task(sendMessageToChannelHelper(data))
|
215 |
|
216 |
+
|
217 |
def run():
|
218 |
try:
|
219 |
token = os.environ.get("TOKEN") or ""
|
|
|
231 |
else:
|
232 |
raise e
|
233 |
|
234 |
+
|
235 |
def discord_bot():
|
236 |
print("Running discord_bot")
|
237 |
run()
|