[WIP] Improved auto roles #134

This commit is contained in:
Sven Heidemann 2023-02-19 19:45:24 +01:00
parent e0ca057399
commit ed5564dc7c
15 changed files with 183 additions and 29 deletions

@ -1 +1 @@
Subproject commit eff27f42808909a816c37f31d81f085d59d4728c
Subproject commit 84d05a9c7825ada185c4c0819555fa6b3b0de662

@ -1 +1 @@
Subproject commit c712f856ebe30c71ac0b144045599ed2f91a1cba
Subproject commit 81021418c21d45cee54478ab5fd69f9ce7261644

View File

@ -0,0 +1,29 @@
type Guild {
id: ID
name: String
channels: [Channel]
roles: [Role]
emojis: [Emoji]
}
input GuildFilter {
id: ID
}
type Channel {
id: String
name: String
type: String
}
type Role {
id: String
name: String
}
type Emoji {
id: String
name: String
url: String
}

View File

@ -28,4 +28,6 @@ type Query {
userCount: Int
users(filter: UserFilter, page: Page, sort: Sort): [User]
guilds(filter: GuildFilter): [Guild]
}

View File

@ -0,0 +1,10 @@
from bot_graphql.abc.data_query_abc import DataQueryABC
class ChannelQuery(DataQueryABC):
def __init__(self):
DataQueryABC.__init__(self, "Channel")
self.set_field("id", lambda c, *_: c.id)
self.set_field("name", lambda c, *_: c.name)
self.set_field("type", lambda c, *_: type(c))

View File

@ -0,0 +1,10 @@
from bot_graphql.abc.data_query_abc import DataQueryABC
class RoleQuery(DataQueryABC):
def __init__(self):
DataQueryABC.__init__(self, "Emoji")
self.set_field("id", lambda e, *_: e.id)
self.set_field("name", lambda e, *_: e.name)
self.set_field("url", lambda e, *_: e.url)

View File

@ -0,0 +1,12 @@
from bot_graphql.abc.data_query_abc import DataQueryABC
class GuildQuery(DataQueryABC):
def __init__(self):
DataQueryABC.__init__(self, "Guild")
self.set_field("id", lambda g, *_: g.id)
self.set_field("name", lambda g, *_: g.name)
self.set_field("channels", lambda g, *_: g.channels)
self.set_field("roles", lambda g, *_: g.roles)
self.set_field("emojis", lambda g, *_: g.emojis)

View File

@ -0,0 +1,9 @@
from bot_graphql.abc.data_query_abc import DataQueryABC
class RoleQuery(DataQueryABC):
def __init__(self):
DataQueryABC.__init__(self, "Role")
self.set_field("id", lambda r, *_: r.id)
self.set_field("name", lambda r, *_: r.name)

View File

@ -1,3 +1,5 @@
from cpl_discord.service import DiscordBotServiceABC
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
@ -22,44 +24,46 @@ from bot_graphql.filter.user_joined_voice_channel_filter import UserJoinedVoiceC
class Query(QueryABC):
def __init__(
self,
bot: DiscordBotServiceABC,
auto_roles: AutoRoleRepositoryABC,
clients: ClientRepositoryABC,
known_users: KnownUserRepositoryABC,
levels: LevelRepositoryABC,
servers: ServerRepositoryABC,
user_joined_servers: UserJoinedServerRepositoryABC,
user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC,
user_joined_voice_channels: UserJoinedVoiceChannelRepositoryABC,
user_joined_game_server: UserJoinedGameServerRepositoryABC,
users: UserRepositoryABC,
):
QueryABC.__init__(self, "Query")
self._auto_roles = auto_roles
self._clients = clients
self._known_users = known_users
self._levels = levels
self._servers = servers
self._user_joined_servers = user_joined_servers
self._user_joined_voice_channels = user_joined_voice_channel
self._user_joined_game_server = user_joined_game_server
self._users = users
self.add_collection("autoRole", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter)
self.add_collection("autoRoleRule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter)
self.add_collection("client", lambda *_: self._clients.get_clients(), ClientFilter)
self.add_collection("knownUser", lambda *_: self._known_users.get_users())
self.add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter)
self.add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter)
self._bot = bot
self.add_collection("autoRole", lambda *_: auto_roles.get_auto_roles(), AutoRoleFilter)
self.add_collection("autoRoleRule", lambda *_: auto_roles.get_auto_role_rules(), AutoRoleRuleFilter)
self.add_collection("client", lambda *_: clients.get_clients(), ClientFilter)
self.add_collection("knownUser", lambda *_: known_users.get_users())
self.add_collection("level", lambda *_: levels.get_levels(), LevelFilter)
self.add_collection("server", lambda *_: servers.get_servers(), ServerFilter)
self.add_collection(
"userJoinedServer", lambda *_: self._user_joined_servers.get_user_joined_servers(), UserJoinedServerFilter
"userJoinedServer", lambda *_: user_joined_servers.get_user_joined_servers(), UserJoinedServerFilter
)
self.add_collection(
"userJoinedVoiceChannel",
lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels(),
lambda *_: user_joined_voice_channels.get_user_joined_voice_channels(),
UserJoinedVoiceChannelFilter,
)
self.add_collection(
"userJoinedGameServer",
lambda *_: self._user_joined_game_server.get_user_joined_game_servers(),
lambda *_: user_joined_game_server.get_user_joined_game_servers(),
UserJoinedGameServerFilter,
)
self.add_collection("user", lambda *_: self._users.get_users(), UserFilter)
self.add_collection("user", lambda *_: users.get_users(), UserFilter)
self.set_field("guilds", self._resolve_guilds)
def _resolve_guilds(self, *_, filter=None):
if filter is None and "id" not in filter:
return self._bot.guilds
return self._bot.guilds.where(lambda g: g.id == int(filter["id"]))

