简体   繁体   English

如何让我的 discord 机器人读取 Python 中的 dms?

[英]How do I make my discord bot reading dms in Python?

I have got a discord bot and it works fine, I've just got one problem: If the bot sends a DM to someone due to some command, I want it to receive the answer to that DM.我有一个 discord 机器人,它工作正常,但我遇到了一个问题:如果机器人由于某些命令向某人发送 DM,我希望它接收到该 DM 的答案。 I don't get it working for DMs.我不明白它对 DM 有用。 I've tried some stuff I've found on the internet but nothing was even close to working.我尝试了一些我在互联网上找到的东西,但没有任何东西接近工作。 Any help would be much appreciated:) Here's what I've tried (I'm sorry I can't give you that much)任何帮助将不胜感激:)这是我尝试过的(对不起,我不能给你那么多)

@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 is not a discord event, we just use on_message and check if it is a dm. on_private_message不是 discord 事件,我们只是使用on_message并检查它是否是 dm。

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

However, I see that your problem is accepting an answer from a user in private message, this can be done with wait_for .但是,我看到您的问题是在私人消息中接受用户的回答,这可以通过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

References:参考:

Ok, so as per your question it says that, you want to receive the answer of dms, in order to receive answer, you might have asked a question, so I will explain using an example, so suppose you run a command !question , the bot will dm you or whoever runs the command, a question whose answer needs to be checked.好的,所以根据你的问题,它说,你想收到 dms 的答案,为了得到答案,你可能已经问了一个问题,所以我会用一个例子来解释,所以假设你运行一个命令!question ,机器人会告诉你或运行该命令的人,这是一个需要检查其答案的问题。 For that I would recommend the use of bot.wait_for() :-为此,我建议使用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")

        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM