Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

discord.py - Discord reaction_add doesn't work in Direct Message channel

I've been troubleshooting this thing for days and this is the last straw.

Reaction_add only works if I do !on_message in the server 'general' channel, if I directly message the bot, it doesn't do anything. Though, funny thing is, if I message bot directly first and the enter !on_message in server general channel, bot reacts in general channel and in direct messages.

After TimeoutError I get thumbs down in direct messages.

This is the code, straight from discord.py documentation and I'm using Bot as Client:

@bot.command(pass_context=True)
async def on_message(ctx):
    channel = ctx.channel
    await ctx.send('Send me that ?? reaction, mate')

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

    try:
        reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
    except asyncio.TimeoutError:
        await channel.send('??')
    else:
        await channel.send('??')

It's funny that wait_for(message, ...) message works just fine everywhere.

Tried adding this and still no luck.

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

bot = commands.Bot(command_prefix=PREFIX, description=DESCRIPTION, intents=intents)
question from:https://stackoverflow.com/questions/65927848/discord-reaction-add-doesnt-work-in-direct-message-channel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's because on_message is a bot event, which means it does not need to be invoked as a command, you can call the command with (), but in your case, you are trying to use the command as a event.

This is an event

@bot.event
async def on_message(message):
    channel = message.channel
    await message.channel.send('Send me that ?? reaction, mate')

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

    try:
        reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
    except asyncio.TimeoutError:
        await message.channel.send('??')
    else:
        await message.channel.send('??')

This is a command, a command needs to be invoked with a function, in this case, it would be your prefix + thumbs !thumbs hope both of these help.

@bot.command()
async def thumbs(ctx):
    channel = ctx.channel
    await ctx.send('Send me that ?? reaction, mate')

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

    try:
        reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
    except asyncio.TimeoutError:
        await ctx.send('??')
    else:
        await ctx.send('??')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...