Compare commits
No commits in common. "f83f54749dfc4f87a542d3498460951199d57396" and "6d0eaed426b1446f1f8f7fb9ca6f20358b3da16b" have entirely different histories.
f83f54749d
...
6d0eaed426
@ -82,5 +82,41 @@ Für eine Erklärung wie das alles funktioniert mach einfach /mitgucken für ein
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function manageAnnouncementRoles(guild: Guild, reaction: MessageReaction, requestId: string) {
|
||||||
|
const guildId = guild.id
|
||||||
|
logger.info("Managing roles", { guildId, requestId })
|
||||||
|
|
||||||
|
const announcementRole: Role | undefined = (await guild.roles.fetch()).find(role => role.id === config.bot.announcement_role)
|
||||||
|
if (!announcementRole) {
|
||||||
|
logger.error(`Could not find announcement role! Aborting! Was looking for role with id: ${config.bot.announcement_role}`, { guildId, requestId })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const usersWhoWantRole: User[] = (await reaction.users.fetch()).filter(user => !user.bot).map(user => user)
|
||||||
|
|
||||||
|
const allUsers = (await guild.members.fetch())
|
||||||
|
|
||||||
|
const usersWhoHaveRole: GuildMember[] = allUsers
|
||||||
|
.filter(member => member.roles.cache
|
||||||
|
.find(role => role.id === config.bot.announcement_role) !== undefined)
|
||||||
|
.map(member => member)
|
||||||
|
|
||||||
|
const usersWhoNeedRoleRevoked: GuildMember[] = usersWhoHaveRole
|
||||||
|
.filter(userWhoHas => !usersWhoWantRole.map(wanter => wanter.id).includes(userWhoHas.id))
|
||||||
|
|
||||||
|
const usersWhoDontHaveRole: GuildMember[] = allUsers
|
||||||
|
.filter(member => member.roles.cache
|
||||||
|
.find(role => role.id === config.bot.announcement_role) === undefined)
|
||||||
|
.map(member => member)
|
||||||
|
|
||||||
|
const usersWhoNeedRole: GuildMember[] = usersWhoDontHaveRole
|
||||||
|
.filter(userWhoNeeds => usersWhoWantRole.map(wanter => wanter.id).includes(userWhoNeeds.id))
|
||||||
|
|
||||||
|
|
||||||
|
logger.debug(`Theses users will get the role removed: ${JSON.stringify(usersWhoNeedRoleRevoked)}`, { guildId, requestId })
|
||||||
|
logger.debug(`Theses users will get the role added: ${JSON.stringify(usersWhoNeedRole)}`, { guildId, requestId })
|
||||||
|
|
||||||
|
usersWhoNeedRoleRevoked.forEach(user => user.roles.remove(announcementRole))
|
||||||
|
usersWhoNeedRole.forEach(user => user.roles.add(announcementRole))
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
import { Guild, MessageReaction, Role, User } from "discord.js";
|
import { Guild, Role, User } from "discord.js";
|
||||||
import { ExtendedClient } from "../structures/client";
|
import { ExtendedClient } from "../structures/client";
|
||||||
import { GuildMember } from "discord.js";
|
import { GuildMember } from "discord.js";
|
||||||
import { logger } from "../logger";
|
|
||||||
import { config } from "../configuration";
|
|
||||||
import { Maybe } from "../interfaces";
|
|
||||||
|
|
||||||
export default class RoleController {
|
export default class RoleController {
|
||||||
|
|
||||||
@ -12,11 +9,9 @@ export default class RoleController {
|
|||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
public async addRoleToUser(member: GuildMember, role: Role, guildId: string, requestId: string) {
|
public async addRoleToUser(member: GuildMember, role: Role, guildId: string, requestId: string) {
|
||||||
logger.info(`Adding Role ${role.id} to user ${member.id}|${member.user.username}`, { requestId, guildId })
|
|
||||||
return await member.roles.add(role)
|
return await member.roles.add(role)
|
||||||
}
|
}
|
||||||
private async removeRoleFromUser(member: GuildMember, role: Role, guildId: string, requestId: string) {
|
private async removeRoleFromUser(member: GuildMember, role: any, guildId: string, requestId: string) {
|
||||||
logger.info(`Removing Role ${role.id} from user ${member.id}|${member.user.username}`, { requestId, guildId })
|
|
||||||
return await member.roles.remove(role)
|
return await member.roles.remove(role)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,48 +25,4 @@ export default class RoleController {
|
|||||||
const guildMember = await guild.members.fetch(user)
|
const guildMember = await guild.members.fetch(user)
|
||||||
return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId)
|
return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise<Maybe<Role>> {
|
|
||||||
const mediaRole = this.getMediaRoleForGuild(guild.id)
|
|
||||||
return guild.roles.fetch()
|
|
||||||
.then(fetchedRoles => fetchedRoles.find(role => role.id === mediaRole.id))
|
|
||||||
.catch(error => { logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id }) })
|
|
||||||
}
|
|
||||||
|
|
||||||
public async manageAnnouncementRoles(guild: Guild, reaction: MessageReaction, requestId: string) {
|
|
||||||
const guildId = guild.id
|
|
||||||
logger.info("Managing roles", { guildId, requestId })
|
|
||||||
|
|
||||||
const announcementRole = await this.getAnnouncementRoleForGuild(guild, requestId)
|
|
||||||
if (!announcementRole)
|
|
||||||
throw new Error(`No announcementRole found in guild ${guildId}`)
|
|
||||||
|
|
||||||
const usersWhoWantRole: User[] = (await reaction.users.fetch()).filter(user => !user.bot).map(user => user)
|
|
||||||
|
|
||||||
const allUsers = await guild.members.fetch()
|
|
||||||
|
|
||||||
const usersWhoHaveRole: GuildMember[] = allUsers
|
|
||||||
.filter(member => member.roles.cache
|
|
||||||
.find(role => role.id === announcementRole.id) !== undefined)
|
|
||||||
.map(member => member)
|
|
||||||
|
|
||||||
const usersWhoNeedRoleRevoked: GuildMember[] = usersWhoHaveRole
|
|
||||||
.filter(userWhoHas => !usersWhoWantRole.map(wanter => wanter.id).includes(userWhoHas.id))
|
|
||||||
|
|
||||||
const usersWhoDontHaveRole: GuildMember[] = allUsers
|
|
||||||
.filter(member => member.roles.cache
|
|
||||||
.find(role => role.id === announcementRole.id) === undefined)
|
|
||||||
.map(member => member)
|
|
||||||
|
|
||||||
const usersWhoNeedRole: GuildMember[] = usersWhoDontHaveRole
|
|
||||||
.filter(userWhoNeeds => usersWhoWantRole.map(wanter => wanter.id).includes(userWhoNeeds.id))
|
|
||||||
|
|
||||||
|
|
||||||
logger.debug(`Theses users will get the role removed: ${JSON.stringify(usersWhoNeedRoleRevoked)}`, { guildId, requestId })
|
|
||||||
logger.debug(`Theses users will get the role added: ${JSON.stringify(usersWhoNeedRole)}`, { guildId, requestId })
|
|
||||||
|
|
||||||
usersWhoNeedRoleRevoked.forEach(user => this.removeRoleFromUser(user, announcementRole, guild.id, requestId))
|
|
||||||
usersWhoNeedRole.forEach(user => this.addRoleToUser(user, announcementRole, guild.id, requestId))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Collection } from "@discordjs/collection"
|
import { Collection } from "@discordjs/collection"
|
||||||
import { GuildScheduledEvent, Role, TextChannel } from "discord.js"
|
import { GuildScheduledEvent, Role, TextChannel } from "discord.js"
|
||||||
|
|
||||||
export type Maybe<T> = T | undefined | null | void
|
export type Maybe<T> = T | undefined | null
|
||||||
export interface Player {
|
export interface Player {
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import { ApplicationCommandDataResolvable, Client, ClientOptions, Collection, Gu
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { ScheduledTask, schedule } from "node-cron";
|
import { ScheduledTask, schedule } from "node-cron";
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
|
import { manageAnnouncementRoles } from "../commands/announce";
|
||||||
import { config } from "../configuration";
|
import { config } from "../configuration";
|
||||||
import { Maybe } from "../interfaces";
|
import { Maybe } from "../interfaces";
|
||||||
import { JellyfinHandler } from "../jellyfin/handler";
|
import { JellyfinHandler } from "../jellyfin/handler";
|
||||||
@ -176,7 +177,7 @@ export class ExtendedClient extends Client {
|
|||||||
const reactions = message.reactions.resolve("🎫")
|
const reactions = message.reactions.resolve("🎫")
|
||||||
//logger.debug(`reactions: ${JSON.stringify(reactions, null, 2)}`, { guildId: guild.id, requestId })
|
//logger.debug(`reactions: ${JSON.stringify(reactions, null, 2)}`, { guildId: guild.id, requestId })
|
||||||
if (reactions) {
|
if (reactions) {
|
||||||
this.roleController.manageAnnouncementRoles(message.guild, reactions, requestId)
|
manageAnnouncementRoles(message.guild, reactions, requestId)
|
||||||
} else {
|
} else {
|
||||||
logger.error("Did not get reactions! Aborting!", { guildId: guild.id, requestId })
|
logger.error("Did not get reactions! Aborting!", { guildId: guild.id, requestId })
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user