【问题标题】:Sending embeds through discord.py通过 discord.py 发送嵌入
【发布时间】:2020-05-31 09:20:40
【问题描述】:

我一直在做很多工作,通过 javascript 和 discord.js 使用 discord api 创建不和谐机器人。

我正在尝试使用通过 discord.py 的 discord api 和通过 requests.py 的请求使用 python 创建我的第一个 discord 机器人。

我的目标是检查网站上的状态代码,当发送包含“状态代码”的消息时,它将在嵌入中回复该站点的状态代码。

这是我的代码:

import discord
import requests
r = requests.get('redactedurl')
test = r.status_code
class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message):
        if (message.channel.id == redacted):
                if "status code" in message.content:
                    print('Message from {0.author}: {0.content}'.format(message))
                    
                    embed = discord.Embed(color=0x00ff00)
                    embed.title = "test" 
                    embed.description = '**Status Code:** {r.status_code}'
                    await message.channel.send(embed=embed)


client = MyClient()
client.run('redacted')

以下是我希望任何人都可以回答以帮助我的问题列表:)

  1. 正如您在此处看到的:https://gyazo.com/f6ae7082486cade72389534a05655fec,这只是在嵌入中发送“{r.status_code}”,而不是实际的状态代码,我做错了什么?

  2. 当我在大括号中看到 0 时,这是什么意思。例如,有人可以向我解释“('以 {0} 身份登录!'.format(self.user))”吗?由于我是 python 和 discord.py 的新手,所以我对整行感到困惑。我知道结果是什么,但请原谅我的无知,这一切都是必要的吗?

  3. 在“send(embed=embed)”中,为什么不能直接放send(embed)?

  4. 最后,我还能做些什么来改进代码吗?

如果您能提供帮助,非常感谢!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    1. 在您设置嵌入描述的行中,将r.status_code 输出为字符串而不是它包含的值。试试embed.description = '**Status Code:** {0}'.format(r.status_code)

    2. 0 类似于应该存在的值的索引。例如,'{1}'.format(10, 20) 将打印出索引 1 处的值,在本例中为 20。

    3. 当您使用send(embed) 时,机器人最终会以字符串形式发送嵌入,这看起来很不正常,如果您尝试发送它,您会明白我的意思。在这种情况下,我们必须指定我们将值分配给哪个参数。此函数接受kwargs,它们是关键字参数,在这种情况下,embed 是此send() 函数中的kwargs 之一。这个函数也接受其他的kwargs,比如content, tts, delete_after, etc.,都记录在案了。

    4. 您可以通过传入 kwargs 来简化创建嵌入,例如:discord.Embed(title='whatever title', color='whatever color') 如果您查看文档,discord.Embed() 可以支持更多参数。

    这里是文档的链接:https://discordpy.readthedocs.io/en/latest/index.html 如果您搜索TextChannel,并查找send() 函数,您会发现更多支持的参数以及discord.Embed()

    【讨论】:

      【解决方案2】:

      好的,整理你的问题列表:

      1. 您没有格式化 {r.status_code},只是将其作为字符串发送,这就是它显示为这样的原因。要修复它,您只需添加 1 个“f”即可。
      embed.description = f'**Status Code:** {r.status_code}'
      

      或者,如果您想使用一致的方式格式化字符串:

      embed.description = '**Status Code:** {0}'.format(r.status_code)
      
      1. 在 Python 中,字符串中的大括号“{}”可用于格式化。这可以通过多种方式完成,包括您在代码中的操作方式。如果要格式化字符串中的不同值,str.format() 可以接受多个参数。 0 只是您要使用的参数的索引。
      2. 我对 discord 库不太熟悉,但是快速查看文档后,似乎如果您这样做,它将获取您的 embed 变量并将其作为 content 变量传递给发送。从而将其作为普通消息发送而不是嵌入。
      3. 我个人更喜欢 f-strings 用于格式化,因为它使您的代码更易于阅读。我无法评论您对 discord 库的使用,但除此之外,您的代码看起来还不错!纯粹为了美观/可读性,我会在您的导入和定义变量之间以及您的变量和类之间放置一个空行。

      【讨论】:

        【解决方案3】:

        这是一个例子!

        import discord
        
        client = commands.Bot(command_prefix = '~')
        
        @client.command()
        async def embed(ctx):
           embed=discord.Embed(title="Hello!",description="Im a embed text!")
        await ctx.send(embed=embed)
        
        client.run("TOKEN")
        

        【讨论】:

          猜你喜欢
          • 2021-02-05
          • 2021-02-13
          • 2021-07-02
          • 1970-01-01
          • 1970-01-01
          • 2023-03-09
          • 1970-01-01
          • 2021-10-20
          • 1970-01-01
          相关资源
          最近更新 更多