1

I'm making a Discord bot in Python and I want to add a feature when I use the command _throw and ping a user, the bot will reply depending on the user ping (It's preferred to be a usual text message, not embed). Currently, I have this code:

if message.content == "_throw":
    user = message.mentions[0]
    await message.channel.send("You threw a hamster to " + f"{user}" + "!")

But my bot doesn't reply to it at all (PyCharm doesn't see any error).

Here's my bot script:

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

    if message.content.startswith('_hi'):
        await message.channel.send(f'Hello, {message.author.mention}!')

    if message.content == "_throw":
        user = message.mentions[0]
        await message.channel.send("You threw hamster to " + f"{user}" + "!")

    if message.content.startswith("_userinfo"):
        user = message.mentions[0]
        emb14 = discord.Embed(
            title=f"@{user} info:",
            colour=discord.Colour.dark_blue()
        )
        emb14.set_image(url=user.avatar_url)
        emb14.add_field(name=f"Name", value=f"{user}", inline=True)
        emb14.add_field(name=f"Discord Joined date", value=f"{user.created_at}", inline=False)
        emb14.add_field(name=f"Server Joined date", value=f"{user.joined_at}", inline=False)
        emb14.add_field(name="Profile Picture", value=":arrow_down: :arrow_down: ", inline=False)
        await message.channel.send(embed=emb14)

client.run('TOKEN')

Any ideas?

Aaron
  • 227
  • 5
  • 10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238509/discussion-between-aaron-and-taku). – Aaron Oct 25 '21 at 14:06

1 Answers1

1

I understand that you want your bot to respond to _throw @user. In this case, when you check message.content == "_throw", it's filtering the messages that's exactly _throw and not the @user part.

To accept a user mention argument, the simplest method would to use a regex match:

import re

# First checks if there are any mentions in the message
# Then uses regex "^_throw\s+<@!?[0-9]{17,22}>$" to match the message content
if message.mentions and re.match(r"^_throw\s+<@!?[0-9]{17,22}>$", message.content):
    user = message.mentions[0]
    await message.channel.send(f"You threw a hamster to {user.mention)!")

This will allow the bot to reply to _throw @user and responds with You threw a hamster to @user!

Taku
  • 31,927
  • 11
  • 74
  • 85
  • Thank you really much! It works! But I have one question: What is `\s+<@!?[0-9]{17,22}>$`? – Aaron Oct 25 '21 at 14:34
  • Its a [regex](https://stackoverflow.com/questions/4736/learning-regular-expressions) and is used to match patterns. The one I wrote means: starts with 1 or more spaces, followed by "<", then optionally followed by "!", followed by 17-22 digits, and ends with ">". Together it'll match Discord's user mention, such as: "<@!357449931555471605>" (which is the actual message stored in @user). – Taku Oct 25 '21 at 14:39
  • Thank you! One last question: How do you make a code, when I mention myself in the `_throw` command, the bot will reply: `Woah, you threw hamster to yourself` or `You can't throw things at urself lol`? – Aaron Oct 25 '21 at 15:19
  • @Aaron You can add an if statement to check `if user == message.author`, and write your desired outputs in the if and else blocks. – Taku Oct 25 '21 at 19:18
  • Thanks for the reply! But when I added your code (`if user == message.author`), the bot replies both with the `_throw` command and `You can't throw things at urself`. It seems that bot firstly replies with `You threw hamster to @Me` and then `You can't throw things at urself`. How to make a bot send only 1 message - You can't throw things at urself, if I mention myself? – Aaron Oct 26 '21 at 10:00
  • @Aaron Hello, as I mentioned before, you'll need an if-else block. In the if part you can have `await message.channel.send(f"You threw a hamster to yourself!")` and the else with `await message.channel.send(f"You threw a hamster to {user.mention)!")` so only one message gets sent. – Taku Oct 26 '21 at 11:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238552/discussion-between-aaron-and-taku). – Aaron Oct 26 '21 at 13:26