【问题标题】:How can I send an embed via my Discord bot, with python?如何使用 python 通过我的 Discord 机器人发送嵌入?
【发布时间】:2021-04-10 00:15:52
【问题描述】:

我一直在开发一个新的 Discord 机器人。我已经学到了一些东西,现在我想让这些东西更加自定义。

我一直在尝试让机器人发送嵌入,而不是普通消息:

@bot.command()
async def testinfo(ctx, *, arg):
            idtouser = robloxpy.GetName(f'{arg}'.format(arg))
            usergroups = robloxpy.GetUserGroups(f'{arg}'.format(arg))
            userjoin = robloxpy.AccountAgeDays(f'{arg}'.format(arg))
            userbanned = robloxpy.IsBanned(f'{arg}'.format(arg))
            userval = robloxpy.GetUserLimitedValue(f'{arg}'.format(arg))
            onfriends = robloxpy.GetOnlineFriends(f'{arg}'.format(arg))
            frinedsoff = robloxpy.GetOfflineFriends(f'{arg}'.format(arg))

embedVar = discord.Embed(title='user: {idtouser} account stats can be folow below'.format(arg))
embedVar.add_field(name = 'User value', value= "{userval}", inline = True)
embedVar.add_field(name = 'Check if banned', value = "{userbanned}", inline = True)
embedVar.add_field(name = 'User join date (in days)', value = "{userjoin}", inline = True)

embedVar.add_field(name = 'User groups', value = "{usergroups}", inline = True)
embedVar.add_field(name = 'Users friends online', value = "{onfriends}", inline = True)
embedVar.add_field(name = 'Users offline friends', value = "{friendsoff}", inline = True)

embed.set_footer(text=ctx.author.name, icon_url = ctx.author.avatar_url)
await ctx.send(embed=embedVar)

当我尝试运行我的机器人时,出现语法错误:

    await ctx.send(embed=embedVar)
            ^
SyntaxError: invalid syntax

【问题讨论】:

    标签: python syntax discord bots


    【解决方案1】:

    缩进在 Python 中很重要,用于将代码块组合在一起。如果我们以您的testinfo 函数为例,它所包含的只是:

    @bot.command()
    async def testinfo(ctx, *, arg):
                idtouser = robloxpy.GetName(f'{arg}'.format(arg))
                usergroups = robloxpy.GetUserGroups(f'{arg}'.format(arg))
                userjoin = robloxpy.AccountAgeDays(f'{arg}'.format(arg))
                userbanned = robloxpy.IsBanned(f'{arg}'.format(arg))
                userval = robloxpy.GetUserLimitedValue(f'{arg}'.format(arg))
                onfriends = robloxpy.GetOnlineFriends(f'{arg}'.format(arg))
                frinedsoff = robloxpy.GetOfflineFriends(f'{arg}'.format(arg))
    

    这之后的行不被视为testinfo 函数的一部分,因为它们的缩进方式不同。您可以通过缩进其余代码来修复语法错误,如下所示:

    @bot.command()
    async def testinfo(ctx, *, arg):
                idtouser = robloxpy.GetName(f'{arg}'.format(arg))
                usergroups = robloxpy.GetUserGroups(f'{arg}'.format(arg))
                userjoin = robloxpy.AccountAgeDays(f'{arg}'.format(arg))
                userbanned = robloxpy.IsBanned(f'{arg}'.format(arg))
                userval = robloxpy.GetUserLimitedValue(f'{arg}'.format(arg))
                onfriends = robloxpy.GetOnlineFriends(f'{arg}'.format(arg))
                frinedsoff = robloxpy.GetOfflineFriends(f'{arg}'.format(arg))
    
                embedVar = discord.Embed(title='user: {idtouser} account stats can be folow below'.format(arg))
                embedVar.add_field(name = 'User value', value= "{userval}", inline = True)
                embedVar.add_field(name = 'Check if banned', value = "{userbanned}", inline = True)
                embedVar.add_field(name = 'User join date (in days)', value = "{userjoin}", inline = True)
    
                embedVar.add_field(name = 'User groups', value = "{usergroups}", inline = True)
                embedVar.add_field(name = 'Users friends online', value = "{onfriends}", inline = True)
                embedVar.add_field(name = 'Users offline friends', value = "{friendsoff}", inline = True)
    
                embed.set_footer(text=ctx.author.name, icon_url = ctx.author.avatar_url)
                await ctx.send(embed=embedVar)
    

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2021-07-20
      • 2021-07-24
      • 2020-06-29
      • 2018-06-23
      • 2021-04-14
      • 2021-01-12
      • 2020-04-11
      • 2021-06-13
      相关资源
      最近更新 更多