如何让我的不和谐机器人在 Python 中读取 dms?

问题描述

我有一个 discord bot,它运行良好,但我遇到了一个问题:如果 bot 由于某些命令向某人发送 DM,我希望它收到该 DM 的答案。我不明白它对 DM 有用。我尝试了一些我在互联网上找到的东西,但没有任何东西接近工作。任何帮助将非常感激 :) 这是我尝试过的(对不起,我不能给你那么多)

@bot.event
async def on_private_message(ctx):
    if ctx.channel.id == ctx.author.dm_channel.id:
        # here I've tried using ctx.content in several ways but there was no ctx.content...

解决方法

on_private_message 不是不和谐事件,我们只是使用 on_message 并检查它是否是 dm。

@bot.event()
async def on_message(message):
    if message.guild is None:
       #this is a dm message

但是,我发现您的问题是在私人消息中接受用户的回答,这可以通过 wait_for 完成。

@bot.command()
async def something(ctx):
    await ctx.author.send('hello there')
    await ctx.author.send("you have 30 seconds to reply")
    msg = bot.wait_for('message',check = lambda x: x.author == ctx.author and x.channel == ctx.author.dm_channel,timeout=30)
    # do stuff with msg

参考:

,

好的,所以根据你的问题,你想收到 dms 的答案,为了收到答案,你可能已经问了一个问题,所以我会用一个例子来解释,所以假设你运行一个命令!question,机器人会告诉你或任何运行命令的人,这是一个需要检查答案的问题。为此,我建议使用 bot.wait_for():-

bot.command()
async def question(ctx):
    channel = await ctx.author.create_dm()
    def check(m):
        return m.author == ctx.author and m.channel == channel
    try:
        await ctx.author.send("") #your question inside ""
        msg = await bot.wait_for('message',timeout=100.0,check=check)
        message_content = msg.content
        print(message_content) #you can do anything with the content of the message
    except:
        await ctx.author.send("You did not answer in given time")