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
from typing import Any, Union, Dict
from typing import Any, Dict
from bson import ObjectId
from discord import User, Member
@ -12,11 +12,11 @@ logger = logging.getLogger(__name__)
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
### 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:
* `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
jav_user: Union[Dict[str, Any], None] = sync_col_users.find_one(
{"user": self.id}
)
jav_user: Dict[str, Any] | None = sync_col_users.find_one({"user": self.id})
if jav_user is None:
raise UserNotFoundError(user=user, user_id=self.id)
self.db_id: ObjectId = jav_user["_id"]
self.customrole: Union[int, None] = jav_user["customrole"]
self.customchannel: Union[int, None] = jav_user["customchannel"]
self.customrole: int | None = jav_user["customrole"]
self.customchannel: int | None = jav_user["customchannel"]
self.warnings: int = self.warns()
def warns(self) -> int:
@ -43,9 +41,7 @@ class HoloUser:
### Returns:
* `int`: Number of warnings
"""
warns: Union[Dict[str, Any], None] = sync_col_warnings.find_one(
{"user": self.id}
)
warns: Dict[str, Any] | None = sync_col_warnings.find_one({"user": self.id})
return 0 if warns is None else warns["warns"]
@ -55,9 +51,7 @@ class HoloUser:
### Args:
* `count` (int, optional): Count of warnings to be added. Defaults to 1.
"""
warns: Union[Dict[str, Any], None] = await col_warnings.find_one(
{"user": self.id}
)
warns: Dict[str, Any] | None = await col_warnings.find_one({"user": self.id})
if warns is not None:
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)
@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
### Args:
* `member` (Union[User, Member]): Member object
* `member` (User | Member): Member object
### Returns:
`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):
return False
moderator_role: Union[int, None] = await config_get("moderators", "roles")
council_role: Union[int, None] = await config_get("council", "roles")
moderator_role: int | None = await config_get("moderators", "roles")
council_role: int | None = await config_get("council", "roles")
for role in member.roles:
if role.id in (moderator_role, council_role):
@ -110,11 +104,11 @@ class HoloUser:
return False
@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
### Args:
* `member` (Union[User, Member]): Member object
* `member` (User | Member): Member object
### Returns:
`bool`: `True` if member is a member of council and `False` if not

View File

@ -1,6 +1,5 @@
import logging
import sys
from typing import Union
from discord import (
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")
)
admin_chan: Union[TextChannel, None] = ds_utils.get(
admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels,
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")
)
admin_chan: Union[TextChannel, None] = ds_utils.get(
admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"),
)

View File

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

View File

@ -2,7 +2,7 @@ import logging
from logging import Logger
from os import makedirs
from pathlib import Path
from typing import Union, List, Dict, Any
from typing import List, Dict, Any
from uuid import uuid4
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")
)
admin_chan: Union[TextChannel, None] = ds_utils.get(
admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels,
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")
)
admin_chan: Union[TextChannel, None] = ds_utils.get(
admin_chan: TextChannel | None = ds_utils.get(
ctx.user.guild.channels,
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 utils as ds_utils
@ -33,11 +33,11 @@ class Logger(commands.Cog):
@commands.Cog.listener()
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,
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,
id=await config_get("rules", "channels", "text"),
)

View File

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