Skip to content

Commit

Permalink
added setup commands for default role and admin roles
Browse files Browse the repository at this point in the history
  • Loading branch information
camalot committed May 11, 2024
1 parent eb364ec commit 0f877e4
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 40 deletions.
42 changes: 42 additions & 0 deletions bot/cogs/lib/mongodb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,48 @@ def set(self, settings: GuildSettings):
)
return False

def set_default_role(self, guildId: int, roleId: int, categoryId: typing.Optional[int] = None, userId: typing.Optional[int] = None) -> bool:
_method = inspect.stack()[0][3]
try:
if self.connection is None:
self.open()

if userId is not None: # check user settings
user_settings = self.connection.user_settings.find_one({"guild_id": str(guildId), "user_id": str(userId)})
if user_settings:
self.connection.user_settings.update_one(
{"guild_id": str(guildId), "user_id": str(userId)},
{"$set": {"default_role": str(roleId), "timestamp": utils.get_timestamp()}},
upsert=True,
)
return True

if categoryId is not None:
category_settings = self.connection.category_settings.find_one({"guild_id": str(guildId), "category_id": str(categoryId)})
if category_settings:
self.connection.category_settings.update_one(
{"guild_id": str(guildId), "category_id": str(categoryId)},
{"$set": {"default_role": str(roleId), "timestamp": utils.get_timestamp()}},
upsert=True,
)
return True

self.connection.guild_settings.update_one(
{"guild_id": str(guildId)},
{"$set": {"default_role": str(roleId), "timestamp": utils.get_timestamp()}},
upsert=True,
)
return True
except Exception as ex:
self.log(
guildId=guildId,
level=LogLevel.ERROR,
method=f"{self._module}.{self._class}.{_method}",
message=f"{ex}",
stackTrace=traceback.format_exc(),
)
return False

def get_default_role(self, guildId: int, categoryId: typing.Optional[int], userId: typing.Optional[int]) -> typing.Optional[int]:
_method = inspect.stack()[0][3]
try:
Expand Down
154 changes: 114 additions & 40 deletions bot/cogs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,27 +406,32 @@ async def category_app_command(
locked: typing.Optional[bool] = False,
default_role: typing.Optional[discord.Role] = None,
):
_method = inspect.stack()[0][3]
if interaction.guild is None:
return

# is user an admin
if not self._users.isAdmin(interaction):
await interaction.response.send_message("You are not an administrator of the bot. You cannot use this command.", ephemeral=True)
return

guild_id = interaction.guild.id
await self._configure_category(
guild_id=guild_id,
category_id=category.id,
bitrate=bitrate,
limit=limit,
auto_game=auto_game,
allow_soundboard=allow_soundboard,
auto_name=auto_name,
locked=locked,
default_role=default_role
)
await interaction.response.send_message("Category settings updated", ephemeral=True)
try:
if not self._users.isAdmin(interaction):
await interaction.response.send_message(
self.settings.get_string(interaction.guild.id, "info_permission_denied"),
ephemeral=True,
)
return
await self._configure_category(
guild_id=guild_id,
category_id=category.id,
bitrate=bitrate,
limit=limit,
auto_game=auto_game,
allow_soundboard=allow_soundboard,
auto_name=auto_name,
locked=locked,
default_role=default_role,
)
await interaction.response.send_message("Category settings updated", ephemeral=True)
except Exception as e:
self.log.error(guild_id, f"{self._module}.{_method}", f"{e}", traceback.format_exc())

