Reply Message Discord Bot

Question:
I am trying to figure out if there is a way to make it reply to the same message differently for different people. In other words let’s say my command is !hello, can I have it reply to two people with “Hello!” and two other people “How are you!” both in reply to the SAME command?

You need to add a condition to the message handler where one is sent to some users and another is sent to others.

I cannot answer more specifically, since you did not specify the programming language and the library in which the bot is written.

I am using Python and tbh am using the how to build a discord currency bot videos y’all have as a basis and am just doing some changes to be how I want it… Like with these messages to certain users and such

If you know the user IDs beforehand you can simply hardcode the message handler logic to reply differently.
Unless you meant it to reply

User A hello!
User B How are you?

then you need to do a bit extra, like concatenating strings.
There is more than one python wrapper though, so you need to be more specific.


So this is what I have so far. This is just the basic level of stuff. I am just trying to get it to reply with something different for certain people. And I have their user IDs so how would I go about inputting the code as such?

Tbh I won’t lie I am just learning. So I am using one of the videos made by replit and just doing minor edits as needed. So I may be a bit dumb when it comes to it LMAO

You can do that, your bot needs to differentiate users and have a logic to respond accordingly.

You first identify the user (you can capture the ID of the user) and group the users in a group (like group 1 will get the answer X and group2 will get the answer Y).

@bot.command()
async def hello(ctx):
    user_id = ctx.author.id  # You can use this to id the user
    if user_id in group1: # A simple if
        await ctx.reply("Hello!")
    elif user_id in group2:
        await ctx.reply("How are you!")
    else:
        await ctx.reply("Welcome!")  # And you can make a default response for users not in group1 or group2
3 Likes

@nikolashake Another, possibly simpler, alternative is to just use the random library. If I were to modify your current code:

@bot.event
async def on_message(message):
  if message.author == bot.user:
    return
  
  if message.content.startswith('!hello'):
    await message.channel.send('What do ya want?')

With these changes in mind, this is what it would be:

@bot.event
async def on_message(message):
  if message.author == bot.user:
    return
  
  if message.content.startswith('!hello'):
    await message.channel.send(random.choice(['Hello!','How are you?','Howdy!']))

This will result in the bot responding with “Hello!”, “How are you?”, or “Howdy!” when the user types “!hello”.

4 Likes

You shouldn’t be needing to check whether or not the message is from your own bot, provided you are using Bot

Ok and maybe I am just stupid lmao but how would I identify who belongs to what group?

1 Like

You’d use the ID of the person who initialized the command, you need to use message.author.id.

1 Like

No I know that what I mean is how would I initiate the code for the actual creation of said group
Like what code would be best? Sorry I am a bit new tbh still getting started and learning…

1 Like

in tests for membership, you can simply use a list/set of numerical IDs to test against. You should learn more about python itself.

a = [1 , 2]
if 1 in a: #true
    pass
if 2 in b: #true
    pass
2 Likes

Like @PhoenixStarlight said, by hardcoding what member belong to each group. This is the simplest method if you are not using the bot outside your server.

You just need to create the group:

group1 = [123456789, 987654321]  # You use the user IDs
group2 = [101010101,202020202]
1 Like

Tbh it also can’t hurt to do that check, it won’t really slow anything down.

1 Like

You can also just use the random library, as showcased in my solution.

@bot.event
async def on_message(message):
  if message.author == bot.user:
    return
  
  if message.content.startswith('!hello'):
    await message.channel.send(random.choice(['Hello!','How are you?','Howdy!']))

That’s not really what they wanted to do, They wanted to vary it by groups, not pure randomness.

Lol thank you all for trying to help and it was helpful actually so thx

1 Like