`AttributeError: 模块 ‘discord.ui’ 没有 ‘ActionRow’ 属性`

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

AttributeError: module 'discord.ui' has no attribute 'ActionRow'

问题

我理解你的要求,以下是翻译好的内容:

我正在制作的内容

我正在为我的Discord机器人添加一个功能,允许你使用 :pushpin: 固定和取消固定消息。当你取消固定一条消息时,机器人会发送一条消息,说明消息已被取消固定。

我的代码

# 固定功能
@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("已添加反应。")

@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="查看取消固定的消息", 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("先前已固定的消息已被取消固定。", components=[view_message_action_row])
    print("已移除反应。")

错误信息

如果我删除所有使按钮弹出的代码,消息会显示。但我想要按钮存在,但使用我现在的方法,消息根本不会弹出,并显示以下错误:

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" 的东西曾经有效。尝试使用 pip3 install discord-components 下载它,但没有任何效果。我听说discord.py本身支持按钮,所以我想这也不会有所帮助。

完整代码

不知道这是否对任何人有所帮助,但以防万一,这是机器人的完整代码:

import discord
from discord.ext import commands

# 无聊的内容

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

THRESHOLD = 5

@bot.event
async def on_ready():
    channel = bot.get_channel(1069346031281651843)
    # 用于在服务器中发布公告,目前已禁用(应该使其更有效)
    # await channel.send('', file=discord.File(''))
    print('所有系统正常!'.format(bot))

# 固定功能
@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("已添加反应。")

@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="查看取消固定的消息", 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("先前已固定的消息已被取消固定。", components=[view_message_action_row])
    print("已移除反应。")

# API令牌

bot.run('TOKEN')
英文:

What i'm making

I am adding a feature to my discord bot that allows you to pin and unpin messages with :pushpin:. When you unpin a message, the bot sends a message saying that a message has been unpinned.

My code

# 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.")

I want it so that there is a button that is under the "pinned message has been unpinned" message that redirects you to the message that was unpinned.

The error

If i remove all the code that makes the button pop up, the message appears.
I want the button to be there though, but with what i'm using, the message doesn't pop up at all and gives me this error:

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'

What I tried

Some of the things first things i've tried are updating my python version (now 3.11) and my discord.py version (now 2.2.2). Same problem.

I've also found that something called "discord-components" once worked.
Tried downloading it using pip3 install discord-components, but it did nothing. I heard that discord.py supports buttons on it's own anyway so I guess it wouldn't have helped either way.

Full code

Don't know if this is gonna help anyone, but just in case, here is the bot's entire code:

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')

答案1

得分: 0

ActionRow 是位于 Discord 中的,如果你想要添加一个按钮,最好创建一个类来代替。

UnpinButton,你的主按钮:

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

        self.message = message

    @discord.ui.button(label="查看取消置顶的消息", 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("先前置顶的消息已取消置顶。", view=Unpinned)

    print("已移除一项反应。")

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

英文:

ActionRow is located in discord, though if you're wanting to add a button, its better to create a class for it.

UnpinButton, your main button:

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)

After your class, just replace the previous ActionRow message with your new button:

@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.")

The full documentation is here: Discord.ActionRow Documentation

huangapple
  • 本文由 发表于 2023年4月11日 12:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982418.html
匿名

发表评论

匿名网友

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

确定