L
L
LxneSamurai2022-03-25 11:36:57
Python
LxneSamurai, 2022-03-25 11:36:57

Discord.py How to make a "reaction" with and without mentioning the user?

Good day.
I'm working on a discord bot. I decided to take up reactions/actions with GIFs (example in the screenshot)
img src=" https://habrastorage.org/webt/62/3d/7d/623d7da9e1e... " alt="image"/>

I decided to add the ability to interact with users, but leave the possibility to
use the "action" without mentioning any user.
I ran into a problem that works either without writing {member.mention} and member : discord.Member (i.e.,
the "action" will be performed as in the screenshot without mentioning the user), or with the above, but
then it will no longer be possible to perform " action" alone.

Wrote reactions through embeds.

Here is the code itself:

#гифки для "действий"
smoke_gifs = ['https://c.tenor.com/GbIdim1VGIcAAAAC/cowboy-bebop.gif', 'https://c.tenor.com/4AlXS_jcSfEAAAAC/cigarette-smoke.gif',
'https://c.tenor.com/e6fnyOkyodMAAAAC/nana-anime.gif', 'https://c.tenor.com/dNPpY-4GkAQAAAAC/smoking-cowboy-bebop.gif']

@bot.command()
async def smoke(ctx, member: discord.Member):
   #emb - "действие" без участия и упоминания пользователя
    emb = discord.Embed(title="Реакция: курить",description=f"{ctx.author.mention} курит.",timestamp=ctx.message.created_at, colour=discord.Color.darker_grey())
    emb.set_image(url=(random.choice(smoke_gifs)))
    emb.set_footer(text="{}".format(ctx.author.name), icon_url=ctx.author.avatar_url)
   
   #emb2 - "действие" совместно с пользователем
    emb2 = discord.Embed(title="Реакция: курить",description=f"{ctx.author.mention} курит вместе с {member.mention}",timestamp=ctx.message.created_at, colour=discord.Color.darker_grey())
    emb2.set_image(url=(random.choice(smoke_gifs)))
    emb2.set_footer(text="{}".format(ctx.author.name), icon_url=ctx.author.avatar_url)
   
    if member.mention in ctx.message:
      await ctx.send(embed=emb2)
    else:
      await ctx.send(embed=emb)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-03-25
@LxneSamurai

import typing

async def smoke(ctx, member: typing.Optional[discord.Member] = None):

And inside the handler you check if member is not None. If completed, then the participant is specified.
Well, do not forget to read the documentation , it describes what constructions in the arguments discord.py understands.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question