Skip to content

Commit

Permalink
🐛 fix(groups): correct variables refractor
Browse files Browse the repository at this point in the history
🚚 feat(core, groups): transfer group errors to group plugin
  • Loading branch information
ascpial authored and ZRunner committed Dec 12, 2023
1 parent d9cccfc commit 90781f6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 98 deletions.
10 changes: 1 addition & 9 deletions bot/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,6 @@ async def on_command_error(self, ctx: MyContext, error: Exception):
),
ephemeral=True,
)
# Group "twitter" not found.
result = re.search(r"Group \"([^\"]+)\" not found", raw_error)
if result is not None:
return await ctx.send(
await self.bot._(
ctx.channel, "errors.unknown-group", g=result.group(1)
),
ephemeral=True,
)
# Too many text channels
if raw_error == "Too many text channels":
return await ctx.send(
Expand Down Expand Up @@ -236,6 +227,7 @@ async def on_command_error(self, ctx: MyContext, error: Exception):
ephemeral=True,
)
print("errors -", error)
return
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(
await self.bot._(ctx.channel, "errors.missing-arg", a=error.param.name),
Expand Down
1 change: 0 additions & 1 deletion langs/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ en:
unknown-role: "Unable to find the role %{r} :confused:"
unknown-server: Unknown server
unknown-user: "Unable to find user %{u} :confused:"
unknown-group: "Unable to find group %{u} :confused:"
general:
stats:
title: "Bot statistics"
Expand Down
1 change: 0 additions & 1 deletion langs/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fr:
unknown-role: "Impossible de trouver le rôle %{r} :confused:"
unknown-server: Serveur introuvable
unknown-user: "Impossible de trouver l'utilisateur %{u} :confused:"
unknown-group: "Impossible de trouver le groupe %{g} :confused:"
general:
stats:
title: "Statistiques du bot"
Expand Down
175 changes: 90 additions & 85 deletions plugins/groups/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,71 @@ def __init__(
self._role = None
self._channel = None

def role(self, bot: Gunibot) -> discord.Role:
"""Get the Discord Role attached to that group"""
if self._role is None:
self._role = bot.get_guild(self.guild_id).get_role(self.role_id)
return self._role

def channel(self, bot: Gunibot) -> discord.TextChannel:
"""Get the Discord Text Channel attached to that group"""
if self.channel_id is None:
return None
if self._channel is None:
self._channel = bot.get_guild(self.guild_id).get_channel(self.channel_id)
return self._channel

def member_is_in(self, member: discord.Member) -> bool:
"""Check if a member is part of that group (ie has the attached role)"""
for role in member.roles:
if role.id == self.role_id:
return True
return False

def to_str(self) -> str:
"""Transform the group to a human-readable string"""
channel = f"<#{self.channel_id}>" if self.channel_id else "None"
private = "True" if self.privacy == 1 else "False"
return f"Group: <@&{self.role_id}> (*id : {self.role_id}*)\n"\
f"┗━▷ Owner: <@{self.owner_id}> - Channel: {channel} - Private: {private}"


class GroupConverter(commands.Converter):
"""
Convert a user argument to the corresponding group, by looking for the Role name/id/mention
"""

async def convert(self, ctx: MyContext, arg: str) -> Group:
try:
# try to convert it to a role
role = await commands.RoleConverter().convert(ctx, arg)
except commands.BadArgument:
await ctx.send(
await ctx.bot._(ctx.channel, "groups.error.unknown-group", g=arg),
ephemeral=True
)
raise commands.BadArgument()
# make sure the cog is actually loaded, let's not break everything
if cog := ctx.bot.get_cog("Groups"):
if res := cog.db_get_group(ctx.guild.id, role.id):
# if group exists, we return it
return res
await ctx.send(
await ctx.bot._(ctx.channel, "groups.error.unknown-group", g=arg),
ephemeral=True
)
raise commands.BadArgument()


class Groups(commands.Cog):
def __init__(self, bot: Gunibot):
self.bot = bot
self.config_options = [
"group_allowed_role",
"group_channel_category",
"group_over_role",
"max_group",
]
try:
bot.get_command("config").add_command(self.config_group_allowed_role)
bot.get_command("config").add_command(self.config_group_channel_category)
Expand Down Expand Up @@ -90,66 +155,6 @@ async def config_backup(self, ctx: MyContext):
if ctx.subcommand_passed is None:
await ctx.send_help("config-backup")

def role(self, bot: Gunibot) -> discord.Role:
"""Get the Discord Role attached to that group"""
if self._role is None:
self._role = bot.get_guild(self.guild_id).get_role(self.role_id)
return self._role

def channel(self, bot: Gunibot) -> discord.TextChannel:
"""Get the Discord Text Channel attached to that group"""
if self.channel_id is None:
return None
if self._channel is None:
self._channel = bot.get_guild(self.guild_id).get_channel(self.channel_id)
return self._channel

def member_is_in(self, member: discord.Member) -> bool:
"""Check if a member is part of that group (ie has the attached role)"""
for role in member.roles:
if role.id == self.role_id:
return True
return False

def to_str(self) -> str:
"""Transform the group to a human-readable string"""
channel = f"<#{self.channel_id}>" if self.channel_id else "None"
private = "True" if self.privacy == 1 else "False"
return (
f"Group: <@&{self.role_id}> (*id : {self.role_id}*)\n"
f"┗━▷ Owner: <@{self.owner_id}> - Channel: {channel} - Private: {private}"
)


class GroupConverter(commands.Converter):
"""
Convert a user argument to the corresponding group, by looking for the Role name/id/mention
"""

async def convert(self, ctx: MyContext, arg: str) -> Group:
try:
# try to convert it to a role
role = await commands.RoleConverter().convert(ctx, arg)
except commands.BadArgument as exception:
raise commands.BadArgument(f'Group "{arg}" not found.') from exception
# make sure the cog is actually loaded, let's not break everything
if cog := ctx.bot.get_cog("Groups"):
if res := cog.db_get_group(ctx.guild.id, role.id):
# if group exists, we return it
return res
raise commands.BadArgument(f'Group "{arg}" not found.')


class Groups(commands.Cog):
def __init__(self, bot: Gunibot):
self.bot = bot
self.config_options = [
"group_allowed_role",
"group_channel_category",
"group_over_role",
"max_group",
]

def db_get_config(self, guild_id: int) -> List[Group]:
"""Get every group of a specific guild"""
query = "SELECT rowid, * FROM groups WHERE guild=?"
Expand Down Expand Up @@ -278,23 +283,23 @@ async def group_remove(self, ctx: MyContext, group: GroupConverter):
Use its name, role ID or mention"""
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
return ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
deleted = self.db_delete_group(ctx.guild.id, group.roleID)
deleted = self.db_delete_group(ctx.guild.id, group.role_id)
if deleted: # if everything went fine
role = group.role(self.bot)
await ctx.send(
await self.bot._(ctx.guild.id, "groups.delete", name=role.name)
)
await role.delete()
# try to get the channel
if not (group.channelID and group.channel(self.bot)):
if not (group.channel_id and group.channel(self.bot)):
return
else:
# remove the channel in the database
update = self.db_update_group_channel(ctx.guild.id, group.roleID, None)
update = self.db_update_group_channel(ctx.guild.id, group.role_id, None)
if update:
# delete the channel now
await group.channel(self.bot).delete()
Expand Down Expand Up @@ -328,7 +333,7 @@ async def group_register(self, ctx: MyContext, role: discord.Role):
async def group_unregister(self, ctx: MyContext, group: GroupConverter):
"""Unregister a group without deleting the role
Use his ID, name or mention"""
role_id = group.roleID
role_id = group.role_id
deleted = self.db_delete_group(ctx.guild.id, role_id)
if deleted: # deletion confirmed
role_name = group.role(self.bot).name
Expand All @@ -354,7 +359,7 @@ async def group_modify_owner(
"""Edit the owner of a group"""
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-update"))
Expand All @@ -378,7 +383,7 @@ def check(reaction, user2):
and reaction.message.id == msg.id
)

role_id = group.roleID
role_id = group.role_id
msg = await ctx.send(
await self.bot._(
ctx.guild.id,
Expand Down Expand Up @@ -413,7 +418,7 @@ def check(reaction, user2):
async def group_modify_name(self, ctx: MyContext, group: GroupConverter, name):
"""Edit the name of a group"""
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-update"))
Expand All @@ -422,7 +427,7 @@ async def group_modify_name(self, ctx: MyContext, group: GroupConverter, name):
# let's edit role accordingly
await group.role(self.bot).edit(name=name)
# if we should also update the channel name
if role_name.lower() == group.channel(self.bot).name:
if group.channel_id != None and role_name.lower() == group.channel(self.bot).name:
await group.channel(self.bot).edit(name=name)
await ctx.send(
await self.bot._(
Expand All @@ -441,7 +446,7 @@ async def group_modify_privacy(
Example: group modify privacy CoolGuys private"""
# if user is nor group owner nor server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-update"))
Expand All @@ -452,7 +457,7 @@ async def group_modify_privacy(
return
# it's private if the user asked to (yes)
private = privacy.lower() == "private"
update = self.db_update_group_privacy(ctx.guild.id, group.roleID, private)
update = self.db_update_group_privacy(ctx.guild.id, group.role_id, private)
if update:
await ctx.send(
await self.bot._(
Expand Down Expand Up @@ -511,7 +516,7 @@ async def group_join(self, ctx: MyContext, group: GroupConverter):
async def group_leave(self, ctx: MyContext, group: GroupConverter):
"""Leave a group"""
# the owner cannot leave its own group
if group.ownerID == ctx.author.id:
if group.owner_id == ctx.author.id:
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.owner"))
return
# if user is not even in the group
Expand All @@ -538,7 +543,7 @@ async def group_admin_list(self, ctx: MyContext, group: GroupConverter):
"""Give the userlist of your group"""
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
Expand All @@ -563,7 +568,7 @@ async def group_admin_add(
Use that if the group is set to private"""
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
Expand Down Expand Up @@ -592,7 +597,7 @@ async def group_admin_remove(
"""Remove a user to a group (by force)"""
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
Expand Down Expand Up @@ -623,18 +628,18 @@ async def group_channel_remove(self, ctx: MyContext, group: GroupConverter):
"""Remove a group channel"""
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
return
# try to get the channel
if not (group.channelID and group.channel(self.bot)):
if not (group.channel_id and group.channel(self.bot)):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-channel"))
return
else:
# remove the channel in the database
update = self.db_update_group_channel(ctx.guild.id, group.roleID, None)
update = self.db_update_group_channel(ctx.guild.id, group.role_id, None)
if update:
# delete the channel now
await group.channel(self.bot).delete()
Expand All @@ -659,13 +664,13 @@ async def group_channel_add(self, ctx: MyContext, group: GroupConverter, name=No
name = group.role(self.bot).name
# if user is not the group owner and neither a server admin, we abort
if (
group.ownerID != ctx.author.id
group.owner_id != ctx.author.id
and not ctx.author.guild_permissions.administrator
):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
return
# if channel already exists
if group.channelID and group.channel(self.bot):
if group.channel_id and group.channel(self.bot):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.channel-exist"))
return
# if no category has been created
Expand All @@ -691,7 +696,7 @@ async def group_channel_add(self, ctx: MyContext, group: GroupConverter, name=No
channel = await ctx.guild.create_text_channel(
name=name, overwrites=overwrite, category=categ
)
self.db_update_group_channel(ctx.guild.id, group.roleID, channel.id)
self.db_update_group_channel(ctx.guild.id, group.role_id, channel.id)
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.channel-create", name=group.role(self.bot).name
Expand All @@ -707,11 +712,11 @@ async def group_channel_register(
"""Register a channel as a group channel
You'll have to edit the permissions yourself :/"""
# if a channel already exists for that group
if group.channelID and group.channel(self.bot):
if group.channel_id and group.channel(self.bot):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.channel-exist"))
return
# update database, say yeepee
self.db_update_group_channel(ctx.guild.id, group.roleID, channel.id)
self.db_update_group_channel(ctx.guild.id, group.role_id, channel.id)
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.channel-registred", name=group.role(self.bot).name
Expand All @@ -725,11 +730,11 @@ async def group_channel_unregister(self, ctx: MyContext, group: GroupConverter):
"""Unregister a channel as a group channel
This action will not delete the channel!"""
# if no channel can be found
if not (group.channelID and group.channel(self.bot)):
if not (group.channel_id and group.channel(self.bot)):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-channel"))
return
else:
update = self.db_update_group_channel(ctx.guild.id, group.roleID, None)
update = self.db_update_group_channel(ctx.guild.id, group.role_id, None)
if update:
await ctx.send(
await self.bot._(
Expand Down
3 changes: 2 additions & 1 deletion plugins/groups/langs/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ en:
no-channel: "This group don't have a channel"
no-delete-channel: "Cannot find a group with this identifier or name"
channel-exist: "This group already have a channel"
no-category: "You have to config a category for groups channels : `%{p}config group_channel_category`"
no-category: "You have to config a category for groups channels : `%{p}config group_channel_category`"
unknown-group: "Unable to find group %{g} :confused:"
3 changes: 2 additions & 1 deletion plugins/groups/langs/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ fr:
no-channel: "Ce group ne possède pas de salon"
no-delete-channel: "Impossible de trouver un groupe avec cet identifiant ou ce nom"
channel-exist: "Ce groupe possède déja un salon"
no-category: "Vous devez d'abord configurer une catégorie pour les salons de groupes : `%{p}config group_channel_category`"
no-category: "Vous devez d'abord configurer une catégorie pour les salons de groupes : `%{p}config group_channel_category`"
unknown-group: "Impossible de trouver le groupe %{g} :confused:"

0 comments on commit 90781f6

Please sign in to comment.