Replaced legacy Union[] with new syntax

This commit is contained in:
Profitroll 2024-12-27 00:18:54 +01:00
parent 7b64f6938b
commit eed084cd91
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
6 changed files with 31 additions and 42 deletions

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import Any, Union, Dict from typing import Any, Dict
from bson import ObjectId from bson import ObjectId
from discord import User, Member from discord import User, Member
@ -12,11 +12,11 @@ logger = logging.getLogger(__name__)
class HoloUser: class HoloUser:
def __init__(self, user: Union[User, Member, int]) -> None: def __init__(self, user: User | Member | int) -> None:
"""Get an object that has a proper binding between Discord ID and database """Get an object that has a proper binding between Discord ID and database
### Args: ### Args:
* `user` (Union[User, Member, int]): Object from which ID can be extracted * `user` (User | Member | int): Object from which ID can be extracted
### Raises: ### Raises:
* `UserNotFoundError`: User with such ID does not seem to exist in database * `UserNotFoundError`: User with such ID does not seem to exist in database
@ -24,17 +24,15 @@ class HoloUser:
self.id: int = user if not hasattr(user, "id") else user.id self.id: int = user if not hasattr(user, "id") else user.id
jav_user: Union[Dict[str, Any], None] = sync_col_users.find_one( jav_user: Dict[str, Any] | None = sync_col_users.find_one({"user": self.id})
{"user": self.id}
)
if jav_user is None: if jav_user is None:
raise UserNotFoundError(user=user, user_id=self.id) raise UserNotFoundError(user=user, user_id=self.id)
self.db_id: ObjectId = jav_user["_id"] self.db_id: ObjectId = jav_user["_id"]
self.customrole: Union[int, None] = jav_user["customrole"] self.customrole: int | None = jav_user["customrole"]
self.customchannel: Union[int, None] = jav_user["customchannel"] self.customchannel: int | None = jav_user["customchannel"]
self.warnings: int = self.warns() self.warnings: int = self.warns()
def warns(self) -> int: def warns(self) -> int:
@ -43,9 +41,7 @@ class HoloUser:
### Returns: ### Returns:
* `int`: Number of warnings * `int`: Number of warnings
""" """
warns: Union[Dict[str, Any], None] = sync_col_warnings.find_one( warns: Dict[str, Any] | None = sync_col_warnings.find_one({"user": self.id})
{"user": self.id}
)
return 0 if warns is None else warns["warns"] return 0 if warns is None else warns["warns"]
@ -55,9 +51,7 @@ class HoloUser:
### Args: ### Args:
* `count` (int, optional): Count of warnings to be added. Defaults to 1. * `count` (int, optional): Count of warnings to be added. Defaults to 1.
""" """
warns: Union[Dict[str, Any], None] = await col_warnings.find_one( warns: Dict[str, Any] | None = await col_warnings.find_one({"user": self.id})
{"user": self.id}
)
if warns is not None: if warns is not None:
await col_warnings.update_one( await col_warnings.update_one(
@ -88,11 +82,11 @@ class HoloUser:
logger.info("Set attribute %s of user %s to %s", key, self.id, value) logger.info("Set attribute %s of user %s to %s", key, self.id, value)
@staticmethod @staticmethod
async def is_moderator(member: Union[User, Member]) -> bool: async def is_moderator(member: User | Member) -> bool:
"""Check if user is moderator or council member """Check if user is moderator or council member
### Args: ### Args:
* `member` (Union[User, Member]): Member object * `member` (User | Member): Member object
### Returns: ### Returns:
`bool`: `True` if member is a moderator or member of council and `False` if not `bool`: `True` if member is a moderator or member of council and `False` if not
@ -100,8 +94,8 @@ class HoloUser:
if isinstance(member, User): if isinstance(member, User):
return False return False
moderator_role: Union[int, None] = await config_get("moderators", "roles") moderator_role: int | None = await config_get("moderators", "roles")
council_role: Union[int, None] = await config_get("council", "roles") council_role: int | None = await config_get("council", "roles")
for role in member.roles: for role in member.roles:
if role.id in (moderator_role, council_role): if role.id in (moderator_role, council_role):
@ -110,11 +104,11 @@ class HoloUser:
return False return False
@staticmethod @staticmethod
async def is_council(member: Union[User, Member]) -> bool: async def is_council(member: User | Member) -> bool:
"""Check if user is a member of council """Check if user is a member of council
### Args: ### Args:
* `member` (Union[User, Member]): Member object * `member` (User | Member): Member object
### Returns: ### Returns:
`bool`: `True` if member is a member of council and `False` if not `bool`: `True` if member is a member of council and `False` if not

View File

@ -1,6 +1,5 @@
import logging import logging
import sys import sys
from typing import Union
from discord import ( from discord import (
ApplicationContext, ApplicationContext,
@ -160,10 +159,10 @@ class Admin(commands.Cog):
) )
) )
mod_role: Union[Role, None] = ds_utils.get( mod_role: Role | None = ds_utils.get(
ctx.user.guild.roles, id=await config_get("moderators", "roles") ctx.user.guild.roles, id=await config_get("moderators", "roles")
) )
admin_chan: Union[TextChannel, None] = ds_utils.get( admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels, ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"), id=await config_get("adminchat", "channels", "text"),
) )
@ -216,10 +215,10 @@ class Admin(commands.Cog):
) )
) )
mod_role: Union[Role, None] = ds_utils.get( mod_role: Role | None = ds_utils.get(
ctx.user.guild.roles, id=await config_get("moderators", "roles") ctx.user.guild.roles, id=await config_get("moderators", "roles")
) )
admin_chan: Union[TextChannel, None] = ds_utils.get( admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels, ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"), id=await config_get("adminchat", "channels", "text"),
) )

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import Any, Dict, Union from typing import Any, Dict
from discord import ApplicationContext, Embed, option, TextChannel, Role from discord import ApplicationContext, Embed, option, TextChannel, Role
from discord import utils as ds_utils from discord import utils as ds_utils
@ -108,9 +108,7 @@ class CustomChannels(commands.Cog):
bots: Dict[str, Any] = await config_get("bots") bots: Dict[str, Any] = await config_get("bots")
for bot in bots: for bot in bots:
role: Union[Role, None] = ds_utils.get( role: Role | None = ds_utils.get(ctx.user.guild.roles, id=bots[bot]["role"])
ctx.user.guild.roles, id=bots[bot]["role"]
)
if role is not None: if role is not None:
await created_channel.set_permissions( await created_channel.set_permissions(
@ -131,7 +129,7 @@ class CustomChannels(commands.Cog):
) -> None: ) -> None:
holo_user_ctx: HoloUser = HoloUser(ctx.user) holo_user_ctx: HoloUser = HoloUser(ctx.user)
custom_channel: Union[TextChannel, None] = ds_utils.get( custom_channel: TextChannel | None = ds_utils.get(
ctx.guild.channels, id=holo_user_ctx.customchannel ctx.guild.channels, id=holo_user_ctx.customchannel
) )
@ -188,7 +186,7 @@ class CustomChannels(commands.Cog):
await ctx.defer() await ctx.defer()
custom_channel: Union[TextChannel, None] = ds_utils.get( custom_channel: TextChannel | None = ds_utils.get(
ctx.guild.channels, id=holo_user_ctx.customchannel ctx.guild.channels, id=holo_user_ctx.customchannel
) )

