简体   繁体   中英

wait_for() from discord.py don't work in dm

This code works in server channels, but does not work in private messages of users. Is there an alternative feature for private messages?

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$thumb'):
        channel = message.channel
        await channel.send('Send me that 👍 reaction, mate')

        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == '👍'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=10.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('👎')
        else:
            await channel.send('👍')

I found some problems similar to mine and rewrote the code, but it didn't help:

discord.py wait_for('reaction_add') functioning differently with direct messages

Discord.py Bot Reactions in DMs

import discord

intents = discord.Intents.default()
intents.members = True

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$thumb'):
        
        msg = await message.channel.send(f'Hi {message.author.mention}')
        await msg.add_reaction('✅')

        def check(reaction, user):
            print(user.id, message.author.id)
            return reaction.message.id == msg.id and user.id == message.author.id and str(reaction.emoji) == '✅'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=30.0, check=check)
        except asyncio.TimeoutError:
            pass
        else:
            await message.channel.send('success')

client.run(token)

只是制作一个intents变量不会让你的机器人使用它。

client = discord.Client(intents=intents)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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