Skip to content

Commit

Permalink
fix: better code and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ascpial authored and ZRunner committed Dec 12, 2023
1 parent 90781f6 commit d0598dd
Showing 1 changed file with 51 additions and 50 deletions.
101 changes: 51 additions & 50 deletions plugins/groups/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
de la licence CeCILL diffusée sur le site "http://www.cecill.info".
"""

from typing import List
from typing import List, Annotated
import asyncio

import discord
Expand Down Expand Up @@ -35,18 +35,18 @@ def __init__(
self._role = None
self._channel = None

def role(self, bot: Gunibot) -> discord.Role:
def role(self) -> 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)
self._role = self.bot.get_guild(self.guild_id).get_role(self.role_id)
return self._role

def channel(self, bot: Gunibot) -> discord.TextChannel:
def channel(self) -> 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)
self._channel = self.bot.get_guild(self.guild_id).get_channel(self.channel_id)
return self._channel

def member_is_in(self, member: discord.Member) -> bool:
Expand Down Expand Up @@ -90,6 +90,7 @@ async def convert(self, ctx: MyContext, arg: str) -> Group:
)
raise commands.BadArgument()

GroupType = Annotated[Group, GroupConverter]

class Groups(commands.Cog):
def __init__(self, bot: Gunibot):
Expand Down Expand Up @@ -278,7 +279,7 @@ async def group_add(self, ctx: MyContext, name: str):

@group_main.command(name="remove")
@commands.check(checks.can_group)
async def group_remove(self, ctx: MyContext, group: GroupConverter):
async def group_remove(self, ctx: MyContext, group: GroupType):
"""Delete a group
Use its name, role ID or mention"""
# if user is not the group owner and neither a server admin, we abort
Expand All @@ -289,20 +290,20 @@ async def group_remove(self, ctx: MyContext, group: GroupConverter):
return ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
deleted = self.db_delete_group(ctx.guild.id, group.role_id)
if deleted: # if everything went fine
role = group.role(self.bot)
role = group.role()
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.channel_id and group.channel(self.bot)):
if not (group.channel_id and group.channel()):
return
else:
# remove the channel in the database
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()
await group.channel().delete()
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.channel_delete", group=role.name
Expand Down Expand Up @@ -330,13 +331,13 @@ async def group_register(self, ctx: MyContext, role: discord.Role):

@group_main.command(name="unregister")
@commands.check(checks.is_admin)
async def group_unregister(self, ctx: MyContext, group: GroupConverter):
async def group_unregister(self, ctx: MyContext, group: GroupType):
"""Unregister a group without deleting the role
Use his ID, name or mention"""
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
role_name = group.role().name
await ctx.send(
await self.bot._(ctx.guild.id, "groups.unregistred", name=role_name)
)
Expand All @@ -354,7 +355,7 @@ async def group_modify_main(self, ctx: MyContext):
@group_modify_main.command(name="leader")
@commands.cooldown(1, 30, commands.BucketType.user)
async def group_modify_owner(
self, ctx: MyContext, group: GroupConverter, user: discord.Member
self, ctx: MyContext, group: GroupType, user: discord.Member
):
"""Edit the owner of a group"""
# if user is not the group owner and neither a server admin, we abort
Expand Down Expand Up @@ -390,7 +391,7 @@ def check(reaction, user2):
"groups.give",
user=user.mention,
owner=ctx.author.name,
group=group.role(self.bot).name,
group=group.role().name,
)
)
await msg.add_reaction("✅")
Expand All @@ -409,26 +410,26 @@ def check(reaction, user2):
ctx.guild.id,
"groups.update_owner",
owner=user.name,
group=group.role(self.bot).name,
group=group.role().name,
)
)

@group_modify_main.command(name="name")
@commands.cooldown(1, 10, commands.BucketType.user)
async def group_modify_name(self, ctx: MyContext, group: GroupConverter, name):
async def group_modify_name(self, ctx: MyContext, group: GroupType, name):
"""Edit the name of a group"""
if (
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"))
return
role_name = group.role(self.bot).name
role_name = group.role().name
# let's edit role accordingly
await group.role(self.bot).edit(name=name)
await group.role().edit(name=name)
# if we should also update the channel name
if group.channel_id != None and role_name.lower() == group.channel(self.bot).name:
await group.channel(self.bot).edit(name=name)
if group.channel_id is not None and role_name.lower() == group.channel().name:
await group.channel().edit(name=name)
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.update_name", name=name, group=role_name
Expand All @@ -438,7 +439,7 @@ async def group_modify_name(self, ctx: MyContext, group: GroupConverter, name):
@group_modify_main.command(name="privacy")
@commands.cooldown(1, 20, commands.BucketType.user)
async def group_modify_privacy(
self, ctx: MyContext, group: GroupConverter, privacy: str
self, ctx: MyContext, group: GroupType, privacy: str
):
"""Edit the privacy of a group
Privacy parameter needs to be either 'private' or 'public'
Expand All @@ -464,7 +465,7 @@ async def group_modify_privacy(
ctx.guild.id,
"groups.update_privacy",
privacy=privacy,
group=group.role(self.bot).name,
group=group.role().name,
)
)
else: # bruh
Expand Down Expand Up @@ -492,7 +493,7 @@ async def group_list(self, ctx: MyContext):

