如何在 Pycord 的视图子类中添加 url 按钮?
How do I add a url button to a subclass of view in Pycord?
我的 pycord 机器人中有一个显示命令列表的帮助命令。这是要显示的嵌入:
helpEmbed = discord.Embed(title='BlazingBot Help', description='Hi, I\'m a bot made by BlazingLeaf#3982, but I can\'t do much yet because I\'m still under development')
这是视图的子类(当用户单击命令按钮时,它会切换到不同的嵌入和视图):
class helpView(View):
@discord.ui.button(label='Commands', style=discord.ButtonStyle.green)
async def command_callback(self, button, interaction):
await interaction.response.edit_message(embed=commandEmbed, view=commandView())
然后我有斜杠命令:
@bot.slash_command(name='help', description='Show a list of commands and get a link to the support server!', guild_ids=[861826763570151424])
async def help(ctx):
await ctx.interaction.response.send_message(embed=helpEmbed, view=helpView())
这很好用,但我还想添加一个 URL 按钮,该按钮会指向我的机器人的支持服务器。我检查了 api,它提到你不能用 @discord.ui.button 装饰器创建一个 URL 按钮,你应该手动创建一个按钮。所以我在斜杠命令之前和视图的子类之后添加了这段代码:
supportServerButton = Button(label='Support Server', style=discord.ButtonStyle.gray, url='https://discord.com')
helpView().add_item(supportServerButton)
但是,我得到这个错误:
loop = asyncio.get_running_loop()
RuntimeError: no running event loop
我该如何解决这个问题?
将 __init__
添加到您的 class。这将在 class 首次初始化时 运行。从那里您可以使用 self
.
将 URL 按钮类型添加到您的 class
class helpView(View):
def __init__(self):
super().__init__(timeout=None)
supportServerButton = discord.ui.Button(label='Support Server', style=discord.ButtonStyle.gray, url='https://discord.com')
self.add_item(supportServerButton)
@discord.ui.button(label='Commands', style=discord.ButtonStyle.green)
async def command_callback(self, button, interaction):
await interaction.response.edit_message(embed=commandEmbed, view=commandView())
我的 pycord 机器人中有一个显示命令列表的帮助命令。这是要显示的嵌入:
helpEmbed = discord.Embed(title='BlazingBot Help', description='Hi, I\'m a bot made by BlazingLeaf#3982, but I can\'t do much yet because I\'m still under development')
这是视图的子类(当用户单击命令按钮时,它会切换到不同的嵌入和视图):
class helpView(View):
@discord.ui.button(label='Commands', style=discord.ButtonStyle.green)
async def command_callback(self, button, interaction):
await interaction.response.edit_message(embed=commandEmbed, view=commandView())
然后我有斜杠命令:
@bot.slash_command(name='help', description='Show a list of commands and get a link to the support server!', guild_ids=[861826763570151424])
async def help(ctx):
await ctx.interaction.response.send_message(embed=helpEmbed, view=helpView())
这很好用,但我还想添加一个 URL 按钮,该按钮会指向我的机器人的支持服务器。我检查了 api,它提到你不能用 @discord.ui.button 装饰器创建一个 URL 按钮,你应该手动创建一个按钮。所以我在斜杠命令之前和视图的子类之后添加了这段代码:
supportServerButton = Button(label='Support Server', style=discord.ButtonStyle.gray, url='https://discord.com')
helpView().add_item(supportServerButton)
但是,我得到这个错误:
loop = asyncio.get_running_loop()
RuntimeError: no running event loop
我该如何解决这个问题?
将 __init__
添加到您的 class。这将在 class 首次初始化时 运行。从那里您可以使用 self
.
class helpView(View):
def __init__(self):
super().__init__(timeout=None)
supportServerButton = discord.ui.Button(label='Support Server', style=discord.ButtonStyle.gray, url='https://discord.com')
self.add_item(supportServerButton)
@discord.ui.button(label='Commands', style=discord.ButtonStyle.green)
async def command_callback(self, button, interaction):
await interaction.response.edit_message(embed=commandEmbed, view=commandView())