pythondiscorddiscord.pyuserid

How do I mention a member in a text? Discord bot python


if message.content.startswith('!givecookie @member'):
   await message.channel.send('Hello, @member, here is your cookie~ :cookie:')

So how can I add a member(@member) to the command when mentioning someone? What the command is supposed to do is - give cookie to a member.


Solution

  • First of all, you shouldn’t be using if message.content.startswith(“!givecookie @member") It would be better to declare client using client = commands.Bot(command_prefix="!”). After that you can use

    @client.command()
    async def givecookie(ctx, member : commands.MemberConverter):
      await ctx.send(f"Hello, {member.mention}, here is your cookie~ :cookie:")
    

    This means that your bot now has the prefix “!”, and using @client.command can make more commands that you can use in discord. The parameters passed in this command (ctx, member : commands.MemberConverter) mean the context and the specific member you want to give the cookie to respectively. ctx.send(f“Hello, {member.mention}, here is your cookie~ :cookie:") will basically send the message but it will also ping the member. Telling the bot to send “@member” will make it literally send "@member”. Also, the command will only work when you start a message with “!givecookie @member”, but not if you do “!give cookie {pinging that specific member}”.

    In conclusion, you should use client.command() to use commands instead of checking the message’s content.

    API Reference

    https://discordpy.readthedocs.io/en/stable/index.html

    Prefixes: https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=commands#prefix-helpers

    Commands: https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=commands#commands