@group_main.command(name="join")
@commands.cooldown(1, 10, commands.BucketType.user)
async def group_join(self, ctx: MyContext, group: GroupConverter):
async def group_join(self, ctx: MyContext, group: GroupType):
"""Join a group"""
if group.privacy is None: # group doesn't exist
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-exist"))
Expand All @@ -504,16 +505,16 @@ async def group_join(self, ctx: MyContext, group: GroupConverter):
elif group.privacy and not await checks.is_admin(ctx):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.private"))
else:
await ctx.author.add_roles(group.role(self.bot), reason="Joined a group")
await ctx.author.add_roles(group.role(), reason="Joined a group")
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.join", name=group.role(self.bot).name
ctx.guild.id, "groups.join", name=group.role().name
)
)

@group_main.command(name="leave")
@commands.cooldown(1, 10, commands.BucketType.user)
async def group_leave(self, ctx: MyContext, group: GroupConverter):
async def group_leave(self, ctx: MyContext, group: GroupType):
"""Leave a group"""
# the owner cannot leave its own group
if group.owner_id == ctx.author.id:
Expand All @@ -523,10 +524,10 @@ async def group_leave(self, ctx: MyContext, group: GroupConverter):
if not group.member_is_in(ctx.author):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-in"))
return
await ctx.author.remove_roles(group.role(self.bot), reason="Left a group")
await ctx.author.remove_roles(group.role(), reason="Left a group")
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.leave", name=group.role(self.bot).name
ctx.guild.id, "groups.leave", name=group.role().name
)
)

Expand All @@ -539,7 +540,7 @@ async def group_admin_main(self, ctx: MyContext):