View File

@ -2,7 +2,7 @@ import logging
from logging import Logger from logging import Logger
from os import makedirs from os import makedirs
from pathlib import Path from pathlib import Path
from typing import Union, List, Dict, Any from typing import List, Dict, Any
from uuid import uuid4 from uuid import uuid4
from discord import ApplicationContext, Embed, File, option, Role, TextChannel from discord import ApplicationContext, Embed, File, option, Role, TextChannel
@ -54,10 +54,10 @@ class Data(commands.Cog):
) )
) )
mod_role: Union[Role, None] = ds_utils.get( mod_role: Role | None = ds_utils.get(
ctx.user.guild.roles, id=await config_get("moderators", "roles") ctx.user.guild.roles, id=await config_get("moderators", "roles")
) )
admin_chan: Union[TextChannel, None] = ds_utils.get( admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels, ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"), id=await config_get("adminchat", "channels", "text"),
) )
@ -126,10 +126,10 @@ class Data(commands.Cog):
) )
) )
mod_role: Union[Role, None] = ds_utils.get( mod_role: Role | None = ds_utils.get(
ctx.user.guild.roles, id=await config_get("moderators", "roles") ctx.user.guild.roles, id=await config_get("moderators", "roles")
) )
admin_chan: Union[TextChannel, None] = ds_utils.get( admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels, ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"), id=await config_get("adminchat", "channels", "text"),
) )

View File

@ -1,4 +1,4 @@
from typing import Dict, Any, Union from typing import Dict, Any
from discord import Member, Message, TextChannel from discord import Member, Message, TextChannel
from discord import utils as ds_utils from discord import utils as ds_utils
@ -33,11 +33,11 @@ class Logger(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_join(self, member: Member) -> None: async def on_member_join(self, member: Member) -> None:
welcome_chan: Union[TextChannel, None] = ds_utils.get( welcome_chan: TextChannel | None = ds_utils.get(
self.client.get_guild(await config_get("guild")).channels, self.client.get_guild(await config_get("guild")).channels,
id=await config_get("welcome", "channels", "text"), id=await config_get("welcome", "channels", "text"),
) )
rules_chan: Union[TextChannel, None] = ds_utils.get( rules_chan: TextChannel | None = ds_utils.get(
self.client.get_guild(await config_get("guild")).channels, self.client.get_guild(await config_get("guild")).channels,
id=await config_get("rules", "channels", "text"), id=await config_get("rules", "channels", "text"),
) )

View File

@ -1,9 +1,7 @@
from typing import Union
from discord import Member, User from discord import Member, User
def guild_name(member: Union[Member, User]) -> str: def guild_name(member: Member | User) -> str:
if isinstance(member, User): if isinstance(member, User):
return member.name return member.name