Discord机器人report命令如何将提交结果发送至命令触发原频道
修改方案
你只需要调整指定提交频道的代码行即可,核心逻辑是将硬编码的固定频道替换为触发命令的上下文频道:
- 原代码中固定获取ID为
889700840913371166的频道作为提交目标,直接替换为ctx.channel即可拿到触发report命令的原频道对象。
修改后的完整代码如下:
@bot.command() async def report(ctx): a_list = [] # 替换原固定频道获取逻辑,直接取触发命令的原频道 submit_channel = ctx.channel channel = await ctx.author.create_dm() def check(m): return m.content is not None and m.channel == channel for question in q_list: await asyncio.sleep(1) await channel.send(question) msg = await bot.wait_for('message', check=check) a_list.append(msg.content) submit_wait = True while submit_wait: await channel.send('End of questions - "submit" to finish') msg = await bot.wait_for('message', check=check) if "submit" in msg.content.lower(): submit_wait = False answers = "\n".join(f'{a}. {b}' for a, b in enumerate(a_list, 1)) submit_msg = f'Application from {msg.author} \nThe answers are:\n{answers}' await submit_channel.send(submit_msg)
可选优化
如果要避免报告内容泄露到公共频道,可以在函数开头加权限校验,仅允许管理员使用该命令:
# 加在函数第一行即可 if not ctx.author.guild_permissions.administrator: await ctx.send("你没有权限使用该命令", delete_after=5) return
内容的提问来源于stack exchange,提问作者Less Demons
相关产品推荐
相关产品推荐