@group_admin_main.command(name="list")
@commands.cooldown(1, 15, commands.BucketType.user)
async def group_admin_list(self, ctx: MyContext, group: GroupConverter):
async def group_admin_list(self, ctx: MyContext, group: GroupType):
"""Give the userlist of your group"""
# if user is not the group owner and neither a server admin, we abort
if (
Expand All @@ -549,7 +550,7 @@ async def group_admin_list(self, ctx: MyContext, group: GroupConverter):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
return
txt = "**" + await self.bot._(ctx.guild.id, "groups.userlist") + "**\n"
for user in group.role(self.bot).members:
for user in group.role().members:
txt += user.mention + "\n"
# if we can use embeds, let's use them
if ctx.can_send_embed:
Expand All @@ -562,7 +563,7 @@ async def group_admin_list(self, ctx: MyContext, group: GroupConverter):
@group_admin_main.command(name="add")
@commands.cooldown(1, 8, commands.BucketType.user)
async def group_admin_add(
self, ctx: MyContext, group: GroupConverter, user: discord.Member
self, ctx: MyContext, group: GroupType, user: discord.Member
):
"""Add a user to a group (by force)
Use that if the group is set to private"""
Expand All @@ -579,20 +580,20 @@ async def group_admin_add(
await self.bot._(ctx.guild.id, "groups.error.already-in-user")
)
return
await user.add_roles(group.role(self.bot))
await user.add_roles(group.role())
await ctx.send(
await self.bot._(
ctx.guild.id,
"groups.joinbyforce",
name=group.role(self.bot).name,
name=group.role().name,
user=user.name,
)
)

@group_admin_main.command(name="remove")
@commands.cooldown(1, 8, commands.BucketType.user)
async def group_admin_remove(
self, ctx: MyContext, group: GroupConverter, user: discord.Member
self, ctx: MyContext, group: GroupType, user: discord.Member
):
"""Remove a user to a group (by force)"""
# if user is not the group owner and neither a server admin, we abort
Expand All @@ -606,12 +607,12 @@ async def group_admin_remove(
if not group.member_is_in(ctx.author):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-in-user"))
return
await user.remove_roles(group.role(self.bot))
await user.remove_roles(group.role())
await ctx.send(
await self.bot._(
ctx.guild.id,
"groups.leavebyforce",
name=group.role(self.bot).name,
name=group.role().name,
user=user.name,
)
)
Expand All @@ -624,7 +625,7 @@ async def group_channel_main(self, ctx: MyContext):

@group_channel_main.command(name="remove")
@commands.cooldown(1, 30, commands.BucketType.user)
async def group_channel_remove(self, ctx: MyContext, group: GroupConverter):
async def group_channel_remove(self, ctx: MyContext, group: GroupType):
"""Remove a group channel"""
# if user is not the group owner and neither a server admin, we abort
if (
Expand All @@ -634,20 +635,20 @@ async def group_channel_remove(self, ctx: MyContext, group: GroupConverter):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
return
# try to get the channel
if not (group.channel_id and group.channel(self.bot)):
if not (group.channel_id and group.channel()):
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.role_id, None)
if update:
# delete the channel now
await group.channel(self.bot).delete()
await group.channel().delete()
await ctx.send(
await self.bot._(
ctx.guild.id,
"groups.channel_delete",
group=group.role(self.bot).name,
group=group.role().name,
)
)
else: # oops
Expand All @@ -657,11 +658,11 @@ async def group_channel_remove(self, ctx: MyContext, group: GroupConverter):

@group_channel_main.command(name="add")
@commands.cooldown(1, 30, commands.BucketType.user)
async def group_channel_add(self, ctx: MyContext, group: GroupConverter, name=None):
async def group_channel_add(self, ctx: MyContext, group: GroupType, name=None):
"""Create a private channel for you group
Provide a channel name if you want to set it differently than the group name"""
if not name:
name = group.role(self.bot).name
name = group.role().name
# if user is not the group owner and neither a server admin, we abort
if (
group.owner_id != ctx.author.id
Expand All @@ -670,7 +671,7 @@ async def group_channel_add(self, ctx: MyContext, group: GroupConverter, name=No
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.not-owner"))
return
# if channel already exists
if group.channel_id and group.channel(self.bot):
if group.channel_id and group.channel():
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.channel-exist"))
return
# if no category has been created
Expand All @@ -690,7 +691,7 @@ async def group_channel_add(self, ctx: MyContext, group: GroupConverter, name=No
# prepare channel overwrites
overwrite = {
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False),
group.role(self.bot): discord.PermissionOverwrite(read_messages=True),
group.role(): discord.PermissionOverwrite(read_messages=True),
}
# create channel, save it, say success, end of the story.
channel = await ctx.guild.create_text_channel(
Expand All @@ -699,38 +700,38 @@ async def group_channel_add(self, ctx: MyContext, group: GroupConverter, name=No
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
ctx.guild.id, "groups.channel-create", name=group.role().name
)
)

@group_channel_main.command(name="register")
@commands.check(checks.is_admin)
@commands.cooldown(1, 30, commands.BucketType.guild)
async def group_channel_register(
self, ctx: MyContext, group: GroupConverter, channel: discord.TextChannel
self, ctx: MyContext, group: GroupType, channel: discord.TextChannel
):
"""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.channel_id and group.channel(self.bot):
if group.channel_id and group.channel():
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.role_id, channel.id)
await ctx.send(
await self.bot._(
ctx.guild.id, "groups.channel-registred", name=group.role(self.bot).name
ctx.guild.id, "groups.channel-registred", name=group.role().name
)
)

@group_channel_main.command(name="unregister")
@commands.check(checks.is_admin)
@commands.cooldown(1, 30, commands.BucketType.guild)
async def group_channel_unregister(self, ctx: MyContext, group: GroupConverter):
async def group_channel_unregister(self, ctx: MyContext, group: GroupType):
"""Unregister a channel as a group channel
This action will not delete the channel!"""
# if no channel can be found
if not (group.channel_id and group.channel(self.bot)):
if not (group.channel_id and group.channel()):
await ctx.send(await self.bot._(ctx.guild.id, "groups.error.no-channel"))
return
else:
Expand All @@ -740,7 +741,7 @@ async def group_channel_unregister(self, ctx: MyContext, group: GroupConverter):
await self.bot._(
ctx.guild.id,
"groups.channel_unregister",
group=group.role(self.bot).name,
group=group.role().name,
)
)

Expand Down

0 comments on commit d0598dd

Please sign in to comment.