Discord.py 我想在嵌入中创建一个随机答案

时间:2021-01-15 18:30:36

标签: python python-3.x discord discord.py discord.py-rewrite

所以基本上我只想让我的机器人输入不同的答案,但在嵌入中,这听起来很简单,但真的很令人困惑。我搜索了一下,什么也没找到,所以我只是想我可以在这里找到答案。任何事情都可能有所帮助和赞赏!另外我想提一下,代码在一个齿轮内,运行代码后我得到的错误是:

错误 N.1:未定义变量 'responses' [14,17] 错误 N.2:未定义变量 'responses' [24,62]

import discord
import random
from discord.ext import commands

class ppsize(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command(aliases=['mypp', 'sizepp'])
    async def PPSize(self, ctx):
        pp = discord.Embed(title='PP Size Activated!')
        pp.add_field(name='Measuring your PP... Please wait!', 
        value = responses ['Your PP is 2cm.',
                          'Your PP is 38cm.',
                          'Your PP is 0cm.',
                          'Your PP is too small to be measured.',
                          'Your PP is -16cm.',
                          'Your PP is bigger than a whole house.',
                          'I can not even see your PP mate.',
                          'Your PP is 6cm.',
                          'Your PP is 9cm',
                          'Your PP is 13cm'])
        await ctx.channel.send(embed= pp), (f'{random.choice(responses)}')


def setup(client):
    client.add_cog(ppsize(client))

1 个答案:

答案 0 :(得分:0)

这是一个基本的python问题,

responses = [ # Or values, however you want to name it
    'Your PP is 2cm.',
    'Your PP is 38cm.',
    'Your PP is 0cm.',
    'Your PP is too small to be measured.',
    'Your PP is -16cm.',
    'Your PP is bigger than a whole house.',
    'I can not even see your PP mate.',
    'Your PP is 6cm.',
    'Your PP is 9cm',
    'Your PP is 13cm'
]

# To get a random value
import random

random_resp = random.choice(responses)

我的建议:如果你不知道如何定义变量,你应该在编写 discord bots 之前学习更多的 python

pp = discord.Embed(title="PP Size Activated!", description="Measuring your PP... Please wait")
pp.add_field(name="Your PP size is...", value=random.choice(responses))

await ctx.send(embed=pp)