【问题标题】:making the bot resend embeds让机器人重新发送嵌入
【发布时间】:2021-08-08 04:15:57
【问题描述】:

所以这个想法是让机器人将嵌入发送到频道,然后用新的页脚重新发送它
这是我正在测试的代码:

import discord
import os
from discord.ext import commands


intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=',', intents=intents)


@bot.event
async def on_ready():
  global test_channel, bot_command_channel, cu_free_games_channel
  test_channel = bot.get_channel(868816978293452841)
  bot_command_channel = bot.get_channel(808734570283139162)
  cu_free_games_channel = bot.get_channel(873018877020373043)

  print('bot is ready')

@bot.event
async def on_message(message):


  if message.content.startswith('!test'):
    embed_old = discord.Embed(
      title= '''this is the title''', 
      description= '''this is the description''', 
      color= discord.Color.red(),
      # url= '''https://www.google.com/'''
      )
    embed_old.set_footer(text='old footer')
    await bot_command_channel.send(embed=embed_old)


  if message.channel == bot_command_channel:
    if not len(message.embeds):
      return
    else:
      embed_content_in_dict = message.embeds[0].to_dict()
      print(embed_content_in_dict) #prints out the embed's content

      for embed in embed_content_in_dict:
        embed_new = discord.Embed(
          title= embed_content_in_dict["title"],
          type= embed_content_in_dict["type"],
          description= embed_content_in_dict["description"],
          url = embed_content_in_dict["url"], # if the URL in embed_old is commented out, it throws an exception
          )
        embed_new.set_footer(text='new footer')
        await test_channel.send(embed=embed_new)
        return


  

bot.run(os.getenv('TOKEN'))

这是在控制台中打印的内容:

bot is ready

{'footer': {'text': 'old footer'}, 'color': 15158332, 'type': 'rich', 'description': 'this is the description', 'title': 'this is the title'}

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 47, in on_message
    url = embed_content_in_dict["url"], # if the URL in embed_old is commented out, it throws an exception
KeyError: 'url'

那么我如何检测嵌入是否有 url?
以及如何让机器人重新发送原始嵌入及其所有内容,无论它是否有 URL

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    对于您的第一个问题,您可以轻松检查嵌入是否没有 URL:

    只需检查名为“url”的键是否在 embed_content_in_dict 变量中

    if 'url' in embed_content_in_dict.keys():
       #the embed has a url
       #do something
    else:
      #the embed does not have a url
      #do something
    

    第二个问题:

    您可以使用:embed_content_in_dict.get('url') 代替 embed_content_in_dict['url']

    如果该嵌入没有 url,.get('url') 将返回 None

    【讨论】:

    • 我似乎收到此错误 ** raise HTTPException(r, data) discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In embed.url: Scheme "none “不支持。方案必须是 ('http', 'https') 之一。**
    • 哦,那是真的None 不是嵌入的有效 URL。我不知道如何解决这个问题,抱歉
    • 一切都很好,看来您在技术上并没有错,因为该错误不是实际的异常,并且仅在 discord.py 的 1.7.1 而不是 1.6 中触发,我打算使用最新版本所以我还在寻找一个 solotion
    • 哦,祝你好运!
    猜你喜欢
    • 2023-03-30
    • 2018-05-02
    • 2020-07-28
    • 2020-09-15
    • 2021-05-08
    • 2021-07-22
    • 1970-01-01
    • 2021-08-03
    • 2019-09-20
    相关资源
    最近更新 更多