lundi 4 janvier 2021

Discord bot not updating with random numbers

I have been coding a discord bot using Repl.it and it works almost perfectly. Everytime the user activates the bot it is meant to send them the variable which has been chosen randomly. However, the outputs are always the same. The only time they change is when I completely shut down the program and run it again. I was wondering how and what I would need to do to make sure that everytime a user activates the bot the output is random. This is to go into the code below:

import discord
import os
import random
from random import choice
from keep_alive import keep_alive

client = discord.Client()

stat_block = ["Strength", "Dexterity", "Constitution", "Intelligence", "Wisdom","Charisma"]


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


n = file_to_list("Name.txt")
r = file_to_list("Races.txt")
c = file_to_list("Classes.txt")
pr = file_to_list("Person.txt")
ide = file_to_list("Ideals.txt")
b = file_to_list("Bonds.txt")
f = file_to_list("Flaws.txt")
l = file_to_list("Languages.txt")
a = file_to_list("Alignment.txt")
ar = file_to_list("Armor.txt")
w = file_to_list("Weapons.txt")
rw = file_to_list("Rweapons.txt")
p = file_to_list("Packs.txt")
ba = file_to_list("Background.txt")


def stats():
    x = [0, 0, 0, 0, 0, 0]
    while mean(x) != 12.5:
        x = [0, 0, 0, 0, 0, 0]
        for i in range(6):
            a = random.randint(9, 17)
            x[i] = a
    return x


def mean(x):
    y = sum(x) / 6
    return y


def modifiers(stats):
    z = 0
    y = []
    for i in range(6):
        if stats[i] == 9:
            z = -1
            y.append(z)
        elif stats[i] == 10 or stats[i] == 11:
            z = +0
            y.append(z)
        elif stats[i] == 12 or stats[i] == 13:
            z = +1
            y.append(z)
        elif stats[i] == 14 or stats[i] == 15:
            z = +2
            y.append(z)
        elif stats[i] == 16 or stats[i] == 17:
            z = +3
            y.append(z)
    return y


def hp(classes, modifiers):
    if classes == "Barbarian":
        hp = random.randint(1, 12)
        hp = hp + modifiers[2]
    elif classes == "Bard" or classes == "Cleric" or classes == "Druid" or classes == "Monk" or classes == "Rogue" or classes == "Warlock":
        hp = random.randint(1, 8)
        hp = hp + modifiers[2]
    elif classes == "Fighter" or classes == "Paladin" or classes == "Ranger":
        hp = random.randint(1, 10)
        hp = hp + modifiers[2]
    elif classes == "Sorcerer" or classes == "Wizard":
        hp = random.randint(1, 6)
        hp = hp + modifiers[2]

    return hp





s = stats()
m = modifiers(s)
name = choice(n)
race = choice(r)
classes = choice(c)
person = choice(pr)
idea = choice(ide)
bond = choice(b)
flaw = choice(f)
st1 = choice(stat_block)
st2 = choice(stat_block)
lan = choice(l)
lan2 = choice(l)
al = choice(a)
health = hp(classes, m)
arm = choice(ar)
pack = choice(p)
we = choice(w)
rwe = choice(rw)
bac = choice(ba)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith("#stats"):
        await message.channel.send(s)
        await message.channel.send(m)
        dfhah()
    if message.content.startswith("#alignment"):
        await message.channel.send(al)
        dfhah()
    if message.content.startswith("#person"):
        await message.channel.send(person)
        await message.channel.send(idea)
        await message.channel.send(bond)
        await message.channel.send(flaw)
        dfhah()
    if message.content.startswith("#language"):
        await message.channel.send(lan)
        await message.channel.send(lan2)
        dfhah()
    if message.content.startswith("#backgound"):
        await message.channel.send(bac)
        dfhah()
    if message.content.startswith("#nrc"):
        await message.channel.send(name)
        await message.channel.send(race)
        await message.channel.send(classes)
        dfhah()
    if message.content.startswith("#st"):
        await message.channel.send(st1)
        await message.channel.send(st2)
        dfhah()
    if message.content.startswith("#armour"):
        await message.channel.send(arm)
        dfhah()
    if message.content.startswith("#weapon"):
        await message.channel.send(we)
        await message.channel.send(rwe)
        dfhah()
    if message.content.startswith("#health"):
        await message.channel.send(health)
        dfhah()


keep_alive()
client.run(os.getenv("TOKEN"))

Thanks for helping. :-)




Aucun commentaire:

Enregistrer un commentaire