2024-05-03 22:20:53 -04:00
|
|
|
from discord import ButtonStyle, Interaction, Message, ui
|
2024-01-16 07:57:42 -05:00
|
|
|
from redbot.core import commands
|
2024-01-16 08:43:22 -05:00
|
|
|
from redbot.core.utils.chat_formatting import error
|
2024-01-16 07:57:42 -05:00
|
|
|
|
2024-05-06 21:39:43 -04:00
|
|
|
from ..utilities.config import config
|
|
|
|
from ..utilities.factory import addrole_embed
|
2024-02-14 11:04:26 -05:00
|
|
|
|
|
|
|
|
2024-01-16 07:57:42 -05:00
|
|
|
class Addrole(ui.View):
|
2024-05-06 21:04:08 -04:00
|
|
|
def __init__(self, ctx: commands.Context, message: Message, timeout: int | None = None):
|
2024-01-16 07:57:42 -05:00
|
|
|
super().__init__()
|
|
|
|
self.ctx = ctx
|
2024-05-03 22:20:53 -04:00
|
|
|
self.message = message
|
|
|
|
self.timeout = timeout
|
|
|
|
|
|
|
|
async def on_timeout(self):
|
|
|
|
await self.message.edit(view=None)
|
2024-01-16 07:57:42 -05:00
|
|
|
|
2024-01-16 08:43:22 -05:00
|
|
|
@ui.select(cls=ui.RoleSelect, placeholder="Select a role", min_values=0, max_values=25)
|
2024-01-16 08:39:12 -05:00
|
|
|
async def addrole_select(self, interaction: Interaction, select: ui.RoleSelect):
|
2024-01-16 08:03:51 -05:00
|
|
|
if not interaction.user.guild_permissions.manage_guild and not interaction.user.guild_permissions.administrator:
|
2024-01-16 08:43:22 -05:00
|
|
|
await interaction.response.send_message(error("You must have the manage guild permission to add roles to the addrole whitelist."), ephemeral=True)
|
2024-01-16 07:57:42 -05:00
|
|
|
return
|
|
|
|
await interaction.response.defer()
|
2024-04-08 11:46:22 -04:00
|
|
|
async with config.guild(self.ctx.guild).addrole_whitelist() as addrole_whitelist:
|
|
|
|
addrole_whitelist: list # type hint
|
|
|
|
for value in select.values:
|
|
|
|
if value.id in addrole_whitelist:
|
|
|
|
addrole_whitelist.remove(value.id)
|
|
|
|
else:
|
|
|
|
addrole_whitelist.append(value.id)
|
2024-01-16 09:26:54 -05:00
|
|
|
await interaction.message.edit(embed=await addrole_embed(self.ctx))
|
2024-01-16 08:43:22 -05:00
|
|
|
|
|
|
|
@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
|
2024-01-16 08:43:22 -05:00
|
|
|
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 addrole whitelist."), ephemeral=True)
|
|
|
|
return
|
|
|
|
await interaction.response.defer()
|
|
|
|
await config.guild(self.ctx.guild).addrole_whitelist.clear()
|
2024-01-16 09:26:54 -05:00
|
|
|
await interaction.message.edit(embed=await addrole_embed(self.ctx))
|
2024-01-16 08:39:12 -05:00
|
|
|
|
2024-01-16 08:44:19 -05:00
|
|
|
@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
|
2024-01-16 08:43:22 -05:00
|
|
|
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
|
2024-01-16 08:39:12 -05:00
|
|
|
await interaction.message.delete()
|