Discord.py - Using tasks.loop() to send an item from a list every 24 hrs

I'm trying to create a bot that will call a bingo number each day until all 50 numbers have been called. Here's what I have done.

from discord.ext import tasks
from discord.ext.commands import bot
from discord import DMChannel
import os
import random
import asyncio

client = discord.Client()
target_channel_id = [PLACEHOLDER]

rolls = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)
roll_list = list(rolls)
random.shuffle(roll_list)
x = 0

@client.event
async def on_ready():
    print('{0.user} is online.'.format(client))

@tasks.loop(hours=24)
async def called_once_a_day():
    message_channel = bot.get_channel(target_channel_id)
    print(f"Got channel {message_channel}")
    await message_channel.send("THE NUMBER FOR TODAY IS:")
    await message_channel.send(roll_list[x])

I understand that I cannot run this properly because x is defined outside of the @tasks.loop function. However, I also cannot define x within the function, as it would simply reset to the default 0 value each day.

Any ideas on how to solve this?

Thank you.


Solution 1:

You can use list.remove() to remove the number once called and random.choice() to randomly select the number for that day.

First you define the bingo numbers list outside the function.:

rolls_list = list(range(1, 76))  # range() does not include the last number

And then inside the function, we choose a random number, then remove it from that list.

In this example, I'm try-catching IndexError to determine if the list is empty, however your implementation is completely up to you. You can use an if-statement to check if the list is empty if you want--choose whatever you think is right for your needs.

@tasks.loop(hours=24)
async def called_once_a_day():
    channel = bot.get_channel(CHANNEL_ID)
    print(f"Got channel {channel.mention}")

    try:
        number_today = random.choice(rolls_list)
        rolls_list.remove(number_today)
        await channel.send(f"THE NUMBER FOR TODAY IS: {number_today}")

    except IndexError:  # raised by `random.choice()` if list is empty

        # -> code whatever you want here when it has finished drawing all the numbers