简体   繁体   English

如何让我的 Discord.py 机器人从与我的主代码相同的文件夹中的 .txt 文件中随机选择一条“行”?

[英]How do I make my Discord.py bot pick a random "line" from a .txt file in the same folder as my main code?

I am making an overall fun bot and I want it to pick a random colour which has been written in a .txt file.我正在制作一个整体有趣的机器人,我希望它选择一种已写入 .txt 文件的随机颜色。 Please help either on this platform or on Discord.请在此平台或 Discord 上提供帮助。 My Discord Username is: RelaxedNerd#3072.我的 Discord 用户名是:RelaxedNerd#3072。

It would be easier to just set them as a variable and import random such as this将它们设置为变量并导入这样的随机数会更容易

import random

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM