我如何在 nextcord 中创建 link 按钮?

How can i create link button in nextcord?

这是我想要的 link 按钮。

这是我的代码:

class helpcmd(nextcord.ui.View):
    def __init__(self):
        super().__init__()
    
    @nextcord.ui.button(label='create thread', style=nextcord.ButtonStyle.link ,url='https://github.com/lmjaedentai/KaiCheng-Bot#commands')
    async def help(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        #my code

错误:

  File "d:\Desktop\coding\discordpy\main.py", line 226, in helpcmd
    @nextcord.ui.button(label='create thread', style=nextcord.ButtonStyle.link ,url='https://github.com/lmjaedentai/KaiCheng-Bot#commands')
TypeError: button() got an unexpected keyword argument 'url'

你不能,因为 Discord 不 return,也不支持 URL 按钮的回调。相反,您可以在 def __init__ 中执行并在 self.add_item 中执行。您可以在文档中查看更多信息。

class SomeView(View):
    def __init__(self):
        super().__init__() #you can do timeout here
        self.add_item(url = "some url", label = "This is a url button") #using this method doesn't have any callback.

实现相同目标的正确方法是使用类似

的方法
class MyView(ui.View):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.add_item(ui.Button(style=nextcord.ButtonStyle.link, url=<YOUR-URL>, label="Click Me"))

Lim 最初的回答是错误的。

作为一个完整的 class 你应该这样做。

from nextcord.ui import View
from nextcord import Interaction,Button,ButtonStyle

class helpcmd(View):
  def __init__(self):
    super().__init__()
    self.add_item(nextcord.ui.Button(style=nextcord.ButtonStyle.link, url='https://www.youtube.com/watch?v=dQw4w9WgXcQ', label="Create Thread"))
    self.value = None
    @nextcord.ui.button(label='Create a thread', style=nextcord.ButtonStyle.blurple)
    async def rickroll(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
      await interaction.response.send_message('got rickrolled', ephemeral=False)
      self.value = True
      self.stop()