【问题标题】:Show input from other command to this command in discord.py在 discord.py 中显示其他命令对该命令的输入
【发布时间】:2020-10-26 19:10:48
【问题描述】:

我正在开发一个为您提供成员的 Discord.py 机器人。以下是它的工作原理:

  1. 您输入 !find - 机器人将发送一个包含 2 或 3 个服务器的嵌入来加入
  2. 加入服务器获取金币
  3. 输入!bal
  4. 机器人回复并嵌入您的余额
  5. 输入!buy [amount] [ad message]
  6. 当人们输入 !find 时,您的服务器会弹出一条广告消息

你是如何做到的,当你输入!buy [amount] [ad message]时,人们在输入!find时就会得到你的服务器?

这是 !buy 命令:

@client.command()
async def buy(ctx, price: int, *args):
    link = await ctx.channel.create_invite(max_age = 0)

    advertisement = ' '.join(args)


    findlink = str(link)

    findmessage = str(advertisement)
    findguild = str(ctx.guild)
    findoutput = findguild + '\n' + findlink + '\n' + findmessage


    print(findoutput)

    buyer = str(ctx.message.author.id)


    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

这是 !find 命令:

@client.command(aliases=['f'])
async def find(ctx):
    global advertisement, findlink, findguild, findmessage

    useravatar = ctx.message.author.avatar

    name = ctx.guild
    link = await ctx.channel.create_invite(max_age = 0)

    author = ctx.message.author

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

    find.set_author(name='Find Servers', icon_url='https://cdn.discordapp.com/attachments/723566996817575948/723932425369026560/comrade.png')
    find.add_field(name='Do you need help? Join our official help server:', value='\u200b', inline=False)
    find.add_field(name="**iComrade [BOT]** (You won't get any coins from joining this server)", value='https://discord.gg/CMD3aaN', inline=False)
    find.add_field(name=f'Hey {ctx.author.name}, join these servers to get **1 coin each**.', value='\u200b', inline=False)
    find.add_field(name=f'**{name}**', value=f'\n{link}', inline=False)

    await ctx.send(embed=find)

【问题讨论】:

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


    【解决方案1】:

    只需制作一个服务器字典,如果服务器一次像 5 个,则调用一堆。

    例子,

    servers = {}
    
    @bot.command()
    async def buy(ctx, amount, *, message):
      server_id = ctx.guild.id
      servers[server_id] = f"{message}"
    
    @bot.command()
    async def find(ctx):
      embed = discord.Embed(title="Servers")
      i = 0
      for server in servers:
        name = bot.get_guild(server).name
        value = servers[server]
        embed.add_field(name=f"{name}", value=f"{value}")
        i += 1
        if i == 5:
          break
      await ctx.send(embed=embed)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 2021-09-03
      • 2011-09-13
      • 2021-03-26
      • 2023-03-12
      • 2021-02-28
      • 2019-05-09
      • 1970-01-01
      相关资源
      最近更新 更多