嗨,当我尝试在第 26 行运行我的票务机器人时,我遇到了 Flatten 错误,不知道如何修复它,任何帮助将不胜感激
机器人运行,但不会在频道中发布消息或删除旧消息,就像之前那样
这个文件还有其他文件,其中一个是通道 id 所在的配置
import discord
import json
import data
# load config
with open("support.json", encoding="utf-8") as f:
SUPPORT_CONFIG = json.load(f)
async def initialize(client: discord.Client):
global support_request_channel
global support_category
global reaction_msg
support_request_channel = client.get_channel(
int(SUPPORT_CONFIG["SUPPORT_REQUEST_CHANNEL"]))
support_category = client.get_channel(
int(SUPPORT_CONFIG["SUPPORT_CATEGORY"]))
if support_category is None or support_request_channel is None:
print(
"Unable to find support request channel or category. Please check your config.")
exit()
# find reaction message
msgs = await support_request_channel.history(limit=200, oldest_first=True).flatten()
client_user_msgs = [m for m in msgs if m.author.id ==
client.user.id and len(m.embeds) == 1]
if len(client_user_msgs) > 0:
for m in msgs:
await m.delete()
# build the support embed
embed = discord.Embed()
embed.title = "Request Support"
embed.description = "Please react with appropriate emoji to start a support ticket.\n\n"
embed.color = discord.Color.blue()
# add roles to embed from the config
for i in SUPPORT_CONFIG["SUPPORT_DATA"]:
line = "{}: **{}**\n".format(i["emoji"], i["type"])
embed.description += line
reaction_msg = await support_request_channel.send(embed=embed)
# add reactions
for i in SUPPORT_CONFIG["SUPPORT_DATA"]:
await reaction_msg.add_reaction(i["emoji"])
async def handle_reaction_add(reaction: discord.Reaction, user: discord.User, client: discord.Client):
# ignore self reactions
if user.id == client.user.id or reaction.message.id != reaction_msg.id:
return
await reaction.remove(user)
# find proper roles
reaction_info = next(
(i for i in SUPPORT_CONFIG["SUPPORT_DATA"] if i["emoji"] == reaction.emoji), None)
if reaction_info is None:
return
# create permission overwrites for roles
allowed = discord.PermissionOverwrite(view_channel=True)
disallowed = discord.PermissionOverwrite(view_channel=False)
overwrites = {
support_category.guild.default_role: disallowed,
user: allowed
}
# for welcome support channel welcome msg
role_pings = []
for role_id in reaction_info["roles"]:
role = support_category.guild.get_role(int(role_id))
if role is None:
print("Role with ID {} not found. Please check your config.".format(role_id))
continue
overwrites[role] = allowed
role_pings.append("<@&{}>".format(role.id))
role_pings = ",".join(role_pings)
sc_name = "support-{}".format(user.name)
support_channel = await support_category.create_text_channel(name=sc_name, overwrites=overwrites)
# send message to support channel
support_welcome_text = reaction_info["welcome_message"].format(
user.id, role_pings)
embed = discord.Embed()
embed.description = "**{}**".format(support_welcome_text)
embed.color = discord.Color.blue()
await support_channel.send(embed=embed)
# save in db
data.save_support_channel(
support_channel.id, user.id, reaction_info["type"])
```
discord.py 中的 API 似乎发生了变化,并且 flatten 方法在
support_request_channel.history(...)
返回的异步生成器上不再可用。相反,使用此代码将结果放入列表中:
msgs = [msg async for msg in support_request_channel.history(limit=200, oldest_first=True)]
API 更改已记录在此处。