简体   繁体   English

如何让我的机器人的 Slash 响应检测 discord.py 中的反应?

[英]How do I make my bot's Slash response detect reactions in discord.py?

I have been using this function before Discord forced everyone to switch to slash commands.在 Discord 强迫大家切换到斜线命令之前,我一直在使用这个 function。 Now, when I move it to slash command format, it doesn't work.现在,当我将它移动到斜线命令格式时,它不起作用。

What used to work, now doesn't.以前的工作,现在不行。 I'm using this inside a cogs file.我在 cogs 文件中使用它。 If that helps, great.如果这有帮助,那就太好了。 If not, sorry.如果没有,对不起。

async def combat_shop(self, ctx):
  member_id = ctx.author.id
  await utils.refresh_active(ctx, member_id, ctx.author.name)
  bancheck = await utils.ban_check(ctx)
  if bancheck is True:
    await ctx.send("<:BanHammer_:957349442573467708>┃You've been banned from using Daddy Bot. Take this to the support server: <https://discord.gg/SbMyaxWFr9> \nRemember: Being rude/commanding ***will not*** do you any good. Be kind and respectful.")
    return
  await utils.leveling_system(ctx, 5)
  dec = "buy: `daddy buy >id<`\ndismantle: `daddy dismantle >id<`\n\n`r-combat`┃Random Combat Item┃<:Dollar:952432145270452244> 250k"
  page1CS = embed = discord.Embed(title = "**The Weapon Store<:D_Axe:998668619561046077>**", description = f"{dec}", color = discord.Colour.teal())
  embed.add_field(name="Basic Weapons", value="`long_sword`┃<:D_LongSword:1001951918706397235> **Long Sword**┃<:D_WeaponParts:998668591174000690> 10\n`short_sword`┃<:D_ShortSword:998668579207663616>**Short Sword**┃<:D_WeaponParts:998668591174000690> 20\n`halberd`┃<:D_Halberd:1001951880215277738>**Halberd**┃<:D_WeaponParts:998668591174000690>50\n`axe`┃<:D_Axe:998668619561046077>**Axe**┃<:D_WeaponParts:998668591174000690> 35\n`gun`┃<:D_Gun:1002626237618995351>**Gun**┃<:D_WeaponParts:998668591174000690> 20\n`star_blaster`┃<:D_StarBlaster:1001951963828715641>**Star Blaster**┃<:D_WeaponParts:998668591174000690> 150", inline=True)
  embed.add_field(name="Patreon Weapons", value="`lightning_spear`┃<:D_LightningSpear:998668565408403526>**Lightning Spear**┃<:D_WeaponParts:998668591174000690> 50\n`potato_bomb`┃<:D_PotatoBomb:998668568755445830>**Potato Bomb**┃<:D_WeaponParts:998668591174000690> 100\n`shadow_dagger`┃<:D_ShadowDagger:998668575747358810>**Shadow Dagger**┃<:D_WeaponParts:998668591174000690> 50", inline=True)
  embed.set_footer(text="page 1/5")
  
  page2CS = embed = discord.Embed(title = "**The Armor Store<:D_ShadowHelmet:998668576930152508>**", description = f"{dec}", color = discord.Colour.teal())
  embed.add_field(name="Basic Armor Sets", value="`speedy_helmet`┃<:D_SpeedyHelmet:998668582777008238>**Speedy Helmet**┃<:D_ArmorParts:998668617866559519> 15\n`speedy_armor`┃<:D_SpeedyArmor:998668581917184090>**Speedy Armor**┃<:D_ArmorParts:998668617866559519> 20\n`turtle_shell_helmet`┃<:D_TurtleShellHelmet:998668588107968522>**Turtle Helmet**┃<:D_ArmorParts:998668617866559519>25\n`turtle_shell_armor`┃<:D_TurtleShellArmor:998668586749010081>**Turtle Armor**┃<:D_ArmorParts:998668617866559519> 30\n`leather_helmet`┃<:D_LeatherHelmet:998668559632842873>**Leather Helmet**┃<:D_ArmorParts:998668617866559519> 20\n`leather_armor`┃<:D_LeatherArmor:998668558299041812>**Leather Armor**┃<:D_ArmorParts:998668617866559519> 25", inline=True)
  embed.add_field(name="Patreon Armor Sets", value="`shadow_helmet`┃<:D_ShadowHelmet:998668576930152508>**Shadow Helmet**┃<:D_ArmorParts:998668617866559519> 50\n`shadow_armor`┃<:D_ShadowArmor:998668574631661608>**Shadow Armor**┃<:D_ArmorParts:998668617866559519> 60\n`potato_helmet`┃<:D_PotatoHelmet:998668571683078225>**Potato Helmet**┃<:D_ArmorParts:998668617866559519> 150\n`potato_armor`┃<:D_PotatoArmor:998668567673323590>**Potato Armor**┃<:D_ArmorParts:998668617866559519> 200\n`zeus_helmet`┃<:D_ZeusHelmet:998668593363423322>**Zeus Helmet**┃<:D_ArmorParts:998668617866559519> 50\n`zeus_armor`┃<:D_ZeusArmor:998668592126115973>**Zeus Armor**┃<:D_ArmorParts:998668617866559519> 60", inline=True)
  embed.set_footer(text="page 2/5")

  page3CS = embed = discord.Embed(title = "**The Secondary Store<:D_HealingPotion:998668556856197291>**", description = f"{dec}", color = discord.Colour.teal())
  embed.add_field(name="Basic Secondary", value="`strength_staff`┃<:D_StrengthStaff:998668583959810088>**Strength Staff**┃<:D_WeaponParts:998668591174000690> 50\n`defense_staff`┃<:D_DefenseStaff:998668620823531531>**Defense Staff**┃<:D_WeaponParts:998668591174000690> 50\n`shield`┃<:D_Shield:998668578091970580>**Shield**┃<:D_WeaponParts:998668591174000690> 50", inline=True)
  embed.add_field(name="Patreon Secondary", value="`walking_cane`┃<:D_WalkingCane:998668589747945562>**Walking Cane**┃<:D_WeaponParts:998668591174000690> 75\n`potato_buddy`┃<:D_PotatoBuddy:998668569946620045>**Potato Buddy**┃<:D_WeaponParts:998668591174000690> 150\n`thunder_rod`┃<:D_ThunderRod:998668564196241418>**Thunder Rod**┃<:D_WeaponParts:998668591174000690> 75", inline=True)
  embed.set_footer(text="page 3/5")
  
  page4CS = embed = discord.Embed(title = "**The Special Store<:D_PotatoParty:998668573004284008>**", description = f"{dec}", color = discord.Colour.teal())
  embed.add_field(name="Basic Specials", value="`earthquake`┃<:D_Earthquake:998668623512084560>**Earthquake**┃<:Dollar:952432145270452244> 1m\n`life_steal`┃<:D_LifeSteal:998668560706572458>**Life Steal**┃<:Dollar:952432145270452244> 1m", inline=True)
  embed.add_field(name="Patreon Specials", value="`nightmare`┃<:D_Nightmare:998668566146592795>**Nightmare**┃<:Dollar:952432145270452244> 1m\n`potato_party`┃<:D_PotatoParty:998668573004284008>**Potato Party**┃<:Dollar:952432145270452244> 1m\n`thunderstorm`┃<:D_ThunderStorm:998675226743275530>**Thunderstorm**┃<:Dollar:952432145270452244> 1m\n`healing_orb`┃<:D_HealingOrb:998668625097543750>**Healing Orb**┃<:Dollar:952432145270452244> 1m", inline=True)
  embed.set_footer(text="page 4/5")

  page5CS = embed = discord.Embed(title = "**The Items Store<:D_HealingPotion:998668556856197291>**", description = f"{dec}", color = discord.Colour.teal())
  embed.add_field(name="Items", value="`healing_potion`┃<:D_HealingPotion:998668556856197291>**Healing Potion**┃<:Dollar:952432145270452244> 50k\n`bomb`┃<:D_Bomb:1001952068837330984>**Bomb**┃<:Dollar:952432145270452244> 50k\n`damage_upgrade`┃<:D_DamageUpgradeStone:998668585251651735>**Damage Upgrade**┃<:Dollar:952432145270452244> 250k\n`defense_upgrade`┃<:D_DefenseUpgradeStone:998668621951795281>**Defense Upgrade**┃<:Dollar:952432145270452244> 250k\n`speed_upgrade`┃<:D_SpeedUpgradeStone:998668580902150144>**Speed Upgrade**┃<:Dollar:952432145270452244> 250k")
  embed.set_footer(text="page 5/5")

  test_pages_shop = [page1CS, page2CS, page3CS, page4CS, page5CS]

  buttons_shop = ["◀", "▶"]
  current_shop = 0
  msg = await ctx.send(embed=test_pages_shop[current_shop])

  for button in buttons_shop:
    await msg.add_reaction(button)
    
  while True:
    try:
      reaction, user = await self.bot.wait_for("reaction_add", check=lambda reaction, user: user == ctx.author and reaction.message == msg and reaction.emoji in buttons_shop, timeout=25)
      
    except asyncio.TimeoutError:
      await msg.clear_reactions()
      break
      return
    else:
      previous_pages = current_shop

      if reaction.emoji == "◀":
        if current_shop > 0:
          current_shop -= 1
      
      elif reaction.emoji == "▶":
        if current_shop < len(test_pages_shop) - 1:
          current_shop += 1
          
      for button in buttons_shop:
        await msg.remove_reaction(button, ctx.author)
  
      if current_shop != previous_pages:
        await msg.edit(embed=test_pages_shop[current_shop])

