如何使用同一命令discord.py处理多个用户

2024-04-30 17:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的discord服务器中实现一个报告系统,因此当主持人不在线时,用户仍然可以向bot发送报告,bot将通知整个主持人频道

当我作为单个用户进行测试时,下面的代码工作正常;然而,我预测这不会那么容易。当用户执行以下操作时!report命令,另一个不同的用户向bot发送消息,bot会将其视为一个“会话”

请参见下面的屏幕截图

我希望机器人在自己的“会话”中处理所有用户。如果有人能为我指出正确的方向,我将不胜感激

为了进一步澄清,第一张图片是用户A,第二张图片是用户B,最后一张图片是最终报告。如您所见,用户A执行该命令,但如果用户B也对BOT进行DMs,则它会将该命令视为单个会话

enter image description hereenter image description hereenter image description here

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if isinstance(message.channel, discord.DMChannel):
        if message.content.startswith("!report"):
            await message.channel.send("Who are we reporting?")
            await message.channel.send("Follow this format: \n"
                                       "User: <enter user ID> (to get a user's id please right click their name and Copy ID) \n"
                                       "Reason: <enter reason> (ex include but not limited too: bullying, harassment, "
                                       "offensive post ")

            msg = await client.wait_for('message')
            await message.channel.send("report received, an admin should be in contact with you soon")
            channel = client.get_channel(REPORT_CHANNEL)
            print(msg)
            await channel.send("report received from user: {}#{}, unique User ID {} ".format(msg.channel.recipient.name, msg.channel.recipient.discriminator, msg.channel.recipient.id))
            await channel.send(msg.content)

Tags: 用户命令reportclientsendidmessageif
1条回答
网友
1楼 · 发布于 2024-04-30 17:27:00

这是因为您采用了on_消息事件方法。处理这样的会话的最佳方法是使用命令

q_list = [
"Question 1",
"Question 2",
"Question 3"
]

a_list = []

@client.command()
async def report(ctx):
    a_list = []
    submit_channel = client.get_channel(MODERATOR_CHANNEL_ID)
    channel = await ctx.author.create_dm()

    def check(m):
        return m.content is not None and m.channel == channel

    for question in q_list:
        asyncio.sleep(1)
        await channel.send(question)
        msg = await client.wait_for("message", check = check)
        a_list.append(msg.content)

    submit_wait = True
    while submit_wait:
        await channel.send("End of questions type 'submit' to finish")
        msg = await client.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 {ctx.author} \nThe answers are:\n{answers}"
            await submit_channel.send(submit_msg)

相关问题 更多 >