View File

@ -0,0 +1,31 @@
export interface Guild {
id?: string;
name?: string;
channels: [Channel]
role: [Role]
emojis: [Emoji]
}
export interface Channel {
id?: string;
name?: string;
type?: ChannelType;
}
export enum ChannelType {
category = "category",
text = "text",
voice = "voice"
}
export interface Role {
id?: string;
name?: string;
}
export interface Emoji {
id?: string;
name?: string;
url?: string;
}

View File

@ -1,5 +1,29 @@
export class Queries {
static guildsQuery = `
query GuildsQuery($id: ID) {
guilds(filter: {id: $id}) {
id
name
channels {
id
name
type
}
roles {
id
name
}
emojis {
id
name
url
}
}
}
`;
static serversQuery = `
query ServerInfo($filter: ServerFilter, $page: Page, $sort: Sort) {
servers(filter: $filter, page: $page, sort: $sort) {

View File

@ -1,12 +1,17 @@
import { Server } from "../data/server.model";
import { User } from "../data/user.model";
import { AutoRole } from "../data/auto_role.model";
import { Guild } from "../data/discord.model";
export interface Query {
serverCount: number;
servers: Server[];
}
export interface SingleDiscordQuery {
guilds: Guild[];
}
export interface UserListQuery {
userCount: number;
users: User[];

View File

@ -63,13 +63,13 @@
</div>
</th>
<th class="table-header-small-dropdown">
<th>
<div class="table-header-label">
<div class="table-header-text">{{'common.created_at' | translate}}</div>
</div>
</th>
<th class="table-header-small-dropdown">
<th>
<div class="table-header-label">
<div class="table-header-text">{{'common.modified_at' | translate}}</div>
</div>
@ -104,8 +104,8 @@
</form>
</th>
<th></th>
<th></th>
<th></th>
<th class="table-header-small-dropdown"></th>
<th class="table-header-small-dropdown"></th>
<th class="table-header-actions"></th>
</tr>
</ng-template>
@ -127,6 +127,7 @@
<p-cellEditor>
<ng-template pTemplate="input">
{{autoRole.channelId}}
<p-dropdown [options]="channels" [(ngModel)]="autoRole.channelName" placeholder="{{'view.server.members.headers.level' | translate}}"></p-dropdown>
</ng-template>
<ng-template pTemplate="output">
{{autoRole.channelId}}

View File

@ -1,6 +1,6 @@
import { Component, OnInit } from "@angular/core";
import { User } from "../../../../../../models/data/user.model";
import { LazyLoadEvent } from "primeng/api";
import { LazyLoadEvent, MenuItem } from "primeng/api";
import { FormBuilder, FormControl, FormGroup } from "@angular/forms";
import { Page } from "../../../../../../models/graphql/filter/page.model";
import { Sort, SortDirection } from "../../../../../../models/graphql/filter/sort.model";
@ -12,7 +12,7 @@ import { TranslateService } from "@ngx-translate/core";
import { DataService } from "../../../../../../services/data/data.service";
import { SidebarService } from "../../../../../../services/sidebar/sidebar.service";
import { ActivatedRoute } from "@angular/router";
import { AutoRoleQuery } from "../../../../../../models/graphql/query.model";
import { AutoRoleQuery, SingleDiscordQuery } from "../../../../../../models/graphql/query.model";
import { Queries } from "../../../../../../models/graphql/queries.model";
import { catchError, debounceTime } from "rxjs/operators";
import { Table } from "primeng/table";
@ -20,6 +20,7 @@ import { UpdateUserMutationResult } from "../../../../../../models/graphql/resul
import { Mutations } from "../../../../../../models/graphql/mutations.model";
import { throwError } from "rxjs";
import { AutoRole, AutoRoleFilter } from "../../../../../../models/data/auto_role.model";
import { ChannelType, Guild } from "../../../../../../models/data/discord.model";
@Component({
selector: "app-auto-roles",
@ -28,6 +29,8 @@ import { AutoRole, AutoRoleFilter } from "../../../../../../models/data/auto_rol
})
export class AutoRolesComponent implements OnInit {
auto_roles!: AutoRole[];
guild!: Guild;
channels!: MenuItem[];
loading = true;
clonedUsers: { [s: string]: User; } = {};
@ -74,6 +77,20 @@ export class AutoRolesComponent implements OnInit {
ngOnInit(): void {
this.data.getServerFromRoute(this.route);
this.spinner.showSpinner();
this.data.query<SingleDiscordQuery>(Queries.guildsQuery, {
filter: {
id: this.sidebar.server$.value?.discordId
}
}
).subscribe(data => {
this.guild = data.guilds[0];
this.channels = this.guild.channels
.filter(x => x.type === ChannelType.text)
.map(x => {return {value: x.name, label: x.id}});
this.spinner.hideSpinner();
});
this.setFilterForm();
this.loadNextPage();
}
@ -96,7 +113,7 @@ export class AutoRolesComponent implements OnInit {
id: new FormControl<number | null>(null),
channelId: new FormControl<string | null>(null),
channelName: new FormControl<string | null>(null),
messageId: new FormControl<string | null>(null),
messageId: new FormControl<string | null>(null)
});
this.filterForm.valueChanges.pipe(