1.1.0.rc3 #343

Merged
edraft merged 5 commits from 1.1.0.rc3 into 1.1.0 2023-08-16 22:13:42 +02:00
17 changed files with 291 additions and 33 deletions

View File

@ -30,7 +30,8 @@
"requests-oauthlib==1.3.1",
"icmplib==3.0.3",
"ariadne==0.20.1",
"cryptography==41.0.2"
"cryptography==41.0.2",
"discord>=2.3.2"
],
"DevDependencies": [
"cpl-cli==2023.4.0.post3",

View File

@ -43,7 +43,7 @@ class ServerConfigMutation(QueryABC):
if "id" not in input:
raise ValueError("Id not set")
server_config = self._server_configs.get_server_config_by_server(input["id"])
server_config = self._server_configs.get_server_config_by_id(int(input["id"]))
self._can_user_mutate_data(Route.get_user().users[0].server, UserRoleEnum.technician)
server_config.message_delete_timer = (
@ -137,7 +137,7 @@ class ServerConfigMutation(QueryABC):
return server_config
def _update_afk_channel_ids(self, new_config: ServerConfig):
old_config = self._server_configs.get_server_config_by_id(new_config.server.id)
old_config = self._server_configs.get_server_config_by_server(new_config.server.id)
for channel_id in old_config.afk_channel_ids:
if channel_id in new_config.afk_channel_ids:
continue
@ -155,7 +155,7 @@ class ServerConfigMutation(QueryABC):
)
def _update_team_role_ids(self, new_config: ServerConfig):
old_config = self._server_configs.get_server_config_by_id(new_config.server.id)
old_config = self._server_configs.get_server_config_by_server(new_config.server.id)
for role_id in old_config.team_role_ids:
if role_id.role_id in new_config.team_role_ids.select(lambda x: int(x.role_id)):
continue

View File

@ -40,7 +40,7 @@ class ClientQuery(DataQueryWithHistoryABC):
@staticmethod
def resolve_received_message_count(client: Client, *_):
return client.received_command_count
return client.received_message_count
@staticmethod
def resolve_deleted_message_count(client: Client, *_):

View File

@ -10,6 +10,9 @@ from modules.achievements.achievement_attribute_resolver import AchievementAttri
from modules.achievements.achievement_service import AchievementService
from modules.achievements.commands.achievements_group import AchievementGroup
from modules.achievements.events.achievement_on_message_event import AchievementOnMessageEvent
from modules.achievements.events.achievement_on_reaction_add_event import AchievementOnReactionAddEvent
from modules.achievements.events.achievement_on_reaction_remove_event import AchievementOnReactionRemoveEvent
from modules.achievements.events.achievement_on_voice_state_update_event import AchievementOnVoiceStateUpdateEvent
class AchievementsModule(ModuleABC):
@ -26,3 +29,6 @@ class AchievementsModule(ModuleABC):
self._dc.add_command(AchievementGroup)
self._dc.add_event(DiscordEventTypesEnum.on_message.value, AchievementOnMessageEvent)
self._dc.add_event(DiscordEventTypesEnum.on_reaction_add.value, AchievementOnReactionAddEvent)
self._dc.add_event(DiscordEventTypesEnum.on_reaction_remove.value, AchievementOnReactionRemoveEvent)
self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, AchievementOnVoiceStateUpdateEvent)

View File

