从预定的set discord.py中选择随机数

发布于 2025-02-10 06:53:18 字数 1710 浏览 5 评论 0原文

我有一个功能,每隔几个小时就可以将随机用户贴上一个或否问题。但是,我想要它,因此,当用户响应是/否时,仅当ping ID的消息ID =时,该机器人才会回答。

我遇到的问题是找到一种使Python从整数列表中选择的方法。我可以为ping做到这一点:

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

但是问题是,sagess.author.id仅适用于整数,这意味着它们不能用引号。

随机ping的代码是:

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))

终端告诉我值是真实的,所以我可以说此代码不是问题。然后在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)

我需要user_id_for_bot是一个int,但是我不知道如何列出该程序可以选择的每确定ints列表。听起来很简单,但是我真的很难在Google上找到任何东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国粹 2025-02-17 06:53:18

random.choice 是您要在此处使用的内容,以下是文档:

random.choice(seq)

从非空序列seq返回一个随机元素。如果Seq是
空,提高indexError。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文