属性错误:模块"discord.ui"没有属性"ActionRow"



我在做什么

我正在给我的不和聊天机器人添加一个功能,允许你用: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'

What I tried

我尝试的第一件事是更新我的python版本(现在是3.11)和我的discord.py版本(现在是2.2.2)。同样的问题。我还发现了一个叫做"不和谐成分"的东西。曾经工作过。尝试使用pip3 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')

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文档

最新更新