提到作者Python B

2024-05-05 00:01:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在这一行生成标记作者This is your random {} pick,

token = "xxxxxxxxxxxxxxxxxxxxxxxx"
prefix = "?"

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import random 

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

answers = ["apple", "ball", "cat", "dog", "elephant", "frog", "gun"]

@bot.command()  
async def choose(k : int):
    """Chooses between multiple choices."""
    if 0 <= k <= 10:
        await bot.say("This is your random {} pick, {}".format(k, ctx.message.author.mention))
        embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
        await bot.say(embed=embed)
    else:
        await bot.say("Invalid number")

bot.run(token)

在使用命令时出现这样的错误。在

^{pr2}$

Tags: importyourprefixisbotrandomembedawait
1条回答
网友
1楼 · 发布于 2024-05-05 00:01:04

Python identifiers can't begin with a number。您可以做的是将命令的名称指定为除协同程序的名称之外的其他名称。

@bot.command(pass_context=True)  
async def choose(ctx, k: int):
    """Chooses between multiple choices."""
    if 0 <= k <= 10:
        await bot.say("This is your random {} pick, {}".format(k, ctx.message.author.mention))
        embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
        await bot.say(embed=embed)
    else:
        await bot.say("Invalid number")

相关问题 更多 >