@ -0,0 +1,61 @@
from typing import Union
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnReactionAddABC
from cpl_discord.service import DiscordBotServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.achievements.achievement_service import AchievementService
class AchievementOnReactionAddEvent(OnReactionAddABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
achievements: AchievementService,
db: DatabaseContextABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
):
OnReactionAddABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._achievements = achievements
self._db = db
self._servers = servers
self._users = users
async def on_reaction_add(self, reaction: discord.reaction.Reaction, user: Union[discord.Member, discord.User]):
if not isinstance(user, discord.Member):
return
if user.guild is None:
return
if user.bot:
return
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
server = self._servers.get_server_by_discord_id(user.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(user.author.id, server.id)
user.reaction_count += 1
self._db.save_changes()
await self._achievements.validate_achievements_for_user(user)

View File

@ -0,0 +1,61 @@
from typing import Union
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnReactionRemoveABC
from cpl_discord.service import DiscordBotServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.achievements.achievement_service import AchievementService
class AchievementOnReactionRemoveEvent(OnReactionRemoveABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
achievements: AchievementService,
db: DatabaseContextABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
):
OnReactionRemoveABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._achievements = achievements
self._db = db
self._servers = servers
self._users = users
async def on_reaction_remove(self, reaction: discord.reaction.Reaction, user: Union[discord.Member, discord.User]):
if not isinstance(user, discord.Member):
return
if user.guild is None:
return
if user.bot:
return
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
server = self._servers.get_server_by_discord_id(user.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(user.author.id, server.id)
user.reaction_count -= 1
self._db.save_changes()
await self._achievements.validate_achievements_for_user(user)

View File

@ -0,0 +1,55 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnVoiceStateUpdateABC
from cpl_discord.service import DiscordBotServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.achievements.achievement_service import AchievementService
class AchievementOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
achievements: AchievementService,
db: DatabaseContextABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
):
OnVoiceStateUpdateABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._achievements = achievements
self._db = db
self._servers = servers
self._users = users
async def on_voice_state_update(
self, member: discord.member.Member, before: discord.member.VoiceState, after: discord.member.VoiceState
):
if member.guild is None:
return
if member.author.bot:
return
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
server = self._servers.get_server_by_discord_id(member.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(member.author.id, server.id)
await self._achievements.validate_achievements_for_user(user)

View File

@ -1,3 +1,4 @@
from cpl_core.dependency_injection import ServiceProviderABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
@ -10,15 +11,12 @@ from modules.base.forms.complaint_form import ComplaintForm
class SubmitGroup(DiscordCommandABC):
def __init__(
self, logger: LoggerABC, bot: DiscordBotServiceABC, complaint_form: ComplaintForm, bug_form: BugReportForm
):
def __init__(self, services: ServiceProviderABC, logger: LoggerABC, bot: DiscordBotServiceABC):
DiscordCommandABC.__init__(self)
self._services = services
self._logger = logger
self._bot = bot
self._complaint_form = complaint_form
self._bug_form = bug_form
@commands.hybrid_group()
@commands.guild_only()
@ -30,14 +28,15 @@ class SubmitGroup(DiscordCommandABC):
@CommandChecks.check_is_ready()
async def complaint(self, ctx: Context):
self._logger.debug(__name__, f"Received command complaint {ctx}")
await ctx.interaction.response.send_modal(self._complaint_form)
complaint_form: ComplaintForm = self._services.get_service(ComplaintForm)
await ctx.interaction.response.send_modal(complaint_form)
self._logger.trace(__name__, f"Finished command complaint {ctx}")
pass
@submit.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
async def bug_report(self, ctx: Context):
self._logger.debug(__name__, f"Received command complaint {ctx}")
await ctx.interaction.response.send_modal(self._bug_form)
bug_form: BugReportForm = self._services.get_service(BugReportForm)
await ctx.interaction.response.send_modal(bug_form)
self._logger.trace(__name__, f"Finished command complaint {ctx}")

View File

@ -11,6 +11,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.model.server_config import ServerConfig
from bot_data.model.technician_config import TechnicianConfig
class BootLogOnReadyEvent(OnReadyABC):
@ -21,6 +22,7 @@ class BootLogOnReadyEvent(OnReadyABC):
bot: DiscordBotServiceABC,
message_service: MessageServiceABC,
translate: TranslatePipe,
tech_config: TechnicianConfig,
):
OnReadyABC.__init__(self)
self._config = config
@ -29,11 +31,17 @@ class BootLogOnReadyEvent(OnReadyABC):
self._bot = bot
self._message_service = message_service
self._t = translate
self._tech_config = tech_config
self._logger.info(__name__, f"Module {type(self)} loaded")
async def on_ready(self):
self._logger.debug(__name__, f"Module {type(self)} started")
if not FeatureFlagsSettings.get_flag_from_dict(
self._tech_config.feature_flags, FeatureFlagsEnum.boot_log_module
):
return
try:
start_time = self._config.get_configuration("Bot_StartTime")
init_time = round(

View File

@ -1,14 +1,18 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.container import Guild
from cpl_discord.service import DiscordBotServiceABC
from discord import Permissions, Colour
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.data_seeder_abc import DataSeederABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.level import Level
from bot_data.model.server import Server
from bot_data.model.server_config import ServerConfig
from bot_data.service.level_repository_service import LevelRepositoryService
from modules.level.configuration.default_level_settings import DefaultLevelSettings
from modules.level.service.level_service import LevelService
@ -17,6 +21,7 @@ from modules.level.service.level_service import LevelService
class LevelSeeder(DataSeederABC):
def __init__(
self,
config: ConfigurationABC,
logger: DatabaseLogger,
levels: DefaultLevelSettings,
level_repo: LevelRepositoryService,
@ -27,6 +32,7 @@ class LevelSeeder(DataSeederABC):
):
DataSeederABC.__init__(self)
self._config = config
self._logger = logger
self._levels = level_repo
self._servers = servers
@ -65,6 +71,10 @@ class LevelSeeder(DataSeederABC):
async def seed(self):
# create levels
for guild in self._bot.guilds:
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.level_module):
continue
created_default = False
if guild.roles.where(lambda r: r.name == self._level_header).first_or_default() is None:
await guild.create_role(name=self._level_header)

View File

@ -1,17 +1,29 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnMemberUpdateABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.model.server_config import ServerConfig
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class PermissionOnMemberUpdateEvent(OnMemberUpdateABC):
def __init__(self, logger: LoggerABC, permission_service: PermissionServiceABC):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, permission_service: PermissionServiceABC):
OnMemberUpdateABC.__init__(self)
self._config = config
self._logger = logger
self._permission_service = permission_service
async def on_member_update(self, before: discord.Member, after: discord.Member):
if before.guild is not None:
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{before.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.permission_module
):
return
self._logger.debug(__name__, f"Module {type(self)} started")
if before.roles != after.roles:

