Discord.py开发机器人时embed字段超链接无法生效如何解决
解决方法
Discord embed的文本区域(含field的value、embed描述等)支持标准Markdown超链接语法,格式为[显示文本](链接地址),替换原有纯链接即可生成可点击的超链接。
单字段修改示例
你需要修改的add_field代码调整后如下:
embed.add_field(name='Invite Talent Builder to your own server:', value='[点击邀请](https://discord.com/api/oauth2/authorize?client_id=859135646061166613&permissions=68608&scope=bot)', inline=False)
额外代码修正
你现有代码存在逻辑错误:.help的消息判断逻辑写在了on_ready事件中,该事件仅在机器人启动时触发一次,不存在message参数,会直接运行报错。需要将消息响应逻辑迁移到on_message事件中,完整修正代码如下:
import discord from discord.ext import commands import os from keep_alive import keep_alive client = discord.Client() from discord.utils import find @client.event async def on_ready(): print ('Logged in as {0.user}'.format(client)) await client.change_presence(activity=discord.Game(name=f'.help | On {len(client.guilds)} servers')) @client.event async def on_message(message): # 过滤机器人自身发送的消息,避免循环触发 if message.author == client.user: return if message.content.lower().startswith('.help'): embed = discord.Embed(title='To use Talent Builder bot you need to type the name of the commander with a period at the beggining. Example: .richard | To view equipment guide for commanders type .equipment or .eq for short (Please DO NOT use special characters) (Case does not matter)') embed.set_author(name='Talent Builder', icon_url='https://cdn.discordapp.com/attachments/821829884967649291/860267265404305473/TB7.2t.png') embed.add_field(name='Invite Talent Builder to your own server:', value='[点击邀请](https://discord.com/api/oauth2/authorize?client_id=859135646061166613&permissions=68608&scope=bot)', inline=False) embed.add_field(name='Join our discord support server:', value='[点击加入](https://discord.gg/tyDuddtMWe)', inline=False) await message.channel.send(embed=embed)
内容的提问来源于stack exchange,提问作者Dolphin
相关产品推荐
相关产品推荐

