【问题标题】:How do I make my discord bot reading dms in Python?如何让我的不和谐机器人在 Python 中读取 dms?
【发布时间】:2021-06-15 22:17:44
【问题描述】:

我有一个不和谐的机器人,它工作正常,我只有一个问题:如果机器人由于某些命令向某人发送 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...

【问题讨论】:

    标签: python discord bots dm


    【解决方案1】:

    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
    

    参考资料:

    【讨论】:

    • 感谢您的快速回答,我也使用过on_message,但不知道这会有所不同。但是,当我使用第一个代码段时,机器人只会回复 DM,因为我正在使用这些 @bot.command() 东西来定义命令。我该如何解决?
    • 第一部分是让bot只响应dms,我在第二部分已经解释了,该命令可以在任何地方使用并且会dm用户并等待响应,不知道是什么你的疑问是
    • 好吧,我想我没有正确解释。所以我希望机器人回复 DM。它还有一些其他命令可以在服务器中使用。但是我想添加一个只有我可以使用的命令,并且使用该命令,机器人应该向服务器上的所有人发送消息并询问他们的生日(生日机器人)。 (这很好用。)但现在的问题是,机器人无法阅读他们的回复。当我复制您的第一个代码段(实际上没有第二个代码段时它实际上可以工作,因为现在我使用了message.guild),机器人将回复 DM,而服务器命令将不起作用。跨度>
    • 我猜肯定有一些else-statement?
    • 是的,但是你说你使用bot.command,第一段没有使用bot_command,它只是回复dm消息,使用else语句,你可以让它响应guild消息,另请阅读this
    【解决方案2】:

    好的,所以根据您的问题,您希望收到 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")
    
            
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2021-10-12
      • 1970-01-01
      • 2019-04-11
      • 2021-07-07
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多