I have a help command in my pycord bot that displays a list of commands. 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. 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. 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. This will run when the class is initialized for the first time. From there you can add the URL button type to your class by using 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())