Added PyCharm support to base module
This commit is contained in:
parent
291d9ad6d6
commit
0c142f51cc
@ -1,5 +1,5 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Union
|
from typing import Union, Optional
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
@ -78,19 +78,19 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
def _get_config(self, g_id: int) -> BaseSettings:
|
def _get_config(self, g_id: int) -> BaseSettings:
|
||||||
return self._config.get_configuration(f'{type(self).__name__}_{g_id}')
|
return self._config.get_configuration(f'{type(self).__name__}_{g_id}')
|
||||||
|
|
||||||
def _apppend_received_message_count(self, g_id: int):
|
def _append_received_message_count(self, g_id: int):
|
||||||
try:
|
try:
|
||||||
self._clients.apppend_received_message_count(self._bot.user.id, g_id, 1)
|
self._clients.append_received_message_count(self._bot.user.id, g_id, 1)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}')
|
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e)
|
||||||
|
|
||||||
def _apppend_deleted_message_count(self, g_id: int):
|
def _append_deleted_message_count(self, g_id: int):
|
||||||
try:
|
try:
|
||||||
self._clients.apppend_received_message_count(self._bot.user.id, g_id, 1)
|
self._clients.append_received_message_count(self._bot.user.id, g_id, 1)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}')
|
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e)
|
||||||
|
|
||||||
def _check_for_known_user(self, member: Union[discord.User, discord.Member]):
|
def _check_for_known_user(self, member: Union[discord.User, discord.Member]):
|
||||||
self._logger.debug(__name__, f'Check if user is already known {member}')
|
self._logger.debug(__name__, f'Check if user is already known {member}')
|
||||||
@ -154,7 +154,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._logger.error(__name__, f'Cannot get user {member.id}', e)
|
self._logger.error(__name__, f'Cannot get user {member.id}', e)
|
||||||
|
|
||||||
def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, server: Server):
|
def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, server: Server):
|
||||||
user: User = None
|
user: Optional[User] = None
|
||||||
try:
|
try:
|
||||||
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
|
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -198,7 +198,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._logger.error(__name__, f'Cannot get server {message.guild.id}', e)
|
self._logger.error(__name__, f'Cannot get server {message.guild.id}', e)
|
||||||
return
|
return
|
||||||
|
|
||||||
user: User = None
|
user: Optional[User] = None
|
||||||
try:
|
try:
|
||||||
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
|
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -211,7 +211,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
|
|
||||||
settings: BaseSettings = self._get_config(message.guild.id)
|
settings: BaseSettings = self._get_config(message.guild.id)
|
||||||
old_xp = user.xp
|
old_xp = user.xp
|
||||||
user.xp += settings._xp_per_message
|
user.xp += settings.xp_per_message
|
||||||
self._users.update_user(user)
|
self._users.update_user(user)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._logger.debug(__name__, f'Module {type(self)} started')
|
self._logger.debug(__name__, f'Module {type(self)} started')
|
||||||
if message is None or message.guild is None:
|
if message is None or message.guild is None:
|
||||||
return
|
return
|
||||||
self._apppend_received_message_count(message.guild.id)
|
self._append_received_message_count(message.guild.id)
|
||||||
|
|
||||||
if not message.author.bot:
|
if not message.author.bot:
|
||||||
self._handle_message_for_xp(message)
|
self._handle_message_for_xp(message)
|
||||||
@ -238,34 +238,33 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):
|
async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):
|
||||||
self._logger.debug(__name__, f'Module {type(self)} started')
|
self._logger.debug(__name__, f'Module {type(self)} started')
|
||||||
self._logger.trace(__name__, f'Detected on_voice_state_update {member.id} from {before} to {after}')
|
self._logger.trace(__name__, f'Detected on_voice_state_update {member.id} from {before} to {after}')
|
||||||
u: discord.User = member
|
|
||||||
settings: BaseSettings = self._get_config(member.guild.id)
|
settings: BaseSettings = self._get_config(member.guild.id)
|
||||||
server = self._servers.get_server_by_discord_id(member.guild.id)
|
server = self._servers.get_server_by_discord_id(member.guild.id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# join
|
# join
|
||||||
if before.channel is None and after.channel is not None and after.channel.id not in settings.afk_channel_ids:
|
if before.channel is None and after.channel is not None and after.channel.id not in settings.afk_channel_ids:
|
||||||
self._logger.trace(__name__, f'User {u.id} joined {after.channel}')
|
self._logger.trace(__name__, f'User {member.id} joined {after.channel}')
|
||||||
self._update_voice_state(True, member.id, after.channel.id, server)
|
self._update_voice_state(True, member.id, after.channel.id, server)
|
||||||
|
|
||||||
# leave
|
# leave
|
||||||
elif before.channel is not None and after.channel is None and before.channel.id not in settings.afk_channel_ids:
|
elif before.channel is not None and after.channel is None and before.channel.id not in settings.afk_channel_ids:
|
||||||
self._logger.trace(__name__, f'User {u.id} left {before.channel}')
|
self._logger.trace(__name__, f'User {member.id} left {before.channel}')
|
||||||
self._update_voice_state(False, member.id, before.channel.id, server)
|
self._update_voice_state(False, member.id, before.channel.id, server)
|
||||||
|
|
||||||
# channel to channel
|
# channel to channel
|
||||||
elif before.channel is not None and after.channel is not None:
|
elif before.channel is not None and after.channel is not None:
|
||||||
# joined
|
# joined
|
||||||
if before.channel.id in settings.afk_channel_ids and after.channel.id not in settings.afk_channel_ids:
|
if before.channel.id in settings.afk_channel_ids and after.channel.id not in settings.afk_channel_ids:
|
||||||
self._logger.trace(__name__, f'User {u.id} joined {after.channel}')
|
self._logger.trace(__name__, f'User {member.id} joined {after.channel}')
|
||||||
self._update_voice_state(True, member.id, after.channel.id, server)
|
self._update_voice_state(True, member.id, after.channel.id, server)
|
||||||
|
|
||||||
# left
|
# left
|
||||||
elif after.channel.id in settings.afk_channel_ids and before.channel.id not in settings.afk_channel_ids:
|
elif after.channel.id in settings.afk_channel_ids and before.channel.id not in settings.afk_channel_ids:
|
||||||
self._logger.trace(__name__, f'User {u.id} left {before.channel}')
|
self._logger.trace(__name__, f'User {member.id} left {before.channel}')
|
||||||
self._update_voice_state(False, member.id, before.channel.id, server)
|
self._update_voice_state(False, member.id, before.channel.id, server)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._logger.trace(__name__, f'User {u.id} switched to {after.channel}')
|
self._logger.trace(__name__, f'User {member.id} switched to {after.channel}')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot handle voice state for user {u.id}', e)
|
self._logger.error(__name__, f'Cannot handle voice state for user {member.id}', e)
|
||||||
|
@ -78,8 +78,8 @@ class BaseSettings(ConfigurationModelABC):
|
|||||||
self._max_voice_state_hours = int(settings['MaxVoiceStateHours'])
|
self._max_voice_state_hours = int(settings['MaxVoiceStateHours'])
|
||||||
self._xp_per_message = int(settings['XpPerMessage'])
|
self._xp_per_message = int(settings['XpPerMessage'])
|
||||||
self._xp_per_ontime_hour = int(settings['XpPerOntimeHour'])
|
self._xp_per_ontime_hour = int(settings['XpPerOntimeHour'])
|
||||||
for id in settings['AFKChannelIds']:
|
for index in settings['AFKChannelIds']:
|
||||||
self._afk_channel_ids.append(int(id))
|
self._afk_channel_ids.append(int(index))
|
||||||
self._purge_message = settings['PurgeMessage']
|
self._purge_message = settings['PurgeMessage']
|
||||||
self._no_permissions_message = settings['NoPermissionsMessage']
|
self._no_permissions_message = settings['NoPermissionsMessage']
|
||||||
self._afk_command_channel_id = settings['AFKCommandChannelId']
|
self._afk_command_channel_id = settings['AFKCommandChannelId']
|
||||||
|
@ -53,7 +53,7 @@ class BaseCommandService(CommandABC):
|
|||||||
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
|
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
|
||||||
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}')
|
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}')
|
||||||
|
|
||||||
if (not self._permissions.is_member_moderator(ctx.author)):
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
await self._message_service.send_ctx_msg(ctx, settings.no_permissions_message)
|
await self._message_service.send_ctx_msg(ctx, settings.no_permissions_message)
|
||||||
self._logger.trace(__name__, f'Finished purge command')
|
self._logger.trace(__name__, f'Finished purge command')
|
||||||
return
|
return
|
||||||
|
@ -32,7 +32,7 @@ class PermissionServiceABC(ABC):
|
|||||||
def get_moderators(self, g_id: int) -> list[discord.Member]: pass
|
def get_moderators(self, g_id: int) -> list[discord.Member]: pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def is_member_admin(member: discord.Member) -> bool: pass
|
def is_member_admin(self, member: discord.Member) -> bool: pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def is_member_moderator(member: discord.Member) -> bool: pass
|
def is_member_moderator(self, member: discord.Member) -> bool: pass
|
||||||
|
Reference in New Issue
Block a user