-1

I have a function that pings a random user with a yes or no question every few hours. However, I want it so when the user responds yes/no, the bot will only answer if their message id = that of the ping ID.

The issue I'm running into is finding a way to get python to select from a set list of integers. I could just do this for the ping:

USER_ID = ["11111111111", "222222222222", "3333333333333"]

But the issue is, message.author.id ONLY works for integers, meaning they CAN'T be in quotes.

The code for the randomized ping is:

newvalue = False
@bot.command(pass_context=True)
async def randum(ctx):
    while True:
      global USER_ID_FOR_BOT
      USER_ID_FOR_BOT = random.choice(USER_ID)
      RESPONSES = ["<@" + USER_ID_FOR_BOT + ">" + " Do you want to hear a joke?", "(Other none yes/no questions)"]
      possibleresponse = random.choice(RESPONSES)
      if possibleresponse == "<@" + USER_ID_FOR_BOT + ">" + " Do you want to hear a joke?":
        global newvalue
        newvalue = True
        print ('Value is TRUE')
        await ctx.channel.send(possibleresponse)
        await asyncio.sleep(random.randint(minTime, maxTime))

The terminal tells me the value is True, so I can say this code isn't the issue. And then in the on_message....

if newvalue == True:
        if 'no' in message.content:
          if message.channel.id == 988993107368476692:
            if message.author.id == USER_ID_FOR_BOT:
                await message.channel.send(random.choice(denyresponse))
                newvalue = False
                return
        if 'yes' in message.content:
          if message.channel.id == 988993107368476692:
            if message.author.id == USER_ID_FOR_BOT:
                await message.channel.send(random.choice(jokeresponse))
                newvalue = False
                return
    await bot.process_commands(message)

I need USER_ID_FOR_BOT to be an int, but I have no clue how to make a list of per-determined ints that the program can choose from. It sounds simple, but I genuinely am having difficulty finding anything on google.

Ver
  • 3
  • 4
  • I believe `random.choice` is what you want. – Thornily Jun 24 '22 at 05:34
  • Does this answer your question? [How can I randomly select an item from a list?](https://stackoverflow.com/questions/306400/how-can-i-randomly-select-an-item-from-a-list) – TheFungusAmongUs Jun 24 '22 at 18:25
  • @TheFungusAmongUs Sadly, it does not. I need to be able to use random.choice - or a variation of - with numbers NOT in quotes so message.author.id works later. – Ver Jun 24 '22 at 18:42
  • Can you not make a list of `int`s? `random.choice` works with a `list` of any type. – TheFungusAmongUs Jun 24 '22 at 18:44
  • @TheFungusAmongUs When I define USER_ID as [11111111, 22222222, 33333333] and then call it in USER_ID_FOR_BOT = random.choice(USER_ID), I get the error: Command raised an exception: TypeError: must be str, not int – Ver Jun 24 '22 at 18:58
  • @Ver Cannot reproduce that. `random.choice` works with a list of any kind. See [the docs](https://docs.python.org/3/library/random.html#random.choice) – TheFungusAmongUs Jun 24 '22 at 19:04
  • It seems the issue I'm having stems from the code `RESPONSES = ["<@" + USER_ID_FOR_BOT + ">" + " Do you want to hear a joke?", "(Other none yes/no questions)"]` that's when I get the error Command raised an exception: TypeError: must be str, not int. Can I not combine text and int in a value? – Ver Jun 24 '22 at 19:27
  • That would be a separate issue, not the one addressed by this question. The question asked how to "Choose random number from pre-determined set", and `random.choice` solves that. If you are getting errors outside of this, please attempt to solve it on your own or ask a new question following [the guidelines outlined here](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and [here](/help/how-to-ask). – TheFungusAmongUs Jun 24 '22 at 19:37

1 Answers1

0

random.choice is what you want to use here, below is from the documentation:

random.choice(seq)

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

Thornily
  • 533
  • 3
  • 15
  • When I put in USER_ID_FOR_BOT = random.choice(1111111, 2222222, 333333) I get: Command raised an exception: TypeError: choice() takes 2 positional arguments but 4 were given. – Ver Jun 24 '22 at 08:19
  • @Ver you pass in the list. Not the numbers – Thornily Jun 24 '22 at 20:52