View File

@ -1,15 +1,22 @@
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnReadyABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.model.technician_config import TechnicianConfig
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class PermissionOnReadyEvent(OnReadyABC):
def __init__(self, logger: LoggerABC, permission_service: PermissionServiceABC):
def __init__(self, logger: LoggerABC, permission_service: PermissionServiceABC, tech_config: TechnicianConfig):
OnReadyABC.__init__(self)
self._logger = logger
self._permission_service = permission_service
self._tech_config = tech_config
async def on_ready(self):
self._logger.debug(__name__, f"Module {type(self)} started")
if not FeatureFlagsSettings.get_flag_from_dict(
self._tech_config.feature_flags, FeatureFlagsEnum.permission_module
):
return
self._permission_service.on_ready()

View File

@ -1,6 +1,6 @@
{
"name": "kdb-web",
"version": "1.1.0.rc2",
"version": "1.1.0.rc3",
"scripts": {
"ng": "ng",
"update-version": "ts-node-esm update-version.ts",

View File

@ -25,3 +25,9 @@ export interface UserDTO {
isAdmin: boolean;
isModerator: boolean;
}
export enum MemberRoles {
Moderator = 0,
Admin = 1,
Technician = 2,
}

View File

@ -1,17 +1,20 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { catchError } from 'rxjs/operators';
import { AuthService } from 'src/app/services/auth/auth.service';
import { ThemeService } from 'src/app/services/theme/theme.service';
import { Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from "@angular/router";
import { catchError } from "rxjs/operators";
import { AuthService } from "src/app/services/auth/auth.service";
import { ThemeService } from "src/app/services/theme/theme.service";
import { SidebarService } from "../../../../services/sidebar/sidebar.service";
import { MemberRoles } from "../../../../models/auth/auth-user.dto";
@Injectable({
providedIn: 'root'
providedIn: "root"
})
export class AuthGuard implements CanActivate {
constructor(
private router: Router,
private authService: AuthService,
private themeService: ThemeService
private themeService: ThemeService,
private sidebarService: SidebarService
) {
}
@ -26,19 +29,41 @@ export class AuthGuard implements CanActivate {
return false;
}
const role = route.data['role'];
if (role) {
const role = route.data["role"];
const memberRole = route.data["memberRole"];
if (role !== undefined) {
this.authService.hasUserPermission(role).then(async hasPermission => {
let authUser = await this.authService.getLoggedInUser();
let isTechnician = authUser?.users?.map(u => u.isTechnician).filter(u => u) ?? [];
if (!hasPermission && !isTechnician) {
this.router.navigate(['/dashboard']);
this.router.navigate(["/dashboard"]);
return false;
}
return true;
});
}
if (memberRole !== undefined) {
let userHasAccess = false;
let authUser = await this.authService.getLoggedInUser();
authUser?.users?.forEach(u => {
if (u.server === +(this.sidebarService.server$.value?.id ?? 0)) {
if (
memberRole === MemberRoles.Moderator && u.isModerator ||
memberRole === MemberRoles.Admin && u.isAdmin ||
memberRole === MemberRoles.Technician && u.isTechnician
) {
userHasAccess = true;
}
}
});
if (!userHasAccess) {
this.router.navigate(["/dashboard"]);
return false;
}
return true;
}
return true;
}
}

View File

@ -3,15 +3,22 @@ import { RouterModule, Routes } from "@angular/router";
import { ServerDashboardComponent } from "./server-dashboard/server-dashboard.component";
import { ProfileComponent } from "./profile/profile.component";
import { MembersComponent } from "./members/members.component";
import { AuthGuard } from "../../shared/guards/auth/auth.guard";
import { MemberRoles } from "../../../models/auth/auth-user.dto";
const routes: Routes = [
{ path: "", component: ServerDashboardComponent },
{ path: "members", component: MembersComponent },
{ path: "members", component: MembersComponent, canActivate: [AuthGuard], data: { memberRole: MemberRoles.Moderator } },
{ path: "members/:memberId", component: ProfileComponent },
{ path: "auto-roles", loadChildren: () => import("./auto-role/auto-role.module").then(m => m.AutoRoleModule) },
{ path: "levels", loadChildren: () => import("./levels/levels.module").then(m => m.LevelsModule) },
{ path: "achievements", loadChildren: () => import("./achievements/achievements.module").then(m => m.AchievementsModule) },
{ path: "config", loadChildren: () => import("./config/config.module").then(m => m.ConfigModule) }
{
path: "auto-roles",
loadChildren: () => import("./auto-role/auto-role.module").then(m => m.AutoRoleModule),
canActivate: [AuthGuard],
data: { memberRole: MemberRoles.Moderator }
},
{ path: "levels", loadChildren: () => import("./levels/levels.module").then(m => m.LevelsModule), canActivate: [AuthGuard], data: { memberRole: MemberRoles.Moderator } },
{ path: "achievements", loadChildren: () => import("./achievements/achievements.module").then(m => m.AchievementsModule), data: { memberRole: MemberRoles.Moderator } },
{ path: "config", loadChildren: () => import("./config/config.module").then(m => m.ConfigModule), data: { memberRole: MemberRoles.Admin } }
];
@NgModule({

View File

@ -2,6 +2,6 @@
"WebVersion": {
"Major": "1",
"Minor": "1",
"Micro": "0.rc2"
"Micro": "0.rc3"
}
}