Keep in mind that I removed the @cog_ext.cog_slash part because that's not the issue.请记住,我删除了 @cog_ext.cog_slash 部分,因为这不是问题。 I've been doing testing beforehand and I found that it gets stuck on the @cog_ext.cog_slash part.我事先一直在进行测试,发现它卡在了@cog_ext.cog_slash部分。

To convert this command, you'll need these changes to the function definition要转换此命令,您需要对 function 定义进行这些更改

@app_commands.command(name="combat_shop", description="The Combat shop")
async def combat_shop(self, interaction : discord.Interaction):

You'll also need to adapt your code to deal with a discord.Interaction instead of a discord.ext.commands.Context .您还需要调整代码以处理discord.Interaction而不是discord.ext.commands.Context That includes switching:这包括切换:

  • ctx.send to interaction.response.send_message ctx.sendinteraction.response.send_message
  • Switching the msg.edit to one of the interaction edits (eg interaction.edit_original_response )msg.edit切换到交互编辑之一(例如interaction.edit_original_response
  • ctx.author to interaction.user ctx.authorinteraction.user

I found out the issue after another hour of testing.经过一个小时的测试,我发现了这个问题。 In slash commands, it has to be reaction.message.id == msg.id for that check to work.在斜杠命令中,它必须是reaction.message.id == msg.id才能使该检查起作用。

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

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