如何保存使用discord.py中按钮对应的斜杠命令的discord用户

问题描述 投票:0回答:1

我正在尝试创建一个应用程序斜杠命令,将应用程序发送到 mod 通道,mod 可以在其中接受或拒绝该应用程序。

但目前如果有 2 个用户使用 /apply2 命令,则发送到 Carrier-app-log 通道的按钮都会将角色分配给第二个使用该命令的用户(因为全局变量 id 已设置为新 id)

@client.tree.command(name="apply2", description="Apply for carrier")
@app_commands.describe(player_name="Player Name", profile="Profile Name", floor="Floor", proof="Proof")
async def apply2(interaction: discord.Interaction, player_name: str, profile: str, floor: int, proof: discord.Attachment):
  global id
  global role



  get_player_profile(player_name, "dungeons", profile)
  if check_reqs_dungeon(floor):
    global role
    await interaction.response.send_message("Submitted Succesfully", ephemeral=True)
    channel = discord.utils.get(interaction.guild.channels, name="carrier-app-log")
    embed=discord.Embed(title=player_name.capitalize(), url=f"https://sky.shiiyu.moe/stats/{player_name}/{profile}", description=f"{interaction.user.mention} applied for floor {floor} carrier on account {player_name.capitalize()}", color=368)
    id=interaction.user
    role=floorRoles[floor-1]
    print(role)
    embed.set_image(url=proof.proxy_url)

  
    await channel.send(embed=embed, view=ApplyButtons())
  else:
    await interaction.response.send_message(f"You do not meet the minimum requirements for floor {floor}, the minimum requirement is {floorReqs[floor-1]}", ephemeral=True)
  



class ApplyButtons(discord.ui.View):
  def __init__(self):
      super().__init__(timeout=None)

  @discord.ui.button(label="Accept", style=discord.ButtonStyle.green, custom_id="accept")
  async def accept(self, interaction: discord.Interaction, button: discord.ui.Button):
    global role
    global id
    user = id
    print(user)
    print(role)
    if type(role) is int:
      role=interaction.guild.get_role(role)
      print(role)
      print("Converted")
    if role not in user.roles:
      print("Not in user roles")
      await user.add_roles(role)
      await interaction.response.send_message(f"{role.mention} role given to {user.mention}", ephemeral=False)
    else:
      print("In user roles")
      await interaction.response.send_message(f"Applicant already have {role.mention} role", ephemeral=False)

我尝试将按钮的custom_id设置为原始全局ID,并希望每次按下按钮时都可以访问用户名的按钮的custom_id。

@discord.ui.button(label="Accept", style=discord.ButtonStyle.green, custom_id=id)
user = self.custom_id

但是返回一个属性错误,说 “AttributeError:‘ApplyButtons’对象没有属性‘custom_id’”

谢谢;-;

python discord.py
1个回答
0
投票

不要使用全局变量。特别是对于 Discord 机器人,命令经常由具有不同信息的不同用户运行,使用全局变量是不好的做法。

从命令函数到视图类使用变量的方法是在初始化类时将其作为参数。换句话说,让

__init__
进行论证。

class MyView(discord.ui.View):
    def __init__(self, user): #  <-
        super().__init__()
        self.user = user #  Storing it as an attribute in the class

    @discord.ui.button(label="Accept", style=discord.ButtonStyle.green, custom_id="accept")
    async def accept(self, interaction: discord.Interaction, button: discord.ui.Button):
        user = self.user #  Getting the user argument


@client.tree.command(name="command")
async def command_callback(interaction: discord.Interaction):
     await interaction.response.send_message(...,
                                             view=MyView(interaction.user)) #  Parsing the argument

还有一点需要注意:尽量不要使用内置函数名作为变量名。

id
是一个函数,因此您不应该将其用作变量,因为它会覆盖其用途。这一点尤其重要,因为在您的代码中,
id
是一个全局变量。否则,它只会在本地覆盖
id
函数(在定义它的函数中)。

@discord.ui.button(label="Accept", style=discord.ButtonStyle.green, custom_id=id) user = self.custom_id
也不起作用。

var = None

class MyView(discord.ui.View):
    def __init__(self):
        super().__init__()

    @discord.ui.button(label="Accept", style=discord.ButtonStyle.green, custom_id=var)
    async def accept(self, interaction: discord.Interaction, button: discord.ui.Button):
        var = self.custom_id

self
指的是班级。类中的方法(例如按钮回调)采用我们传统名称为
self
的参数。这是类对象。

如果您想访问 custom_id,您应该从按钮本身而不是类中获取它。

    @discord.ui.button(label="Accept", style=discord.ButtonStyle.green, custom_id="accept")
    async def accept(self, interaction: discord.Interaction, button: discord.ui.Button):
        custom_id = button.custom_id
© www.soinside.com 2019 - 2024. All rights reserved.