【问题标题】:How to make a string longer than 1 word in discord.py?如何在 discord.py 中使字符串长于 1 个单词?
【发布时间】:2020-10-12 07:26:02
【问题描述】:

我正在开发一个 discord.py 机器人,它会将您的服务器“推荐”给其他成员。当您输入“!buy [您要支付的价格] [短广告]”时,将出现一个指向您服务器的邀请链接。虽然,当我尝试让短广告长于一个词时,它只显示一个词,即第一个词。

这是我的代码:

@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

如果你能帮忙,谢谢!

【问题讨论】:

    标签: python url-rewriting discord discord.py discord.py-rewrite


    【解决方案1】:

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

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

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

    希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      您可以通过以下方式将它们全部加入一个参数:

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

      【讨论】:

        猜你喜欢
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-10
        • 1970-01-01
        • 2019-10-22
        • 1970-01-01
        • 2014-08-27
        相关资源
        最近更新 更多