Discord机器人工单系统无法在新频道发送消息求助
问题描述
我为自己的服务器开发Discord机器人,添加带反应监听器的工单系统时,能成功创建新工单频道,但无法在该频道发送消息,且无任何报错提示。尝试用asyncio的Queue功能也没解决问题,请求帮忙排查原因(这是我的第一个项目,我是编程新手)。相关代码如下:
import asyncio def ticketsys(): from main import bot from hikari import GuildReactionAddEvent, Permissions, Embed from config import TICKET_MESSAGE, TICKET_CHANNEL, TICKET_CATEGORY, DEFAULT_GUILD, STAFF_ROLE_ID # This function reacts to the ReactionAdd class and deletes it right away. # It then creates a channel and stores it in the returned value for further use in the permission overwrites @bot.listen() async def ticket_handler(event: GuildReactionAddEvent) -> None: if event.message_id == TICKET_MESSAGE: await bot.rest.delete_reaction(message=TICKET_MESSAGE, channel=TICKET_CHANNEL, user=event.user_id, emoji="📩") ticketch = await bot.rest.create_guild_text_channel(name=f"{event.member.display_name}\'s Ticket", category=TICKET_CATEGORY, guild=DEFAULT_GUILD) queue1 = asyncio.Queue() ticket_channel = ticketch.id await queue1.put(ticket_channel) ticketch_CLIENTALLOW_perms = (Permissions.SEND_MESSAGES | Permissions.VIEW_CHANNEL) ticketch_STAFF_ALLOW_perms = (Permissions.MANAGE_MESSAGES | Permissions.VIEW_CHANNEL | Permissions.MANAGE_CHANNELS | Permissions.MENTION_ROLES) await bot.rest.edit_permission_overwrite(channel=await queue1.get(), target=event.user_id, target_type=1, allow=ticketch_CLIENTALLOW_perms) await bot.rest.edit_permission_overwrite(channel=await queue1.get(), target=STAFF_ROLE_ID, target_type=0, allow=ticketch_STAFF_ALLOW_perms) await bot.rest.edit_permission_overwrite(channel=await queue1.get(), target=1136311522184921300, target_type=0, deny=Permissions.VIEW_CHANNEL) print(await bot.cache.get_guild_channel(channel=await queue1.get())) embed = Embed(title="Your ticket has been created.", description="Please be patient as we try our best to get to your case. \n" "Be aware, that the staff also has to sleep and keep their life together." "If they haven't responded in at least three hours, feel free to ping them.", color="#14f541") await bot.rest.create_message(channel=await queue1.get(), content=f"<@{event.user_id}> \n{embed}", user_mentions=True) await bot.rest.create_message(channel=await queue1.get(), content=f"<@{STAFF_ROLE_ID}>", role_mentions=False) await bot.rest.create_message(channel=await queue1.get(), content=f"Test", role_mentions=False)
问题分析与修复
- Queue使用逻辑错误:你仅向
queue1中存入了1次工单频道ID,但后续多次调用await queue1.get(),队列取空后,后续的get()会进入无限等待状态,直接导致发消息的代码无法执行。完全不需要用Queue传递频道ID,直接使用创建好的频道对象或其ID变量即可。 - Embed参数传递错误:调用
create_message时,Embed对象不能直接拼接在content字符串中,需要单独通过embeds参数传入。 - 代码分支逻辑错误:创建Embed和发送消息的代码块不在
if event.message_id == TICKET_MESSAGE:的分支内,这意味着即使不是触发工单的反应,这部分代码也会执行,会引发不必要的错误。
修正后的代码:
import asyncio def ticketsys(): from main import bot from hikari import GuildReactionAddEvent, Permissions, Embed from config import TICKET_MESSAGE, TICKET_CHANNEL, TICKET_CATEGORY, DEFAULT_GUILD, STAFF_ROLE_ID @bot.listen() async def ticket_handler(event: GuildReactionAddEvent) -> None: if event.message_id == TICKET_MESSAGE: await bot.rest.delete_reaction(message=TICKET_MESSAGE, channel=TICKET_CHANNEL, user=event.user_id, emoji="📩") # 创建工单频道 ticketch = await bot.rest.create_guild_text_channel( name=f"{event.member.display_name}'s Ticket", category=TICKET_CATEGORY, guild=DEFAULT_GUILD ) ticket_channel_id = ticketch.id # 定义权限 client_perms = Permissions.SEND_MESSAGES | Permissions.VIEW_CHANNEL staff_perms = (Permissions.MANAGE_MESSAGES | Permissions.VIEW_CHANNEL | Permissions.MANAGE_CHANNELS | Permissions.MENTION_ROLES) # 设置权限覆盖,直接使用频道ID变量 await bot.rest.edit_permission_overwrite( channel=ticket_channel_id, target=event.user_id, target_type=1, allow=client_perms ) await bot.rest.edit_permission_overwrite( channel=ticket_channel_id, target=STAFF_ROLE_ID, target_type=0, allow=staff_perms ) await bot.rest.edit_permission_overwrite( channel=ticket_channel_id, target=1136311522184921300, target_type=0, deny=Permissions.VIEW_CHANNEL ) # 打印频道信息验证 print(await bot.cache.get_guild_channel(channel=ticket_channel_id)) # 创建Embed并发送消息 embed = Embed( title="你的工单已创建", description="请耐心等待我们处理你的问题。\n请注意,工作人员也需要休息和处理生活事务。如果三小时内未得到回复,可以@工作人员提醒。", color="#14f541" ) # 发送用户提醒消息,正确传入embed参数 await bot.rest.create_message( channel=ticket_channel_id, content=f"<@{event.user_id}>", embeds=[embed], user_mentions=True ) # 发送工作人员提醒,角色@需要用<@&角色ID>格式 await bot.rest.create_message( channel=ticket_channel_id, content=f"<@&{STAFF_ROLE_ID}>", role_mentions=True ) # 测试消息 await bot.rest.create_message( channel=ticket_channel_id, content="Test" )
内容的提问来源于stack exchange,提问作者AJ for the Sky
相关产品推荐
相关产品推荐

