0

Ok so i am making a gen bot for me and my friends and i wanted to know how do i make my bot reply to the message with a account from a .txt file that i put in the folder with the bot please help me thank you

i tried `

import random

@client.command()
async def color(ctx):
    responses = ['red',
                 'blue',
                 'green',
                 'purple',
                 'Add more',]
    await ctx.send(f'Color: {random.choice(responses)}')

` but i didnt want to put it all in one line in the responses

  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 14 '22 at 23:57
  • Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – goose.mp4 Nov 16 '22 at 01:01

2 Answers2

0

You could add something like this to your code:

import random


def readTxtLines(filename):
    with open(filename, "r") as f:
        lines = f.readlines()
    return lines


def getRandomLine(lines):
    return random.choice(lines).strip()


print(getRandomLine(readTxtLines("test.txt")))
Temba
  • 370
  • 2
  • 14
0

Here is a method in cog form:

import random

@commands.command()
async def color(self,ctx):
    filename1=open('cogs/color.txt','r')
    wordList1=[line.rstrip('\n') for line in filename1]
    filename1.close()
    out1 = random.choice(wordList1)
    await ctx.send(f"color: {out1}")