Working on #13
This commit is contained in:
@@ -3,25 +3,20 @@ from typing import Any, Union
|
||||
|
||||
import discord
|
||||
import discord.member
|
||||
from libbot import config_get
|
||||
|
||||
from modules.database import col_users, col_warnings
|
||||
from modules.utils import config_get
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class NotEnoughMoneyError(Exception):
|
||||
"""User does not have enough money to do that"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class UserNotFoundError(Exception):
|
||||
"""HoloUser could not find user with such an ID in database"""
|
||||
|
||||
def __init__(self, user, user_id):
|
||||
self.user = user
|
||||
self.user_id = user_id
|
||||
|
||||
super().__init__(
|
||||
f"User of type {type(self.user)} with id {self.user_id} was not found"
|
||||
)
|
||||
@@ -63,10 +58,8 @@ class HoloUser:
|
||||
* `int`: Number of warnings
|
||||
"""
|
||||
warns = col_warnings.find_one({"user": self.id})
|
||||
if warns == None:
|
||||
return 0
|
||||
else:
|
||||
return warns["warns"]
|
||||
|
||||
return 0 if warns is None else warns["warns"]
|
||||
|
||||
def warn(self, count=1, reason: str = "Not provided") -> None:
|
||||
"""Warn and add count to warns number
|
||||
@@ -75,13 +68,15 @@ class HoloUser:
|
||||
* `count` (int, optional): Count of warnings to be added. Defaults to 1.
|
||||
"""
|
||||
warns = col_warnings.find_one({"user": self.id})
|
||||
if warns != None:
|
||||
|
||||
if warns is not None:
|
||||
col_warnings.update_one(
|
||||
filter={"_id": self.db_id},
|
||||
update={"$set": {"warns": warns["warns"] + count}},
|
||||
)
|
||||
else:
|
||||
col_warnings.insert_one(document={"user": self.id, "warns": count})
|
||||
|
||||
logger.info(f"User {self.id} was warned {count} times due to: {reason}")
|
||||
|
||||
def set(self, key: str, value: Any) -> None:
|
||||
@@ -93,10 +88,12 @@ class HoloUser:
|
||||
"""
|
||||
if not hasattr(self, key):
|
||||
raise AttributeError()
|
||||
|
||||
setattr(self, key, value)
|
||||
col_users.update_one(
|
||||
filter={"_id": self.db_id}, update={"$set": {key: value}}, upsert=True
|
||||
)
|
||||
|
||||
logger.info(f"Set attribute {key} of user {self.id} to {value}")
|
||||
|
||||
async def is_moderator(
|
||||
@@ -112,11 +109,14 @@ class HoloUser:
|
||||
"""
|
||||
if isinstance(member, discord.User):
|
||||
return False
|
||||
|
||||
moderator_role = await config_get("moderators", "roles")
|
||||
council_role = await config_get("council", "roles")
|
||||
|
||||
for role in member.roles:
|
||||
if role.id == moderator_role or role.id == council_role:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
async def is_council(
|
||||
@@ -132,10 +132,13 @@ class HoloUser:
|
||||
"""
|
||||
if isinstance(member, discord.User):
|
||||
return False
|
||||
|
||||
council_role = await config_get("council", "roles")
|
||||
|
||||
for role in member.roles:
|
||||
if role.id == council_role:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
# def purge(self) -> None:
|
||||
|
Reference in New Issue
Block a user