Nextcord开发问题:按钮点击后新按钮不显示且回调参数缺失报错
问题原因
- Helpb类的按钮回调参数错误:nextcord按钮回调的固定参数顺序为
self、button、interaction,你额外新增了不存在的ctx参数,导致框架传参时参数数量不匹配,触发报错。 - Sbdrugs函数定义错误:该函数不是类方法,多余了
self参数,且你在按钮回调中直接将函数名作为消息内容发送,没有调用函数生成Embed对象。 - 缺少
nextcord基础导入:代码中直接使用nextcord.Embed、nextcord.ui.Button等类,但没有对应的导入语句。 - Sbdrugs函数参数设计不合理:原参数依赖命令上下文
ctx,但按钮回调中无法直接获取ctx,可直接用interaction对象获取用户信息。
修正后完整代码
import nextcord from nextcord.ext import commands def Sbdrugs(interaction): embed = nextcord.Embed(title="Drugs", description="Drugs play an important role in Slotbot. So make sure to remember what they do.") embed.set_author(name = interaction.user.display_name, icon_url = interaction.user.avatar.url) embed.add_field(name = "Weed", value = "Weed can be smoked to restart cooldowns of almost everything including drink. (*Doesnt include drugs.*) Remember, weed is the currency of the black market.", inline = False) embed.add_field(name = "Drink", value = "Drinking beers basically restarts every cooldown except drugs.", inline = False) embed.add_field(name = "Steroids", value = "Dosing steroids makes you immune against the ~hex command and gives you access to the ~beatup command. Steroids even decrease the chance of getting scammed from 30% to 10%. It shortens the feed cooldown too.", inline = False) embed.add_field(name = "Opioids", value = "Make you immune against gun shots, makes you immune against beatup too and resets all cooldowns (except drugs) including smoke and drink.", inline = False) embed.add_field(name = "Anesthesia **AKA** Anes", value = "Stuns yourself but also makes others not possible to interact with your slotbot stats (bal, goose etc they cant check anything from you).", inline = False) embed.add_field(name = "LSD", value = "Shortens every cooldown to 15 secs but makes it very hard to do other cooldowns like bal or farm for example. LSD is mostly used for pill farming, constantly shooting people and for some other things but i currently cant remember.", inline = False) return embed def Helpmeembed(self, ctx): embed = nextcord.Embed(title = "You called me...?", description= "What do you need help with?") #still working on this class SbDrugs(nextcord.ui.View): def __init__(self): super().__init__() self.value = None @nextcord.ui.button(label = "1", style = nextcord.ButtonStyle.blurple) async def helpb(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): # 调用函数生成Embed后传入send方法 await interaction.response.send_message(embed=Sbdrugs(interaction), ephemeral=False) self.value = True self.stop() class Helpb(nextcord.ui.View): def __init__(self): super().__init__() self.value = None # 去掉多余的ctx参数 @nextcord.ui.button(label = "1", style = nextcord.ButtonStyle.blurple) async def helpb(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): view = Sbdrugs() await interaction.response.send_message("What do you need help with on Slotbot?\n**1. Drugs**", ephemeral=False, view = view) self.value = True self.stop() class Help(commands.Cog, name="Help"): def __init__(self, bot: commands.Bot): self.bot = bot @commands.command() async def helpme(self, ctx): view = Helpb() await ctx.send("Hey, I'm here, what do you need help with?\n\n**Slotbot**\n*Press 1 for more information.*", view = view) await view.wait() if view.value is None: await ctx.send("Ay, where you at bruh") def setup(bot: commands.Bot): bot.add_cog(Help(bot))
额外注意
- nextcord 2.x版本中用户头像属性已改为
avatar.url,原avatar_url已废弃,若你使用旧版本可以改回avatar_url。 - 按钮回调中不需要自行传入ctx,所有上下文信息都可以从
interaction对象中获取。
内容的提问来源于stack exchange,提问作者Prime
相关产品推荐
相关产品推荐

