python-3.x 属性错误:模块“discord.ui”没有属性“ActionRow”

a64a0gku  于 2023-04-13  发布在  Python
关注(0)|答案(1)|浏览(116)

我在做什么

我正在为我的discord bot添加一个功能,允许您使用:pushpin:来固定和取消固定消息。当您取消固定消息时,bot会发送一条消息说消息已被取消固定。

我的密码

# Pin feature
@bot.event
async def on_raw_reaction_add(payload):
    if payload.emoji.name == "📌":
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        await message.pin()
    print("Reaction added.")

@bot.event
async def on_raw_reaction_remove(payload):
    if payload.emoji.name == "📌":
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        pins = [reaction for reaction in message.reactions if str(reaction.emoji) == "📌"]
        if len(pins) == 0:
            await message.unpin()
            button = discord.ui.Button(label="View Unpinned Message", style=discord.ButtonStyle.grey, custom_id="view_unpinned_message")
            async def send_message(ctx: discord.Interaction):
                await ctx.channel.send(content=message.content)
            button.callback = send_message
            view_message_action_row = discord.ui.ActionRow(button)
            await channel.send("A previously pinned message has been unpinned.", components=[view_message_action_row])
    print("A reaction has been removed.")

我希望它,以便有一个按钮,是在“钉的消息已被取消钉”的消息,重定向到该消息被取消钉。

错误

如果我删除所有使按钮弹出的代码,则会出现消息。虽然我希望按钮在那里,但由于我使用的是什么,消息根本不会弹出,并给我以下错误:

2023-04-11 00:16:30 ERROR    discord.client Ignoring exception in on_raw_reaction_remove
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "tar.py", line 45, in on_raw_reaction_remove
    view_message_action_row = discord.ui.ActionRow(button)
                              ^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'discord.ui' has no attribute 'ActionRow'

我努力

我尝试的第一件事是更新我的python版本(现在是3.11)和discord.py版本(现在是2.2.2)。同样的问题。
我还发现一个叫做“discord-components”的东西曾经工作过。我试着用pip 3 install discord-components下载它,但它什么也没做。我听说discord.py自己也支持按钮,所以我猜它不会有任何帮助。

完整编码

不知道这是否对任何人都有帮助,但以防万一,这里是机器人的全部代码:

import discord
from discord.ext import commands

# boring

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)

THRESHOLD = 5

@bot.event
async def on_ready():
    channel = bot.get_channel(1069346031281651843)
# Used to make announcements in server, disabled atm (should prob make it more efficient)
#    await channel.send('', file=discord.File(''))
    print('All systems go!'.format(bot))

# Pin feature
@bot.event
async def on_raw_reaction_add(payload):
    if payload.emoji.name == "📌":
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        await message.pin()
    print("Reaction added.")

@bot.event
async def on_raw_reaction_remove(payload):
    if payload.emoji.name == "📌":
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        pins = [reaction for reaction in message.reactions if str(reaction.emoji) == "📌"]
        if len(pins) == 0:
            await message.unpin()
            button = discord.ui.Button(label="View Unpinned Message", style=discord.ButtonStyle.grey, custom_id="view_unpinned_message")
            async def send_message(ctx: discord.Interaction):
                await ctx.channel.send(content=message.content)
            button.callback = send_message
            view_message_action_row = discord.ui.ActionRow(button)
            await channel.send("A previously pinned message has been unpinned.", components=[view_message_action_row])
    print("A reaction has been removed.")

# api token

bot.run('TOKEN')
svujldwt

svujldwt1#

ActionRow位于discord中,但是如果你想添加一个按钮,最好为它创建一个类。
UnpinButton,您的主按钮:

class UnpinButton(discord.ui.View):
    def __init__(self, message):
        super().__init__()

        self.message = message

    @discord.ui.button(label="View Unpinned Message", style=discord.ButtonStyle.grey, emoji="📌")
    async def button_callback(self, interaction, button):
        await interaction.response.send_message(self.message.content)

在你的类之后,用你的新按钮替换之前的ActionRow消息:

@bot.event
async def on_raw_reaction_remove(payload):
    if payload.emoji.name == "📌":
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        pins = [reaction for reaction in message.reactions if str(reaction.emoji) == "📌"]

        if len(pins) == 0:
            if message.pinned:
                await message.unpin()

                Unpinned = UnpinButton(message)
            
                await channel.send("A previously pinned message has been unpinned.", view=Unpinned)

    print("A reaction has been removed.")

完整的文档在这里:Discord.ActionRow文档

相关问题