我在做什么
我正在为我的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')
1条答案
按热度按时间svujldwt1#
ActionRow
位于discord中,但是如果你想添加一个按钮,最好为它创建一个类。UnpinButton,您的主按钮:
在你的类之后,用你的新按钮替换之前的ActionRow消息:
完整的文档在这里:Discord.ActionRow文档