You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 09:18:03