如何在discord.py中使用客户端(在选择回调中)获取一个频道对象?

huangapple go评论90阅读模式
英文:

How to get a channel object using the client (in select callback) discord.py

问题

我在选择回调中遇到了获取频道对象(使用客户端)的问题。我不想使用公会ID进行获取,因为一个用户可能在DM频道中(此命令必须适用于一个用户在普通频道中,另一个用户在文本频道中)。

@discord.ui.button(label="确认", custom_id='confirm', style=discord.ButtonStyle.green)
async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):

    initialize_trade(self.user.id, interaction.user.id, self.user_channel, interaction.channel.id, self.user_message, self.own_message)

    inventory = get_inventory(interaction.user.id)
    user1_inv, user2_inv = [], []

    formatted_inventory = []
    count_dict = {}
    for item in inventory:
        count_dict[item] = count_dict.get(item, 0) + 1
        formatted_inventory.append(f"{item} ({count_dict[item]})" if count_dict[item] > 1 else item)

    await interaction.response.edit_message(content='交易已确认!', view=TradeView(self.user, 2, formatted_inventory, user1_inv, user2_inv))
class TradeView(discord.ui.View):
    def __init__(self, other_user: discord.User, usertype, inventory, user1inv, user2inv):
        super().__init__(timeout=60)

        self.other_user = other_user
        self.inventory = inventory
        self.user1inv = user1inv
        self.user2inv = user2inv

        self.usertype = usertype

        self.select_callback.options = [discord.SelectOption(label=item) for item in inventory]

        self.select_callback.max_values = len(inventory)

    @discord.ui.select(placeholder="选择要交易的物品", min_values=1, options=[])
    async def select_callback(self, interaction: discord.Interaction, select):
        if self.usertype == 1:
            configure_user1_inv(interaction.user.id, self.other_user.id, select.values)
            channel_id, msg_id = get_user2_message(interaction.user.id, self.other_user.id)
        else:
            configure_user2_inv(interaction.user.id, self.other_user.id, select.values)
            channel_id, msg_id = get_user1_message(self.other_user.id, interaction.user.id)

        channel = self.bot.get_channel(channel_id)
        message = await channel.fetch_message(msg_id)

        await message.edit(content=f'编辑的消息!{select.values}')
        await interaction.response.edit_message(content=f"{select.values}")
英文:

I have an issue getting a channel object (using the client) within a select callback. I don't want to fetch using a guild id because one user will be in a DM channel (this command will have to work for one user in a normal channel, and the other in a text channel).

    @discord.ui.button(label="Confirm", custom_id='confirm', style=discord.ButtonStyle.green)
    async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):

        initialize_trade(self.user.id, interaction.user.id, self.user_channel, interaction.channel.id, self.user_message, self.own_message)

        inventory = get_inventory(interaction.user.id)
        user1_inv, user2_inv = [], []

        formatted_inventory = []
        count_dict = {}
        for item in inventory:
            count_dict[item] = count_dict.get(item, 0) + 1
            formatted_inventory.append(f"{item} ({count_dict[item]})" if count_dict[item] > 1 else item)

        await interaction.response.edit_message(content='Trade confirmed!', view=TradeView(self.user, 2, formatted_inventory, user1_inv, user2_inv))
class TradeView(discord.ui.View):
    def __init__(self, other_user: discord.User, usertype, inventory, user1inv, user2inv):
        super().__init__(timeout=60)


        self.other_user = other_user
        self.inventory = inventory
        self.user1inv = user1inv
        self.user2inv = user2inv

        self.usertype = usertype

        self.select_callback.options = [discord.SelectOption(label=item) for item in inventory]

        self.select_callback.max_values = len(inventory)


    @discord.ui.select(placeholder="Select Items to Trade", min_values=1, options=[])
    async def select_callback(self, interaction: discord.Interaction, select):
        if self.usertype == 1:
            configure_user1_inv(interaction.user.id, self.other_user.id, select.values)
            channel_id, msg_id = get_user2_message(interaction.user.id, self.other_user.id)
        else:
            configure_user2_inv(interaction.user.id, self.other_user.id, select.values)
            channel_id, msg_id = get_user1_message(self.other_user.id, interaction.user.id)



        channel = self.bot.get_channel(channel_id)
        message = await channel.fetch_message(msg_id)

        await message.edit(content=f'EDITED MESSAGE! {select.values}')
        await interaction.response.edit_message(content=f"{select.values}")

Anything helps :)! Sorry if this is a little confusing/not clear.

答案1

得分: 2

你可以简单地将机器人传递给__init__方法的另一个参数中:

class TradeView(discord.ui.View):
    def __init__(self, other_user: discord.User, usertype, inventory, user1inv, user2inv, bot):
        super().__init__(timeout=60)

        self.bot = bot

        self.other_user = other_user
        self.inventory = inventory
        self.user1inv = user1inv
        self.user2inv = user2inv

        self.usertype = usertype

        self.select_callback.options = [discord.SelectOption(label=item) for item in inventory]

        self.select_callback.max_values = len(inventory)

    # ...其余的代码

然后在创建视图时简单地传递机器人实例:

    @discord.ui.button(label="确认", custom_id='confirm', style=discord.ButtonStyle.green)
    async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):

        initialize_trade(self.user.id, interaction.user.id, self.user_channel, interaction.channel.id, self.user_message, self.own_message)

        inventory = get_inventory(interaction.user.id)
        user1_inv, user2_inv = [], []

        formatted_inventory = []
        count_dict = {}
        for item in inventory:
            count_dict[item] = count_dict.get(item, 0) + 1
            formatted_inventory.append(f"{item} ({count_dict[item]})" if count_dict[item] > 1 else item)

        await interaction.response.edit_message(
            content='交易确认!', 
            view=TradeView(
                self.user, 2, formatted_inventory, user1_inv, user2_inv, interaction.client  # 重要
            )
        )
英文:

You can simply pass the bot into another argument of the __init__ method:

class TradeView(discord.ui.View):
    def __init__(self, other_user: discord.User, usertype, inventory, user1inv, user2inv, bot):
        super().__init__(timeout=60)

        self.bot = bot

        self.other_user = other_user
        self.inventory = inventory
        self.user1inv = user1inv
        self.user2inv = user2inv

        self.usertype = usertype

        self.select_callback.options = [discord.SelectOption(label=item) for item in inventory]

        self.select_callback.max_values = len(inventory)

    # ...rest of the code

And simply pass the bot instance when creating the view:

    @discord.ui.button(label="Confirm", custom_id='confirm', style=discord.ButtonStyle.green)
    async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):

        initialize_trade(self.user.id, interaction.user.id, self.user_channel, interaction.channel.id, self.user_message, self.own_message)

        inventory = get_inventory(interaction.user.id)
        user1_inv, user2_inv = [], []

        formatted_inventory = []
        count_dict = {}
        for item in inventory:
            count_dict[item] = count_dict.get(item, 0) + 1
            formatted_inventory.append(f"{item} ({count_dict[item]})" if count_dict[item] > 1 else item)

        await interaction.response.edit_message(
            content='Trade confirmed!', 
            view=TradeView(
                self.user, 2, formatted_inventory, user1_inv, user2_inv, interaction.client  # important
            )
        )

huangapple
  • 本文由 发表于 2023年6月22日 04:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定