async def _configure_category(
self,
Expand Down Expand Up @@ -484,13 +489,16 @@ async def channel_app_command(
if interaction.guild is None:
return

# is user an admin
if not self._users.isAdmin(interaction):
await interaction.response.send_message("You are not an administrator of the bot. You cannot use this command.", ephemeral=True)
return

guild_id = interaction.guild.id
try:
# is user an admin
if not self._users.isAdmin(interaction):
await interaction.response.send_message(
self.settings.get_string(interaction.guild.id, "info_permission_denied"),
ephemeral=True,
)
return

if channel_name is None or channel_name == "":
channel_name = "CREATE CHANNEL 🔊"
if use_stage is None:
Expand All @@ -512,15 +520,15 @@ async def channel_app_command(
bitrate=64 * 1000,
user_limit=0,
position=0,
# overrites={
# temp_default_role: discord.PermissionOverwrite(
# view_channel=True,
# connect=True,
# speak=True,
# stream=False,
# use_voice_activation=True
# )
# },
overwrites={
temp_default_role: discord.PermissionOverwrite(
view_channel=True,
connect=True,
speak=True,
stream=False,
use_voice_activation=True
)
},
reason=f"Created for VoiceCreateBot by {interaction.user.name}"
)
else:
Expand All @@ -529,15 +537,15 @@ async def channel_app_command(
bitrate=64 * 1000,
user_limit=0,
position=0,
# overrites={
# temp_default_role: discord.PermissionOverwrite(
# view_channel=True,
# connect=True,
# speak=True,
# stream=False,
# use_voice_activation=True
# )
# },
overwrites={
temp_default_role: discord.PermissionOverwrite(
view_channel=True,
connect=True,
speak=True,
stream=False,
use_voice_activation=True
)
},
reason=f"Created for VoiceCreateBot by {interaction.user.name}"
)

Expand Down Expand Up @@ -657,13 +665,79 @@ async def user(self, ctx, inputRole: typing.Optional[typing.Union[str, discord.R
except Exception as e:
self.log.error(ctx.guild.id, f"{self._module}.{_method}", f"{e}", traceback.format_exc())

@group.command(name="admin", description="Add or remove an admin role")
@commands.guild_only()
@app_commands.guild_only()
@commands.has_permissions(administrator=True)
@app_commands.default_permissions(administrator=True)
@app_commands.describe(action="Add or Remove the role")
@app_commands.describe(role="The role to add or remove")
async def role_admin_app_command(self, interaction: discord.Interaction, action: AddRemoveAction, role: discord.Role):
_method = inspect.stack()[0][3]
if interaction.guild is None:
return

guild_id = interaction.guild.id
try:
if not self._users.isAdmin(interaction):
await interaction.response.send_message(
self.settings.get_string(guild_id, "info_permission_denied"),
ephemeral=True,
)
return

if action == AddRemoveAction.ADD:
self.settings.db.add_admin_role(guildId=guild_id, roleId=role.id)
await interaction.response.send_message(f"Added role {role.name}", ephemeral=True)
elif action == AddRemoveAction.REMOVE:
self.settings.db.delete_admin_role(guildId=guild_id, roleId=role.id)
await interaction.response.send_message(f"Removed role {role.name}", ephemeral=True)
except Exception as e:
self.log.error(guild_id, f"{self._module}.{_method}", f"{e}", traceback.format_exc())
await self.messaging.notify_of_error(interaction)

@group.command(name="default-role", description="Set the default role for channels")
@commands.guild_only()
@app_commands.guild_only()
@commands.has_permissions(administrator=True)
@app_commands.default_permissions(administrator=True)
@app_commands.describe(role="The role to set as the default role for channels")
async def role_default_app_command(self, interaction: discord.Interaction, role: discord.Role):
_method = inspect.stack()[0][3]
if interaction.guild is None:
return

guild_id = interaction.guild.id
try:
if not self._users.isAdmin(interaction):
await interaction.response.send_message(
self.settings.get_string(guild_id, "info_permission_denied"),
ephemeral=True,
)
return

self.settings.db.set_default_role(guildId=guild_id, roleId=role.id)
await interaction.response.send_message(f"Set role {role.name} as the default guild role", ephemeral=True)
except Exception as e:
self.log.error(guild_id, f"{self._module}.{_method}", f"{e}", traceback.format_exc())
await self.messaging.notify_of_error(interaction)

@role.command(name="admin", aliases=['a'])
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def admin(self, ctx, action: typing.Optional[AddRemoveAction], role: typing.Optional[typing.Union[str, discord.Role]]):
_method = inspect.stack()[0][3]
guild_id = ctx.guild.id
try:
if not self._users.isAdmin(ctx):
await self.messaging.send_embed(
channel=ctx.channel,
message=self.settings.get_string(guild_id, "info_permission_denied"),
title=self.settings.get_string(guild_id, "title_permission_denied"),
delete_after=10
)
return

if action is None and role is None:
# return the list of admin roles
pass
Expand Down

0 comments on commit 0f877e4

Please sign in to comment.