File size: 6,062 Bytes
9cf96c4
 
 
 
 
4b6b17e
9cf96c4
 
 
a3fa62e
a38c7a5
9cf96c4
 
bb67179
 
 
 
 
 
 
 
 
9cf96c4
400b811
9cf96c4
e5419b0
b442dbe
 
4b6b17e
bb67179
400b811
bb67179
 
 
400b811
cb390bc
400b811
 
 
 
cb390bc
 
 
 
 
 
 
 
400b811
 
 
 
 
cb390bc
400b811
a38c7a5
bb67179
 
 
 
400b811
bb67179
 
 
 
 
400b811
bb67179
 
 
400b811
bb67179
400b811
bb67179
400b811
 
 
 
 
 
bb67179
400b811
 
 
 
 
bb67179
 
 
 
be328a1
 
 
 
 
9cf96c4
 
b442dbe
0a11892
9cf96c4
 
 
 
 
 
 
 
bb67179
 
 
 
 
 
 
 
 
 
 
400b811
 
bb67179
 
400b811
bb67179
 
 
 
9cf96c4
 
6ac2b5f
9cf96c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb67179
9cf96c4
 
 
bb67179
9cf96c4
 
6ac2b5f
9cf96c4
 
 
 
 
 
 
 
 
 
 
 
 
 
bb67179
9cf96c4
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# This code is based on the following example:
# https://discordpy.readthedocs.io/en/stable/quickstart.html#a-minimal-bot

import os
import discord
from discord import app_commands
from discord.ext import commands
from threading import Thread
import json
from horde import HordeAPI
import inspect



# 创建一个字典,将规则类型映射到相应的条件检查函数
check_functions = {
    "equals": lambda content, message: message.content == content,
    "contains": lambda content, message: content in message.content,
    "startswith": lambda content, message: message.content.startswith(content),
    "endswith": lambda content, message: message.content.endswith(content)
}


# 定义bot和tree
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>', intents=intents)
tree = bot.tree


# 读取json
with open("discord.json", "r") as f:
    json_data = json.load(f)


# 自动生成调用函数
COMMAND_NAME_PREFIX="discord_bot_call_"
FILE_CONTENT="\n\n".join([
f"async def {COMMAND_NAME_PREFIX}{command['name']}"
+"(interaction: discord.Interaction"
+''.join([f", {param['name']}: {param['type']}" for param in command["parameters"]])
+f"):\n\
    await interaction.response.defer()\n\
    result = await {command['function']}("
+', '.join([f"{param['name']}={param['name']}" for param in command["parameters"]])
+f")\n\
    if result is not None:\n\
        await interaction.followup.send(result)\n\
tree.add_command(app_commands.Command(\
name=\"{command['name']}\", \
description=\"{command['description']}\", \
callback={COMMAND_NAME_PREFIX}{command['name']}\
))"
for command in json_data["command"]])
exec(FILE_CONTENT)


async def greet(name: str):
    return f"Hello, {name}!"

async def get_kudos():
    async with HordeAPI.getUserDetails() as details:
        if "kudos" not in details:
            return f'Error: {details["code"]} {details["reason"]}'
        return f'The amount of Kudos this user has is {details["kudos"]}'

async def generate_status(id: str):
    async with HordeAPI.generateCheck(id) as details:
        if "kudos" not in details:
            return f'Check Error: {details["code"]} {details["reason"]}'
        if bool(details["is_possible"]) is False:
            return "This generation is impossible."
        if bool(details["faulted"]) is True:
            return "This generation is faulted."
        if bool(details["done"]) is True:
            async with HordeAPI.generateStatus(id) as generation_detail:
                if "generations" not in generation_detail:
                    return f'Status Error: {generation_detail["code"]} {generation_detail["reason"]}'
                for i in range(len(generation_detail["generations"])):
                    return generation_detail["generations"][i]["img"]
        if int(details["processing"]) > 0:
            total = int(details["finished"])
            + int(details["processing"])
            + int(details["queue_position"])
            + int(details["restarted"])
            + int(details["waiting"])
            return f'Processing image: {details["processing"]}/{total}'
        return f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s'


@bot.command()
async def ping(ctx):
    await ctx.send('pong')


@bot.event
async def on_ready():
    await tree.sync()
    await bot.tree.sync()
    print('We have logged in as {0.user}'.format(bot))


@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    for rule in json_data["message"]:
        rule_type = rule["type"]
        content = rule["content"]
        response = rule["response"]
        
        # 根据规则类型动态调用对应的判断函数
        if check_functions.get(rule_type, lambda c, m: False)(content, message):
            # 如果规则指定了函数,则调用对应的函数
            if "function" in rule:
                function_name = rule["function"]
                function = globals()[function_name]
                result = await function(message)
                await message.channel.send(result)
            # 否则发送预定义的响应消息
            elif "response" in rule:
                await message.channel.send(response)

    # 确保命令系统正常工作
    await bot.process_commands(message)


async def sendMessageToChannelHelper(data):
    channel = await bot.fetch_channel(os.environ.get("CHANNEL_ID"))
    # 创建一个 embed 对象
    mTitle = "Empty Title"
    if "id" in data:
        mTitle = data["id"]
    if "log_tag" in data:
        mTitle = data["log_tag"]
    mDescription = "Empty Description"
    if "model" in data:
        mDescription = data["model"]
    if "log_message" in data:
        mDescription = data["log_message"]
    mColor = 0x00ff00
    if ("log_tag" in data or "log_message" in data) and (data["log_level"] == "assert" or data["log_level"] == "error"):
        mColor = 0xff0000
    embed = discord.Embed(title=mTitle, description=mDescription, color=mColor)
    # 将 fields 数据加入 embed
    for field in data:
        if field == "img":
            embed.set_image(url=data[field])
        else:
            embed.add_field(name=field, value=data[field], inline=True)
    # 发送 embed 消息
    await channel.send(embed=embed)


def sendMessageToChannel(data):
    bot.loop.create_task(sendMessageToChannelHelper(data))


def run():
    try:
      token = os.environ.get("TOKEN") or ""
      if token == "":
        raise Exception("Please add your token to the Secrets pane.")
      bot.run(token)
    except discord.HTTPException as e:
        if e.status == 429:
            print(
                "The Discord servers denied the connection for making too many requests"
            )
            print(
                "Get help from https://stackoverflow.com/questions/66724687/in-discord-py-how-to-solve-the-error-for-toomanyrequests"
            )
        else:
            raise e


def discord_bot():
    print("Running discord_bot")
    run()