implement announcement role management by reaction

This commit is contained in:
Sammy 2023-06-13 18:18:26 +02:00
parent a2c55ad676
commit 24754decf4
2 changed files with 61 additions and 6 deletions

View File

@ -1,11 +1,14 @@
import { Guild, GuildMember, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildScheduledEventSetStatusArg, GuildScheduledEventStatus, Message, MessageCreateOptions, MessageEditOptions, TextChannel, messageLink } from 'discord.js'
import { Guild, GuildMember, Message, MessageCreateOptions, MessageReaction, Role, TextChannel, User } from 'discord.js'
import { v4 as uuid } from 'uuid'
import { client } from '../..'
import { config } from '../configuration'
import { Emotes } from '../events/guildScheduledEventCreate'
import { logger } from '../logger'
import { Command } from '../structures/command'
import { RunOptions } from '../types/commandTypes'
import { client } from '../..'
import { off } from 'process'
import { ScheduledTask, schedule } from 'node-cron'
let task: ScheduledTask
export default new Command({
name: 'announce',
@ -29,7 +32,7 @@ export default new Command({
})
function isAdmin(member: GuildMember): boolean {
return member.roles.cache.find((role, _) => role.id === config.bot.jf_admin_role) != undefined
return member.roles.cache.find((role, _) => role.id === config.bot.jf_admin_role) !== undefined
}
async function sendAnnouncement(guildId: string, requestId: string): Promise<void> {
@ -47,5 +50,55 @@ Für eine Erklärung wie das alles funktioniert mach einfach /mitgucken für ein
content: body
}
const message: Message<true> = await announcementChannel.send(options)
message.react("🎫")
}
await message.react("🎫")
// await message.pin()
task = schedule("* * * * * *", async () => {
const reactions = await message.reactions.resolve("🎫")
if (reactions) {
manageAnnouncementRoles(message.guild, reactions)
} else {
logger.error("Did not get reactions! Aborting!", { guildId, requestId })
}
})
}
async function manageAnnouncementRoles(guild: Guild, reaction: MessageReaction) {
const requestId = uuid()
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))
}

View File

@ -23,6 +23,7 @@ export interface Config {
workaround_token: string
watcher_role: string
jf_admin_role: string
announcement_role: string
announcement_channel_id: string
jf_collection_id: string
}
@ -57,6 +58,7 @@ export const config: Config = {
workaround_token: process.env.TOKEN ?? "",
watcher_role: process.env.WATCHER_ROLE ?? "",
jf_admin_role: process.env.ADMIN_ROLE ?? "",
announcement_role: process.env.WATCHPARTY_ANNOUNCEMENT_ROLE ?? "",
announcement_channel_id: process.env.CHANNEL_ID ?? "",
jf_collection_id: process.env.JELLYFIN_COLLECTION_ID ?? ""
}