【发布时间】: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