SeaCogs/aurora/menus/immune.py

53 lines
2.6 KiB
Python
Raw Normal View History

from discord import ButtonStyle, Interaction, Message, ui
from discord.errors import NotFound
from redbot.core import commands
from redbot.core.utils.chat_formatting import error
2024-05-06 21:39:43 -04:00
from ..utilities.config import config
from ..utilities.factory import immune_embed
class Immune(ui.View):
2024-05-06 21:04:08 -04:00
def __init__(self, ctx: commands.Context, message: Message, timeout: int | None = None):
super().__init__()
self.ctx = ctx
self.message = message
self.timeout = timeout
async def on_timeout(self):
try:
await self.message.edit(view=None)
except NotFound:
pass
@ui.select(cls=ui.RoleSelect, placeholder="Select a role", min_values=0, max_values=25)
async def immune_select(self, interaction: Interaction, select: ui.RoleSelect):
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message(error("You must have the manage guild permission to add immune roles."), ephemeral=True)
return
await interaction.response.defer()
async with config.guild(self.ctx.guild).immune_roles() as immune_roles:
immune_roles: list # type hint
for value in select.values:
if value.id in immune_roles:
immune_roles.remove(value.id)
else:
immune_roles.append(value.id)
2024-01-16 09:26:54 -05:00
await interaction.message.edit(embed=await immune_embed(self.ctx))
@ui.button(label="Clear", style=ButtonStyle.red, row=1)
2024-02-28 10:56:13 -05:00
async def clear(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message(error("You must have the manage guild permission to clear the guild's immune roles."), ephemeral=True)
return
await interaction.response.defer()
await config.guild(self.ctx.guild).immune_roles.clear()
2024-01-16 09:26:54 -05:00
await interaction.message.edit(embed=await immune_embed(self.ctx))
@ui.button(label="Close", style=ButtonStyle.gray)
2024-02-28 10:56:13 -05:00
async def close(self, interaction: Interaction, button: ui.Button): # pylint: disable=unused-argument
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
await interaction.response.send_message(error("You can't do that!"), ephemeral=True)
return
await interaction.message.delete()