如何使用 python 通过我的 Discord bot 发送嵌入?

时间:2021-01-04 06:50:42

标签: python syntax discord bots

我一直在开发一个新的 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

1 个答案:

答案 0 :(得分:0)

缩进在 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)
相关问题