如何在 discord.py 中使字符串长于 1 个单词?

How to make a string longer than 1 word in discord.py?

我正在开发一个 discord.py 机器人,可以将您的服务器 'reccomends' 发送给其他成员。当您键入“!buy [您要支付的价格] [短广告]”时,将出现 link 到您的服务器的邀请。虽然,当我尝试让短广告长于一个词时,它只显示一个词,第一个词。

这是我的代码:

@client.command()
async def buy(ctx, price: int, advertisement):

    buyer = str(ctx.message.author.id)
    link = await ctx.channel.create_invite(max_age = 0)

    if buyer not in amounts:
        author = ctx.message.author

        noacc = discord.Embed(
            colour = discord.Colour.blue()
        )

        noacc.set_author(name=f'You do not have an account.', icon_url='https://cdn.discordapp.com/attachments/723566996817575948/723932425369026560/comrade.png')
        noacc.add_field(name='Type ":register" to make an account.', value='\u200b', inline=False)

        await ctx.send(embed=noacc)
    elif amounts[buyer] < price:
        author = ctx.message.author

        notaff = discord.Embed(
            colour = discord.Colour.blue()
        )

        notaff.set_author(name=f'You cannot afford this transaction.', icon_url='https://cdn.discordapp.com/attachments/723566996817575948/723932425369026560/comrade.png')
        notaff.add_field(name='Join some more servers, then try again!', value='\u200b', inline=False)

        await ctx.send(embed=notaff)
    elif price < 5:
        author = ctx.message.author

        larger = discord.Embed(
            colour = discord.Colour.blue()
        )

        larger.set_author(name=f'Amount must be 5 or larger.', icon_url='https://cdn.discordapp.com/attachments/723566996817575948/723932425369026560/comrade.png')
        larger.add_field(name='Sorry!', value='\u200b', inline=False)

        await ctx.send(embed=larger)

    else:

        author = ctx.message.author

        buy = discord.Embed(
            colour = discord.Colour.blue()
        )

        buy.set_author(name=f'Purchased {price} Members.', icon_url='https://cdn.discordapp.com/attachments/723566996817575948/723932425369026560/comrade.png')
        buy.add_field(name='This is what people will see when they type ":find"', value='\u200b', inline=False)
        buy.add_field(name=f"**{ctx.guild}**", value=link, inline=False)
        buy.add_field(name=f'{advertisement}', value='\u200b', inline=False)

        await ctx.send(embed=buy)

        amounts[buyer] -= price

如有帮助,谢谢!

Discord.py 用 space 分隔参数。所以你有 2 种可能的解决方案

  1. 当 运行 Discord 中的命令时,用 "" 包围你的广告。 或者
  2. 按如下方式更改参数类型:
@client.command()
async def buy(ctx, price: int, *args):
    advertisement = ' '.join(args)

其余代码保持不变。这将捕获您命令中的所有单词,并将它们加入一个字符串中,从而构成您的广告。

希望这对您有所帮助:)

您可以通过执行以下操作将它们全部合并为一个参数:

@client.command()
async def buy(ctx, price: int, *, args):
    ctx.send(args)