Added support to remove xp on message delete #178
This commit is contained in:
parent
3811cf7d74
commit
c9aeb684d6
@ -19,6 +19,7 @@ from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEv
|
||||
from modules.base.events.base_on_command_event import BaseOnCommandEvent
|
||||
from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent
|
||||
from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent
|
||||
from modules.base.events.base_on_message_delete_event import BaseOnMessageDeleteEvent
|
||||
from modules.base.events.base_on_message_event import BaseOnMessageEvent
|
||||
from modules.base.events.base_on_raw_reaction_add import BaseOnRawReactionAddEvent
|
||||
from modules.base.events.base_on_raw_reaction_remove import BaseOnRawReactionRemoveEvent
|
||||
@ -55,6 +56,7 @@ class BaseModule(ModuleABC):
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent)
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_member_remove.value, BaseOnMemberRemoveEvent)
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_message.value, BaseOnMessageEvent)
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_message_delete.value, BaseOnMessageDeleteEvent)
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, BaseOnRawReactionAddEvent)
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, BaseOnRawReactionRemoveEvent)
|
||||
self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEvent)
|
||||
|
@ -0,0 +1,83 @@
|
||||
from typing import Optional
|
||||
|
||||
import discord
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_discord.events import OnMessageDeleteABC
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
|
||||
from bot_core.helper.log_message_helper import LogMessageHelper
|
||||
from bot_core.logging.message_logger import MessageLogger
|
||||
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.abc.user_repository_abc import UserRepositoryABC
|
||||
from bot_data.model.user import User
|
||||
from modules.base.abc.base_helper_abc import BaseHelperABC
|
||||
from modules.base.configuration.base_server_settings import BaseServerSettings
|
||||
|
||||
|
||||
class BaseOnMessageDeleteEvent(OnMessageDeleteABC):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
logger: MessageLogger,
|
||||
bhs: BaseHelperABC,
|
||||
db: DatabaseContextABC,
|
||||
bot: DiscordBotServiceABC,
|
||||
users: UserRepositoryABC,
|
||||
clients: ClientRepositoryABC,
|
||||
servers: ServerRepositoryABC,
|
||||
):
|
||||
OnMessageDeleteABC.__init__(self)
|
||||
self._logger = logger
|
||||
self._base_helper = bhs
|
||||
self._db = db
|
||||
self._bot = bot
|
||||
self._users = users
|
||||
self._clients = clients
|
||||
self._servers = servers
|
||||
|
||||
def _append_received_message_count(self, g_id: int):
|
||||
try:
|
||||
self._clients.append_deleted_message_count(self._bot.user.id, g_id, 1)
|
||||
self._db.save_changes()
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e)
|
||||
|
||||
def _handle_message_delete(self, message: discord.Message):
|
||||
dc_user_id = message.author.id
|
||||
try:
|
||||
server = self._servers.get_server_by_discord_id(message.guild.id)
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot get server {message.guild.id}', e)
|
||||
return
|
||||
|
||||
user: Optional[User] = None
|
||||
try:
|
||||
user = self._users.find_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot get user {dc_user_id}', e)
|
||||
return
|
||||
|
||||
if user is None:
|
||||
self._logger.error(__name__, f'User not found {dc_user_id}')
|
||||
return
|
||||
|
||||
settings: BaseServerSettings = self._base_helper.get_config(message.guild.id)
|
||||
old_xp = user.xp
|
||||
user.xp -= settings.xp_per_message
|
||||
self._users.update_user(user)
|
||||
self._db.save_changes()
|
||||
|
||||
self._logger.debug(__name__, f'Removed message from user {user}. xp: from {old_xp} to {user.xp}')
|
||||
|
||||
async def on_message_delete(self, message: discord.Message):
|
||||
self._logger.debug(__name__, f'Module {type(self)} started')
|
||||
if message is None or message.guild is None:
|
||||
return
|
||||
|
||||
self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}')
|
||||
self._append_received_message_count(message.guild.id)
|
||||
|
||||
if not message.author.bot:
|
||||
self._handle_message_delete(message)
|
||||
self._logger.debug(__name__, f'Module {type(self)} stopped')
|
@ -83,10 +83,12 @@ class BaseOnMessageEvent(OnMessageABC):
|
||||
@EventChecks.check_is_ready()
|
||||
async def on_message(self, message: discord.Message):
|
||||
self._logger.debug(__name__, f'Module {type(self)} started')
|
||||
self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}')
|
||||
if message is None or message.guild is None:
|
||||
return
|
||||
|
||||
self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}')
|
||||
self._append_received_message_count(message.guild.id)
|
||||
|
||||
if not message.author.bot:
|
||||
self._handle_message_for_xp(message)
|
||||
self._logger.debug(__name__, f'Module {type(self)} stopped')
|
||||
|
Loading…
Reference in New Issue
Block a user