如何使用 python 通过我的 Discord 机器人发送嵌入?

How can I send an embed via my Discord bot, with python?

我一直在使用一个新的 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 中很重要,用于将代码块组合在一起。如果我们以您的 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)