Как сделать вечные кнопки @commands.Cog.listener()
Помогите сделать вечные кнопки в nextcord.
@commands.Cog.listener()
async def on_message(self, message):
channel = self.bot.get_channel(1009917009175269417)
info = self.bot.get_channel(1009917009175269417)
rolestmoder = message.guild.get_role(942397028825169938)
gs_no = message.guild.get_role(942397028854542399)
zgs_no = message.guild.get_role(942397028854542398)
sledak_no = message.guild.get_role(942404384392871968)
gs_getto = message.guild.get_role(942397028854542396)
zgs_getto = message.guild.get_role(942397028837785628)
sledak = message.guild.get_role(942397028837785625)
psgs = message.guild.get_role(942502889799172096)
timestamp = datetime.now(pytz.timezone(r'Europe/Moscow'))
whitelist = ['gg1', 'gg2']
groven=['gg']
grove = message.guild.get_role(id)
groved = message.guild.get_role(id)
if message.channel == channel and not message.author.bot and (len(list(filter (lambda x : x in message.content.lower(), whitelist))) > 0):
if message.channel == channel and not message.author.bot and (len(list(filter (lambda x : x in message.content.lower(), groven))) > 0):
button = Button(label = 'Одобрить', style=nextcord.ButtonStyle.green, custom_id='pokaz')
async def pokaz(interaction):
if message.channel == channel and not message.author.bot and (rolestmoder in interaction.user.roles or gs_no in interaction.user.roles or zgs_no in interaction.user.roles or gs_getto in interaction.user.roles or zgs_getto in interaction.user.roles or sledak in interaction.user.roles or sledak_no in interaction.user.roles or psgs in interaction.user.roles):
newtimestamp = datetime.now(pytz.timezone(r'Europe/Moscow'))
embedVar = nextcord.Embed(title="✅ | Забив капта", description=message.author.mention, color=0x00ff00)
embedVar.add_field(name="Текст забива", value=f'```{message.content}```', inline=True)
embedVar.add_field(name="Одобрено", value= interaction.user.mention, inline=False)
embedVar.set_footer(text=f'DeLorean \u200b{timestamp}', icon_url="https://cdn.nextcordapp.com/avatars/706621027953672253/3fcb6700f5e6fccb8e9e46b720c26208.png")
await info.send(embed=embedVar)
await yvedom.edit(embed=embedVar, view=None)
button.callback = pokaz
button2 = Button(label = 'Отклонить', style=nextcord.ButtonStyle.red, custom_id='pokaz2')
async def pokaz2(interaction):
if message.channel == channel and not message.author.bot and (rolestmoder in interaction.user.roles or gs_no in interaction.user.roles or zgs_no in interaction.user.roles or gs_getto in interaction.user.roles or zgs_getto in interaction.user.roles or sledak in interaction.user.roles or sledak_no in interaction.user.roles or psgs in interaction.user.roles):
newtimestamp = datetime.now(pytz.timezone(r'Europe/Moscow'))
embedVar = nextcord.Embed(title="? | Забив капта", description=message.author.mention, color=0xFF4500)
embedVar.add_field(name="Текст забива", value=f'```{message.content}```', inline=True)
embedVar.add_field(name="Отказан", value= interaction.user.mention, inline=False)
embedVar.set_footer(text=f'DeLorean \u200b{timestamp}', icon_url="https://cdn.nextcordapp.com/avatars/706621027953672253/3fcb6700f5e6fccb8e9e46b720c26208.png")
await yvedom.edit(embed=embedVar, view=None)
button2.callback = pokaz2
```
Ответы (1 шт):
Автор решения: DWolf_19
→ Ссылка
На nextcord никогда не писал, но тщательно проверил документацию. Всё аналогично discord.py
Создайте класс с кнопками и в качестве таймаута передайте None. Подробнее.
class Buttons(nextcord.ui.View):
def __init__(self):
super().__init__(timeout=None) # важно
@nextcord.ui.button(label="...", emoji="...", style=discord.ButtonStyle.<...>)
async def button(self, interaction, button):
...
Также добавьте экземпляр данного класса к объекту бота.
bot.add_view(Buttons())
При отправке сообщения, к которому будут прикреплены кнопки, также передаётся экземпляр.
await my_favourite_channel.send(
embed=nextcord.Embed(...),
view=Buttons()
)