简体   繁体   English

如何将 url 按钮添加到 Pycord 中的视图子类?

[英]How do I add a url button to a subclass of view in Pycord?

I have a help command in my pycord bot that displays a list of commands.我的 pycord 机器人中有一个显示命令列表的帮助命令。 This is the embed to display:这是要显示的嵌入:

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')

This is the subclass of view (When the user clicks the Commands button, it switches to a different embed and view):这是视图的子类(当用户单击命令按钮时,它会切换到不同的嵌入和视图):

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())

Then I have the slash command:然后我有斜杠命令:

@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())  

And this works fine, but I also want to add a URL button that leads to the support server for my bot.这很好用,但我还想添加一个 URL 按钮,该按钮可通往我的机器人的支持服务器。 I checked the api and it mentions that you can't create a URL button with the @discord.ui.button decorator, and that you should create a button manually.我检查了 api,它提到你不能用 @discord.ui.button 装饰器创建一个 URL 按钮,你应该手动创建一个按钮。 So I added this code before the slash command and after the subclass of view:所以我在斜杠命令之前和视图的子类之后添加了这段代码:

supportServerButton = Button(label='Support Server', style=discord.ButtonStyle.gray, url='https://discord.com')
helpView().add_item(supportServerButton)

However, I get this error:但是,我收到此错误:

loop = asyncio.get_running_loop()
RuntimeError: no running event loop

How can I fix this?我怎样才能解决这个问题?

Add a __init__ to your class.__init__添加到您的 class。 This will run when the class is initialized for the first time.这将在第一次初始化 class 时运行。 From there you can add the URL button type to your class by using self .从那里您可以使用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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM