Merge pull request 'Rollen per Reaktion hinzufügen (#54)' (#58) from #54 into master

Reviewed-on: sh-edraft.de/kd_discord_bot#58
Reviewed-by: edraft-dev <dev.sven.heidemann@sh-edraft.de>
Reviewed-by: Ebola-Chan <nick.jungmann@gmail.com>
Closes #54
This commit is contained in:
Sven Heidemann 2022-10-05 19:08:22 +02:00
commit 359a614a1a
69 changed files with 1227 additions and 110 deletions

View File

@ -6,6 +6,7 @@
"bot-core": "src/bot_core/bot-core.json",
"bot-data": "src/bot_data/bot-data.json",
"admin": "src/modules/admin/admin.json",
"auto-role": "src/modules/auto_role/auto-role.json",
"base": "src/modules/base/base.json",
"boot-log": "src/modules/boot_log/boot-log.json",
"database": "src/modules/database/database.json",

View File

@ -15,7 +15,7 @@ __title__ = 'bot'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "0",
"Minor": "2",
"Micro": "1"
"Micro": "2"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",
@ -16,13 +16,13 @@
"LicenseName": "MIT",
"LicenseDescription": "MIT, see LICENSE for more details.",
"Dependencies": [
"cpl-core>=2022.10.0",
"cpl-translation>=2022.10.0",
"cpl-query>=2022.10.0",
"cpl-discord>=2022.10.0"
"cpl-core==2022.10.0.post3",
"cpl-translation==2022.10.0",
"cpl-query==2022.10.0",
"cpl-discord==2022.10.0.post5"
],
"DevDependencies": [
"cpl-cli>=2022.10.0"
"cpl-cli==2022.10.0"
],
"PythonVersion": ">=3.10.4",
"PythonPath": {
@ -48,11 +48,13 @@
"../bot_core/bot-core.json",
"../bot_data/bot-data.json",
"../modules/base/base.json",
"../modules/admin/admin.json",
"../modules/auto_role/auto-role.json",
"../modules/base/base.json",
"../modules/boot_log/boot-log.json",
"../modules/database/database.json",
"../modules/permission/permission.json",
"../modules/admin/admin.json",
"../modules/moderator/moderator.json"
"../modules/moderator/moderator.json",
"../modules/permission/permission.json"
]
}
}

View File

@ -2,7 +2,7 @@
"LoggingSettings": {
"Path": "logs/",
"Filename": "bot.log",
"ConsoleLogLevel": "DEBUG",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
},
"BotLoggingSettings": {

View File

@ -1,6 +1,7 @@
{
"FeatureFlags": {
"AdminModule": true,
"AutoRoleModule": true,
"BaseModule": true,
"BootLogModule": true,
"CoreModule": true,

View File

@ -4,6 +4,7 @@ from bot_core.core_extension.core_extension_module import CoreExtensionModule
from bot_core.core_module import CoreModule
from bot_data.data_module import DataModule
from modules.admin.admin_module import AdminModule
from modules.auto_role.auto_role_module import AutoRoleModule
from modules.base.base_module import BaseModule
from modules.boot_log.boot_log_module import BootLogModule
from modules.database.database_module import DatabaseModule
@ -20,10 +21,12 @@ class ModuleList:
CoreModule, # has to be first!
DataModule,
AdminModule,
AutoRoleModule,
BaseModule,
DatabaseModule,
ModeratorModule,
PermissionModule,
# has to be last!
BootLogModule,
CoreExtensionModule,
BootLogModule # has to be last!
])

View File

@ -1,31 +1,8 @@
from typing import Optional
from cpl_core.application import StartupExtensionABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironmentABC
from cpl_discord import get_discord_collection
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from modules.admin.command.restart_command import RestartCommand
from modules.admin.command.shutdown_command import ShutdownCommand
from modules.base.command.afk_command import AFKCommand
from modules.base.command.help_command import HelpCommand
from modules.base.command.info_command import InfoCommand
from modules.base.command.ping_command import PingCommand
from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent
from modules.base.events.base_on_command_event import BaseOnCommandEvent
from modules.moderator.command.purge_command import PurgeCommand
from modules.base.command.user_info_command import UserInfoCommand
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_event import BaseOnMessageEvent
from modules.base.events.base_on_voice_state_update_event import BaseOnVoiceStateUpdateEvent
from modules.boot_log.boot_log_on_ready_event import BootLogOnReadyEvent
from modules.database.database_on_ready_event import DatabaseOnReadyEvent
from modules.permission.events.permission_on_member_update_event import PermissionOnMemberUpdateEvent
from modules.permission.events.permission_on_ready_event import PermissionOnReadyEvent
class StartupDiscordExtension(StartupExtensionABC):

View File

@ -4,6 +4,7 @@ from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironmentABC
from bot_data.abc.migration_abc import MigrationABC
from bot_data.migration.auto_role_migration import AutoRoleMigration
from bot_data.migration.initial_migration import InitialMigration
from bot_data.service.migration_service import MigrationService
@ -19,3 +20,4 @@ class StartupMigrationExtension(StartupExtensionABC):
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_transient(MigrationService)
services.add_transient(MigrationABC, InitialMigration)
services.add_transient(MigrationABC, AutoRoleMigration) # 03.10.2022 #54 - 0.2.2

View File

@ -53,6 +53,57 @@
"shutdown_message": "Trauert nicht um mich, es war eine logische Entscheidung. Das Wohl von Vielen, es wiegt schwerer als das Wohl von Wenigen oder eines Einzelnen. Ich war es und ich werde es immer sein, Euer Freund. Lebt lange und in Frieden :)",
"deploy_message": "Der neue Stand wurde hochgeladen."
},
"auto_role": {
"list": {
"title": "Beobachtete Nachrichten:",
"description": "Von auto-role beobachtete Nachrichten:",
"auto_role_id": "auto-role Id",
"message_id": "Nachricht-Id"
},
"add": {
"success": "auto-role für die Nachricht {} wurde hinzugefügt :D",
"error": {
"not_found": "Nachricht {} in {} nicht gefunden!",
"already_exists": "auto-role für die Nachricht {} existiert bereits!"
}
},
"remove": {
"success": "auto-role {} wurde entfernt :D",
"error": {
"not_found": "auto-role {} nicht gefunden!"
}
},
"error": {
"nothing_found": "Keine auto-role Einträge gefunden."
},
"rule": {
"list": {
"title": "auto-role Regeln:",
"description": "Von auto-role angewendete Regeln:",
"auto_role_rule_id": "auto-role Regel Id",
"emoji": "Emoji",
"role": "Rolle"
},
"add": {
"success": "Regel {} -> {} für auto-role {} wurde hinzugefügt :D",
"error": {
"not_found": "Regel für auto-role {} nicht gefunden!",
"emoji_not_found": "Emoji {} für auto-role Regel {} nicht gefunden!",
"rule_not_found": "Rolle {} für auto-role Regel {} nicht gefunden!",
"already_exists": "Regel für auto-role {} existiert bereits!"
}
},
"remove": {
"success": "Regel für auto-role {} wurde entfernt :D",
"error": {
"not_found": "Regel für auto-role {} nicht gefunden!"
}
},
"error": {
"id_not_found": "Kein auto-role Eintrag mit der Id gefunden!"
}
}
},
"moderator": {
"purge_message": "Na gut..., ich lösche alle Nachrichten wenns sein muss."
},

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.abc'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.configuration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -5,6 +5,7 @@ class FeatureFlagsEnum(Enum):
# modules
admin_module = 'AdminModule'
auto_role_module = 'AutoRoleModule'
base_module = 'BaseModule'
boot_log_module = 'BootLogModule'
core_module = 'CoreModule'

View File

@ -15,6 +15,7 @@ class FeatureFlagsSettings(ConfigurationModelABC):
self._flags = {
# modules
FeatureFlagsEnum.admin_module.value: False, # 02.10.2022 #48
FeatureFlagsEnum.auto_role_module.value: True, # 03.10.2022 #54
FeatureFlagsEnum.base_module.value: True, # 02.10.2022 #48
FeatureFlagsEnum.boot_log_module.value: True, # 02.10.2022 #48
FeatureFlagsEnum.core_module.value: True, # 03.10.2022 #56

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.core_extension'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.events'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.helper'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.logging'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.pipes'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_core.service'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_data'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'bot_data.abc'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,58 @@
from abc import ABC, abstractmethod
from typing import Optional
from cpl_query.extension import List
from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule
class AutoRoleRepositoryABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def get_auto_roles(self) -> List[AutoRole]: pass
@abstractmethod
def get_auto_role_by_id(self, id: int) -> AutoRole: pass
@abstractmethod
def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: pass
@abstractmethod
def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass
@abstractmethod
def get_auto_role_by_message_id(self, id: int) -> AutoRole: pass
@abstractmethod
def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]: pass
@abstractmethod
def add_auto_role(self, auto_role: AutoRole): pass
@abstractmethod
def update_auto_role(self, auto_role: AutoRole): pass
@abstractmethod
def delete_auto_role(self, auto_role: AutoRole): pass
@abstractmethod
def get_auto_role_rules(self) -> List[AutoRoleRule]: pass
@abstractmethod
def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule: pass
@abstractmethod
def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: pass
@abstractmethod
def add_auto_role_rule(self, auto_role: AutoRoleRule): pass
@abstractmethod
def update_auto_role_rule(self, auto_role: AutoRoleRule): pass
@abstractmethod
def delete_auto_role_rule(self, auto_role: AutoRoleRule): pass

View File

@ -2,6 +2,7 @@ from abc import ABC, abstractmethod
class MigrationABC(ABC):
name = None
@abstractmethod
def __init__(self): pass

View File

@ -5,12 +5,14 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.client_repository_abc import ClientRepositoryABC
from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService
from bot_data.service.client_repository_service import ClientRepositoryService
from bot_data.service.known_user_repository_service import KnownUserRepositoryService
from bot_data.service.server_repository_service import ServerRepositoryService
@ -33,4 +35,5 @@ class DataModule(ModuleABC):
services.add_transient(ClientRepositoryABC, ClientRepositoryService)
services.add_transient(KnownUserRepositoryABC, KnownUserRepositoryService)
services.add_transient(UserJoinedServerRepositoryABC, UserJoinedServerRepositoryService)
services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService)
services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService)
services.add_transient(AutoRoleRepositoryABC, AutoRoleRepositoryService)

View File

@ -15,7 +15,7 @@ __title__ = 'bot_data.migration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,47 @@
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
from bot_data.db_context import DBContext
class AutoRoleMigration(MigrationABC):
name = '0.2.1_AutoRoleMigration'
def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self)
self._logger = logger
self._db = db
self._cursor = db.cursor
def upgrade(self):
self._logger.debug(__name__, 'Running upgrade')
self._cursor.execute(
str(f"""
CREATE TABLE IF NOT EXISTS `AutoRoles` (
`AutoRoleId` BIGINT NOT NULL AUTO_INCREMENT,
`ServerId` BIGINT,
`DiscordMessageId` BIGINT NOT NULL,
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
PRIMARY KEY(`AutoRoleId`)
);
""")
)
self._cursor.execute(
str(f"""
CREATE TABLE IF NOT EXISTS `AutoRoleRules` (
`AutoRoleRuleId` BIGINT NOT NULL AUTO_INCREMENT,
`AutoRoleId` BIGINT,
`DiscordEmojiName` VARCHAR(64),
`DiscordRoleId` BIGINT NOT NULL,
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
PRIMARY KEY(`AutoRoleRuleId`)
);
""")
)
def downgrade(self):
self._cursor.execute('DROP TABLE `AutoRole`;')
self._cursor.execute('DROP TABLE `AutoRoleRules`;')

View File

@ -4,8 +4,10 @@ from bot_data.db_context import DBContext
class InitialMigration(MigrationABC):
name = '0.1_InitialMigration'
def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self)
self._logger = logger
self._db = db
self._cursor = db.cursor

View File

@ -15,7 +15,7 @@ __title__ = 'bot_data.model'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,85 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
class AutoRole(TableABC):
def __init__(self, server_id: int, dc_message_id: int, created_at: datetime=None, modified_at: datetime=None, id=0):
self._auto_role_id = id
self._server_id = server_id
self._discord_message_id = dc_message_id
TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at
self._modified_at = modified_at if modified_at is not None else self._modified_at
@property
def auto_role_id(self) -> int:
return self._auto_role_id
@property
def server_id(self) -> int:
return self._server_id
@property
def discord_message_id(self) -> int:
return self._discord_message_id
@staticmethod
def get_select_all_string() -> str:
return str(f"""
SELECT * FROM `AutoRoles`;
""")
@staticmethod
def get_select_by_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AutoRoles`
WHERE `AutoRoleId` = {id};
""")
@staticmethod
def get_select_by_server_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AutoRoles`
WHERE `ServerId` = {id};
""")
@staticmethod
def get_select_by_message_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AutoRoles`
WHERE `DiscordMessageId` = {id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `AutoRoles` (
`ServerId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._server_id},
{self._discord_message_id},
'{self._created_at}',
'{self._modified_at}'
);
""")
@property
def udpate_string(self) -> str:
return str(f"""
UPDATE `AutoRoles`
SET `ServerId` = {self._server_id},
`DiscordMessageId` = {self._discord_message_id},
`LastModifiedAt` = '{self._modified_at}'
WHERE `AutoRoleId` = {self._auto_role_id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `AutoRoles`
WHERE `AutoRoleId` = {self._auto_role_id};
""")

View File

@ -0,0 +1,85 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
class AutoRoleRule(TableABC):
def __init__(self, auto_role_id: int, discord_emoji_name: str, discord_role_id: int, created_at: datetime=None, modified_at: datetime=None, id=0):
self._auto_role_rule_id = id
self._auto_role_id = auto_role_id
self._discord_emoji_name = discord_emoji_name
self._discord_role_id = discord_role_id
TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at
self._modified_at = modified_at if modified_at is not None else self._modified_at
@property
def auto_role_rule_id(self) -> int:
return self._auto_role_rule_id
@property
def auto_role_id(self) -> int:
return self._auto_role_id
@property
def emoji_name(self) -> str:
return self._discord_emoji_name
@property
def role_id(self) -> int:
return self._discord_role_id
@staticmethod
def get_select_all_string() -> str:
return str(f"""
SELECT * FROM `AutoRoleRules`;
""")
@staticmethod
def get_select_by_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AutoRoleRules`
WHERE `AutoRoleRuleId` = {id};
""")
@staticmethod
def get_select_by_auto_role_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AutoRoleRules`
WHERE `AutoRoleId` = {id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `AutoRoleRules` (
`AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._auto_role_id},
'{self._discord_emoji_name}',
{self._discord_role_id},
'{self._created_at}',
'{self._modified_at}'
);
""")
@property
def udpate_string(self) -> str:
return str(f"""
UPDATE `AutoRoleRules`
SET `AutoRoleId` = {self._auto_role_id},
`DiscordEmojiName` = {self._discord_emoji_name},
`DiscordRoleId` = {self._discord_role_id},
`LastModifiedAt` = '{self._modified_at}'
WHERE `AutoRoleRuleId` = {self._auto_role_rule_id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `AutoRoleRules`
WHERE `AutoRoleRuleId` = {self._auto_role_rule_id};
""")

View File

@ -60,12 +60,12 @@ class Server(TableABC):
UPDATE `Servers`
SET `DiscordServerId` = {self._discord_server_id},
`LastModifiedAt` = '{self._modified_at}'
WHERE `Id` = {self._server_id};
WHERE `ServerId` = {self._server_id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `Servers`
WHERE `Id` = {self._server_id};
WHERE `ServerId` = {self._server_id};
""")

View File

@ -15,7 +15,7 @@ __title__ = 'bot_data.service'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,170 @@
from typing import Optional
from cpl_core.database.context import DatabaseContextABC
from cpl_query.extension import List
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule
class AutoRoleRepositoryService(AutoRoleRepositoryABC):
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC):
self._logger = logger
self._context = db_context
AutoRoleRepositoryABC.__init__(self)
def get_auto_roles(self) -> List[AutoRole]:
auto_roles = List(AutoRole)
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_all_string()}')
results = self._context.select(AutoRole.get_select_all_string())
for result in results:
auto_roles.append(AutoRole(
result[1],
result[2],
result[3],
result[4],
id=result[0]
))
return auto_roles
def get_auto_role_by_id(self, id: int) -> AutoRole:
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_id_string(id)}')
result = self._context.select(AutoRole.get_select_by_id_string(id))[0]
return AutoRole(
result[1],
result[2],
result[3],
result[4],
id=result[0]
)
def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]:
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_id_string(id)}')
result = self._context.select(AutoRole.get_select_by_id_string(id))
if result is None or len(result) == 0:
return None
result = result[0]
return AutoRole(
result[1],
result[2],
result[3],
result[4],
id=result[0]
)
def get_auto_roles_by_server_id(self, id: int) -> List[AutoRole]:
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}')
auto_roles = List(AutoRole)
results = self._context.select(AutoRole.get_select_by_server_id_string(id))
for result in results:
auto_roles.append(AutoRole(
result[1],
result[2],
result[3],
result[4],
id=result[0]
))
return auto_roles
def get_auto_role_by_message_id(self, id: int) -> AutoRole:
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_message_id_string(id)}')
result = self._context.select(AutoRole.get_select_by_message_id_string(id))[0]
return AutoRole(
result[1],
result[2],
result[3],
result[4],
id=result[0]
)
def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]:
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_message_id_string(id)}')
result = self._context.select(AutoRole.get_select_by_message_id_string(id))
if result is None or len(result) == 0:
return None
result = result[0]
return AutoRole(
result[1],
result[2],
result[3],
result[4],
id=result[0]
)
def add_auto_role(self, auto_role: AutoRole):
self._logger.trace(__name__, f'Send SQL command: {auto_role.insert_string}')
self._context.cursor.execute(auto_role.insert_string)
def update_auto_role(self, auto_role: AutoRole):
self._logger.trace(__name__, f'Send SQL command: {auto_role.udpate_string}')
self._context.cursor.execute(auto_role.udpate_string)
def delete_auto_role(self, auto_role: AutoRole):
self._logger.trace(__name__, f'Send SQL command: {auto_role.delete_string}')
self._context.cursor.execute(auto_role.delete_string)
def get_auto_role_rules(self) -> List[AutoRoleRule]:
auto_role_rules = List(AutoRoleRule)
self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_all_string()}')
results = self._context.select(AutoRoleRule.get_select_all_string())
for result in results:
auto_role_rules.append(AutoRoleRule(
result[1],
result[2],
result[3],
result[4],
result[5],
id=result[0]
))
return auto_role_rules
def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule:
self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_id_string(id)}')
result = self._context.select(AutoRoleRule.get_select_by_id_string(id))[0]
return AutoRoleRule(
result[1],
result[2],
result[3],
result[4],
result[5],
id=result[0]
)
def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]:
auto_role_rules = List(AutoRoleRule)
self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_auto_role_id_string(id)}')
results = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id))
for result in results:
auto_role_rules.append(AutoRoleRule(
result[1],
result[2],
result[3],
result[4],
result[5],
id=result[0]
))
return auto_role_rules
def add_auto_role_rule(self, auto_role_rule: AutoRoleRule):
self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.insert_string}')
self._context.cursor.execute(auto_role_rule.insert_string)
def update_auto_role_rule(self, auto_role_rule: AutoRoleRule):
self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.udpate_string}')
self._context.cursor.execute(auto_role_rule.udpate_string)
def delete_auto_role_rule(self, auto_role_rule: AutoRoleRule):
self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}')
self._context.cursor.execute(auto_role_rule.delete_string)

View File

@ -2,6 +2,7 @@ from typing import Type
from cpl_core.database.context import DatabaseContextABC
from cpl_core.dependency_injection import ServiceProviderABC
from cpl_query.extension import List
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
@ -17,7 +18,7 @@ class MigrationService:
self._db = db
self._cursor = db.cursor
self._migrations: list[Type[MigrationABC]] = MigrationABC.__subclasses__()
self._migrations = List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name)
def migrate(self):
self._logger.info(__name__, f"Running Migrations")

View File

@ -15,7 +15,7 @@ __title__ = 'modules.admin'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.admin.command'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
bot Keksdose bot
~~~~~~~~~~~~~~~~~~~
Discord bot for the Keksdose discord Server
:copyright: (c) 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'modules.auto_role'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,46 @@
{
"ProjectSettings": {
"Name": "auto-role",
"Version": {
"Major": "0",
"Minor": "0",
"Micro": "0"
},
"Author": "",
"AuthorEmail": "",
"Description": "",
"LongDescription": "",
"URL": "",
"CopyrightDate": "",
"CopyrightName": "",
"LicenseName": "",
"LicenseDescription": "",
"Dependencies": [
"cpl-core>=2022.10.0.post5"
],
"DevDependencies": [
"cpl-cli>=2022.10.0"
],
"PythonVersion": ">=3.10.4",
"PythonPath": {
"linux": ""
},
"Classifiers": []
},
"BuildSettings": {
"ProjectType": "library",
"SourcePath": "",
"OutputPath": "../../dist",
"Main": "admin.main",
"EntryPoint": "admin",
"IncludePackageData": false,
"Included": [],
"Excluded": [
"*/__pycache__",
"*/logs",
"*/tests"
],
"PackageData": {},
"ProjectReferences": []
}
}

View File

@ -0,0 +1,29 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironmentABC
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.auto_role.command.auto_role_group import AutoRoleGroup
from modules.auto_role.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent
from modules.auto_role.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent
from modules.auto_role.helper.reaction_handler import ReactionHandler
class AutoRoleModule(ModuleABC):
def __init__(self, dc: DiscordCollectionABC):
ModuleABC.__init__(self, dc, FeatureFlagsEnum.auto_role_module)
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
pass
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_transient(ReactionHandler)
# commands
self._dc.add_command(AutoRoleGroup)
# events
self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent)
self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent)

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
bot Keksdose bot
~~~~~~~~~~~~~~~~~~~
Discord bot for the Keksdose discord Server
:copyright: (c) 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'modules.auto_role.command'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,304 @@
from typing import List as TList, Optional
import discord
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.container import TextChannel
from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class AutoRoleGroup(DiscordCommandABC):
def __init__(
self,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC,
translate: TranslatePipe,
servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC,
db_context: DatabaseContextABC,
permission_service: PermissionServiceABC,
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._t = translate
self._servers = servers
self._auto_roles = auto_roles
self._db_context = db_context
self._permissions = permission_service
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.hybrid_group(name="auto-role")
@commands.guild_only()
async def auto_role(self, ctx: Context):
pass
@auto_role.command(alias='auto-roles')
@commands.guild_only()
async def list(self, ctx: Context, wait: int = None):
self._logger.debug(__name__, f'Received command auto-role list {ctx}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command auto-role list')
return
embed = discord.Embed(
title=self._t.transform('modules.auto_role.list.title'),
description=self._t.transform('modules.auto_role.list.description'),
color=int('ef9d0d', 16)
)
auto_roles = self._auto_roles.get_auto_roles()
if auto_roles.count() < 1:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.error.nothing_found'))
self._logger.trace(__name__, f'Finished command auto-role list')
return
auto_role_id = ''
message_id = ''
for auto_role in auto_roles:
auto_role_id += f'\n{auto_role.auto_role_id}'
message_id += f'\n{auto_role.discord_message_id}'
embed.add_field(name=self._t.transform('modules.auto_role.list.auto_role_id'), value=auto_role_id, inline=True)
embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True)
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
self._logger.trace(__name__, f'Finished command auto-role list')
@auto_role.command()
@commands.guild_only()
async def add(self, ctx: Context, channel: discord.TextChannel, message_id: str):
self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command auto-role add')
return
message = List(discord.Message, [message async for message in channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default()
if message is None:
self._logger.debug(__name__, f'Message with id {message_id} not found in {channel.name}')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id, channel.name))
self._logger.trace(__name__, f'Finished command auto-role add')
return
if self._auto_roles.find_auto_role_by_message_id(int(message_id)) is not None:
self._logger.debug(__name__, f'auto-role for message {message_id} already exists')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(message_id))
self._logger.trace(__name__, f'Finished command auto-role add')
return
server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id
self._auto_roles.add_auto_role(AutoRole(server_id, int(message_id)))
self._db_context.save_changes()
self._logger.info(__name__, f'Saved auto-role for message {message_id} at server {server_id}')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.success').format(message_id))
self._logger.trace(__name__, f'Finished command auto-role add')
@add.autocomplete('message_id')
async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id)
try:
channel_from_data = interaction.data['options'][0]['options'][0]['value']
found_channel = discord.utils.get(interaction.guild.text_channels, id=int(channel_from_data))
if found_channel is not None:
channel = found_channel
finally:
pass
messages = [message async for message in channel.history(limit=10)]
return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)]
@auto_role.command()
@commands.guild_only()
async def remove(self, ctx: Context, auto_role: int):
self._logger.debug(__name__, f'Received command auto-role remove {ctx} {auto_role}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command auto-role remove')
return
auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role)
if auto_role_from_db is None:
self._logger.debug(__name__, f'auto-role {auto_role} not found')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role))
self._logger.trace(__name__, f'Finished command auto-role remove')
return
for rule in self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_from_db.auto_role_id):
self._auto_roles.delete_auto_role_rule(rule)
self._logger.info(__name__, f'Removed auto-role rule {rule.role_id}')
self._auto_roles.delete_auto_role(auto_role_from_db)
self._db_context.save_changes()
self._logger.info(__name__, f'Removed auto-role {auto_role}')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.success').format(auto_role))
self._logger.trace(__name__, f'Finished command auto-role remove')
@remove.autocomplete('auto_role')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id)
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
@auto_role.group()
@commands.guild_only()
async def rule(self, ctx: Context):
pass
@rule.command(alias='rules')
@commands.guild_only()
async def list(self, ctx: Context, auto_role: int, wait: int = None):
self._logger.debug(__name__, f'Received command auto-role rule list {ctx}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command auto-role rule list')
return
embed = discord.Embed(
title=self._t.transform('modules.auto_role.list.title'),
description=self._t.transform('modules.auto_role.list.description'),
color=int('ef9d0d', 16)
)
rules = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role)
if rules.count() < 1:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.error.id_not_found'))
self._logger.trace(__name__, f'Finished command auto-role rule list')
return
auto_role_rule_id = ''
emoji = ''
role = ''
for rule in rules:
auto_role_rule_id += f'\n{rule.role_id}'
emoji += f'\n{rule.emoji_name}'
role += f'\n{ctx.guild.get_role(rule.role_id)}'
embed.add_field(name=self._t.transform('modules.auto_role.rule.list.auto_role_rule_id'), value=auto_role_rule_id, inline=True)
embed.add_field(name=self._t.transform('modules.auto_role.rule.list.emoji'), value=emoji, inline=True)
embed.add_field(name=self._t.transform('modules.auto_role.rule.list.role'), value=role, inline=True)
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
self._logger.trace(__name__, f'Finished command auto-role rule list')
@list.autocomplete('auto_role')
async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id)
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
@rule.command()
@commands.guild_only()
async def add(self, ctx: Context, auto_role: int, emoji_name: str, role_id: str):
self._logger.debug(__name__, f'Received command auto-role add {ctx} {auto_role}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command auto-role rule add')
return
emoji = discord.utils.get(self._bot.emojis, name=emoji_name)
if emoji is None:
self._logger.debug(__name__, f'auto-role rule add emoji {emoji_name} not found')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.add.error.emoji_not_found').format(emoji_name, auto_role))
self._logger.trace(__name__, f'Finished command auto-role add')
return
role = ctx.guild.get_role(int(role_id))
if role is None:
self._logger.debug(__name__, f'auto-role rule add role {role_id} not found')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.add.error.role_not_found').format(role_id, auto_role))
self._logger.trace(__name__, f'Finished command auto-role add')
return
auto_role_from_db = self._auto_roles.get_auto_role_by_id(auto_role)
if auto_role_from_db is None:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role))
self._logger.trace(__name__, f'Finished command auto-role rule add')
return
if self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role).where(lambda r: r.emoji_name == emoji.name and int(role_id) == role.id).count() > 0:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(auto_role))
self._logger.trace(__name__, f'Finished command auto-role rule add')
return
self._auto_roles.add_auto_role_rule(AutoRoleRule(auto_role, emoji_name, int(role_id)))
self._db_context.save_changes()
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.add.success').format(emoji, role.name, auto_role))
self._logger.trace(__name__, f'Finished command auto-role rule add')
@add.autocomplete('auto_role')
async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id)
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
@add.autocomplete('emoji_name')
async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
emojis = interaction.guild.emojis
return [app_commands.Choice(name=emoji.name, value=emoji.name) for emoji in emojis]
@add.autocomplete('role_id')
async def rule_add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
roles = interaction.guild.roles
return [app_commands.Choice(name=role.name, value=str(role.id)) for role in roles]
@rule.command()
@commands.guild_only()
async def remove(self, ctx: Context, auto_role_rule: int):
self._logger.debug(__name__, f'Received command auto-role remove {ctx} {auto_role_rule}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command auto-role remove')
return
auto_role_from_db = self._auto_roles.get_auto_role_rule_by_id(auto_role_rule)
if auto_role_from_db is None:
self._logger.debug(__name__, f'auto-role rule {auto_role_rule} not found')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role_rule))
self._logger.trace(__name__, f'Finished command auto-role rule remove')
return
self._auto_roles.delete_auto_role_rule(auto_role_from_db)
self._db_context.save_changes()
self._logger.info(__name__, f'Removed auto-role rule {auto_role_rule}')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.remove.success').format(auto_role_rule))
self._logger.trace(__name__, f'Finished command auto-role remove')
@remove.autocomplete('auto_role_rule')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
rules = self._auto_roles.get_auto_role_rules()
return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in
rules]

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
bot Keksdose bot
~~~~~~~~~~~~~~~~~~~
Discord bot for the Keksdose discord Server
:copyright: (c) 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'modules.auto_role.events'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,34 @@
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC
from cpl_discord.service import DiscordBotServiceABC
from discord import RawReactionActionEvent
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from modules.auto_role.helper.reaction_handler import ReactionHandler
class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC):
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC,
servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC,
reaction_handler: ReactionHandler
):
OnRawReactionAddABC.__init__(self)
self._logger = logger
self._bot = bot
self._servers = servers
self._auto_roles = auto_roles
self._reaction_handler = reaction_handler
async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
self._logger.debug(__name__, f'Module {type(self)} started')
await self._reaction_handler.handle(payload, 'add')
self._logger.debug(__name__, f'Module {type(self)} stopped')

View File

@ -0,0 +1,34 @@
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC
from cpl_discord.service import DiscordBotServiceABC
from discord import RawReactionActionEvent
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from modules.auto_role.helper.reaction_handler import ReactionHandler
class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC,
servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC,
reaction_handler: ReactionHandler
):
OnRawReactionRemoveABC.__init__(self)
self._logger = logger
self._bot = bot
self._servers = servers
self._auto_roles = auto_roles
self._reaction_handler = reaction_handler
async def on_raw_reaction_remove(self, payload: RawReactionActionEvent):
self._logger.debug(__name__, f'Module {type(self)} started')
await self._reaction_handler.handle(payload, 'remove')
self._logger.debug(__name__, f'Module {type(self)} stopped')

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
bot Keksdose bot
~~~~~~~~~~~~~~~~~~~
Discord bot for the Keksdose discord Server
:copyright: (c) 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'modules.auto_role.helper'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -0,0 +1,66 @@
import discord
from cpl_core.logging import LoggerABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from discord import RawReactionActionEvent
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule
class ReactionHandler:
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC,
servers: ServerRepositoryABC,
auto_roles: AutoRoleRepositoryABC
):
self._logger = logger
self._bot = bot
self._servers = servers
self._auto_roles = auto_roles
self._message_ids = self._auto_roles.get_auto_roles().select(lambda x: x.discord_message_id)
self._roles = self._auto_roles.get_auto_roles()
async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None:
self._logger.trace(__name__, f'Handle reaction {payload} {r_type}')
if payload.message_id not in self._message_ids:
self._logger.debug(__name__, f'Message not in auto-roles - skipping')
return
guild = self._bot.get_guild(payload.guild_id)
user = await guild.fetch_member(payload.user_id)
if user.bot:
self._logger.debug(__name__, f'User is bot - skipping')
return
emoji = payload.emoji.name
auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id == payload.message_id).single_or_default()
if auto_role is None:
self._logger.debug(__name__, f'auto-role for message not found - skipping')
return
rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id)
if rules.count() == 0:
self._logger.debug(__name__, f'auto-role rules not found - skipping')
return
for rule in rules:
if emoji != rule.emoji_name:
continue
if r_type == 'add':
role = guild.get_role(rule.role_id)
self._logger.debug(__name__, f'Assign role {role.name} to {user.name}')
await user.add_roles(role)
elif r_type == 'remove':
role = guild.get_role(rule.role_id)
self._logger.debug(__name__, f'Remove role {role.name} to {user.name}')
await user.remove_roles(role)
else:
self._logger.warn(__name__, f'Invalid reaction type {r_type}')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.base'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.base.abc'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -11,7 +11,6 @@ from modules.base.command.afk_command import AFKCommand
from modules.base.command.help_command import HelpCommand
from modules.base.command.info_command import InfoCommand
from modules.base.command.ping_command import PingCommand
from modules.base.command.user_info_command import UserInfoCommand
from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent
from modules.base.events.base_on_command_event import BaseOnCommandEvent
from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent
@ -36,7 +35,6 @@ class BaseModule(ModuleABC):
self._dc.add_command(HelpCommand)
self._dc.add_command(InfoCommand)
self._dc.add_command(PingCommand)
self._dc.add_command(UserInfoCommand)
# events
self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent)
self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent)

View File

@ -15,7 +15,7 @@ __title__ = 'modules.base.command'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -1,6 +1,10 @@
from typing import List
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
@ -41,3 +45,8 @@ class HelpCommand(DiscordCommandABC):
is_persistent = persistent_flag == '--stay'
await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent)
self._logger.trace(__name__, f'Finished help command')
@help.autocomplete('persistent_flag')
async def help_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
flags = ['--stay']
return [app_commands.Choice(name=key, value=key) for key in flags]

View File

@ -62,7 +62,7 @@ class InfoCommand(DiscordCommandABC):
embed.add_field(name=self._t.transform('modules.base.info.fields.moved_users_count'), value=client.moved_users_count)
from bot.module_list import ModuleList
modules = ModuleList.get_modules()
modules = modules.select(lambda x: x.replace('Module', ''))
modules = modules.select(lambda x: x.__name__.replace('Module', ''))
embed.add_field(name=self._t.transform('modules.base.info.fields.modules'), value='\n'.join(modules), inline=False)
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)

View File

@ -15,7 +15,7 @@ __title__ = 'modules.base.configuration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.base.events'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -48,9 +48,9 @@ class BaseOnCommandEvent(OnCommandABC):
self._clients = clients
self._servers = servers
def _append_received_message_count(self, g_id: int):
def _append_received_command_count(self, g_id: int):
try:
self._clients.append_received_message_count(self._bot.user.id, g_id, 1)
self._clients.append_received_command_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)
@ -87,4 +87,4 @@ class BaseOnCommandEvent(OnCommandABC):
self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}')
if ctx is None or ctx.guild is None:
return
self._append_received_message_count(ctx.guild.id)
self._append_received_command_count(ctx.guild.id)

View File

@ -15,7 +15,7 @@ __title__ = 'modules.base.service'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.boot_log'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.boot_log.configuration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.database'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.moderator'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.moderator.command'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -6,6 +6,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.moderator.command.purge_command import PurgeCommand
from modules.moderator.command.user_info_command import UserInfoCommand
class ModeratorModule(ModuleABC):
@ -19,4 +20,5 @@ class ModeratorModule(ModuleABC):
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
# commands
self._dc.add_command(PurgeCommand)
self._dc.add_command(UserInfoCommand)
# events

View File

@ -15,7 +15,7 @@ __title__ = 'modules.permission'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.permission.abc'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.permission.configuration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.permission.events'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')

View File

@ -15,7 +15,7 @@ __title__ = 'modules.permission.service'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '0.2.1'
__version__ = '0.2.2'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='2', micro='1')
version_info = VersionInfo(major='0', minor='2', micro='2')