Added purge command

This commit is contained in:
Sven Heidemann 2021-12-31 11:52:37 +01:00
parent ed2dab6ff8
commit ab44aa55bc
3 changed files with 27 additions and 1 deletions

View File

@ -9,6 +9,7 @@
"AFKChannelIds": [ "AFKChannelIds": [
910199452915093593, 910199452915093593,
910199452915093594 910199452915093594
] ],
"PurgeMessage": "Ja mein Herr, ich lösche alle Nachrichten!"
} }
} }

View File

@ -16,6 +16,7 @@ class BaseSettings(ConfigurationModelABC):
self._xp_per_message: int = 0 self._xp_per_message: int = 0
self._xp_per_ontime_hour: int = 0 self._xp_per_ontime_hour: int = 0
self._afk_channel_ids: list[int] = [] self._afk_channel_ids: list[int] = []
self._purge_message: str = ''
@property @property
def welcome_message(self) -> str: def welcome_message(self) -> str:
@ -45,6 +46,10 @@ class BaseSettings(ConfigurationModelABC):
def afk_channel_ids(self) -> list[int]: def afk_channel_ids(self) -> list[int]:
return self._afk_channel_ids return self._afk_channel_ids
@property
def purge_message(self) -> str:
return self._purge_message
def from_dict(self, settings: dict): def from_dict(self, settings: dict):
try: try:
self._welcome_message = settings['WelcomeMessage'] self._welcome_message = settings['WelcomeMessage']
@ -55,6 +60,7 @@ class BaseSettings(ConfigurationModelABC):
self._xp_per_ontime_hour = int(settings['XpPerOntimeHour']) self._xp_per_ontime_hour = int(settings['XpPerOntimeHour'])
for id in settings['AFKChannelIds']: for id in settings['AFKChannelIds']:
self._afk_channel_ids.append(int(id)) self._afk_channel_ids.append(int(id))
self._purge_message = settings['PurgeMessage']
except Exception as e: except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -1,10 +1,15 @@
import asyncio
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Context from discord.ext.commands import Context
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_core.configuration import ConfigurationABC
from gismo_core.abc.command_abc import CommandABC from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from modules.base.base_settings import BaseSettings
class BaseCommandService(CommandABC): class BaseCommandService(CommandABC):
@ -12,11 +17,13 @@ class BaseCommandService(CommandABC):
def __init__( def __init__(
self, self,
logger: LoggerABC, logger: LoggerABC,
config: ConfigurationABC,
message_service: MessageServiceABC message_service: MessageServiceABC
): ):
CommandABC.__init__(self) CommandABC.__init__(self)
self._logger = logger self._logger = logger
self._config = config
self._message_service = message_service self._message_service = message_service
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@ -25,3 +32,15 @@ class BaseCommandService(CommandABC):
async def ping(self, ctx: Context): async def ping(self, ctx: Context):
self._logger.debug(__name__, f'Received command ping {ctx}') self._logger.debug(__name__, f'Received command ping {ctx}')
await self._message_service.send_ctx_msg(ctx, 'Pong') await self._message_service.send_ctx_msg(ctx, 'Pong')
self._logger.trace(__name__, f'Finished ping command')
@commands.command()
async def purge(self, ctx: Context):
self._logger.debug(__name__, f'Received command purge {ctx}')
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}')
await self._message_service.send_ctx_msg(ctx, settings.purge_message)
await asyncio.sleep(server_settings.message_delete_timer)
await ctx.channel.purge()
self._logger.trace(__name__, f'Finished purge command')