Welcome to OGeek Q&A 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 Reply

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('??')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...