Search code examples
pythonrandomdiscord.pybots

Discord bot not updating with random numbers


I have been learning how to create a discord bot using Repl.it and I have gotten to a stage where it is mostly functional. The purpose of the bot is generate random character for D&D 5e.However, I am having issues with generating random responses from the bot.

The only way I have managed to make a different output is to restart the bot. The code I am using is below, some things have been omitted for conciseness:

```python
import discord
import os
import random
from random import choice
from keep_alive import keep_alive

client = discord.Client()

def file_to_list(filename):
    thefile = open(filename, "r")
    rows = [line for line in thefile]
    thefile.close()
    for i, r in enumerate(rows):
        rows[i] = r.replace("\n", "")
    return rows

a = file_to_list("Alignment.txt")
al = choice(a)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith("#alignment"):
        await message.channel.send(al)
          
keep_alive()
client.run(os.getenv("TOKEN"))
```

Solution

  • Call the randomisation when the function is called:

    import random
    def your_function:
         random.randint(your things)