Compare commits

..

No commits in common. "d9d1d74ef909532f383ea5292aadd29221887081" and "b8a32aab409f1f9e0a8a940823997f24a54842e0" have entirely different histories.

9 changed files with 15 additions and 63 deletions

View File

@ -6,7 +6,6 @@ import { Maybe } from '../interfaces'
import { logger } from '../logger' import { logger } from '../logger'
import { Command } from '../structures/command' import { Command } from '../structures/command'
import { RunOptions } from '../types/commandTypes' import { RunOptions } from '../types/commandTypes'
import { messageIsInitialAnnouncement } from '../helper/messageIdentifiers'
export default new Command({ export default new Command({
name: 'announce', name: 'announce',
@ -62,7 +61,7 @@ async function sendInitialAnnouncement(guildId: string, requestId: string): Prom
return return
} }
const currentPinnedAnnouncementMessages = (await announcementChannel.messages.fetchPinned()).filter(message => messageIsInitialAnnouncement(message)) const currentPinnedAnnouncementMessages = (await announcementChannel.messages.fetchPinned()).filter(message => message.cleanContent.includes("[initial]"))
currentPinnedAnnouncementMessages.forEach(async (message) => await message.unpin()) currentPinnedAnnouncementMessages.forEach(async (message) => await message.unpin())
currentPinnedAnnouncementMessages.forEach(message => message.delete()) currentPinnedAnnouncementMessages.forEach(message => message.delete())

View File

@ -3,12 +3,11 @@ import { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildSchedu
import { v4 as uuid } from 'uuid' import { v4 as uuid } from 'uuid'
import { client } from '../..' import { client } from '../..'
import { config } from '../configuration' import { config } from '../configuration'
import { Emotes } from '../events/autoCreateVoteByWPEvent'
import { Maybe } from '../interfaces' import { Maybe } from '../interfaces'
import { logger } from '../logger' import { logger } from '../logger'
import { Command } from '../structures/command' import { Command } from '../structures/command'
import { RunOptions } from '../types/commandTypes' import { RunOptions } from '../types/commandTypes'
import { messageIsVoteEndedMessage, messageIsVoteMessage } from '../helper/messageIdentifiers'
import { Emotes } from '../constants'
export default new Command({ export default new Command({
name: 'closepoll', name: 'closepoll',
@ -42,7 +41,7 @@ export async function closePoll(guild: Guild, requestId: string) {
const messages: Message<true>[] = (await announcementChannel.messages.fetch()) //todo: fetch only pinned messages const messages: Message<true>[] = (await announcementChannel.messages.fetch()) //todo: fetch only pinned messages
.map((value) => value) .map((value) => value)
.filter(message => !messageIsVoteEndedMessage(message) && messageIsVoteMessage(message)) .filter(message => !message.cleanContent.includes("[Abstimmung beendet]") && message.cleanContent.includes("[Abstimmung]"))
.sort((a, b) => b.createdTimestamp - a.createdTimestamp) .sort((a, b) => b.createdTimestamp - a.createdTimestamp)
if (!messages || messages.length <= 0) { if (!messages || messages.length <= 0) {

View File

@ -1,3 +0,0 @@
export enum Emotes { "1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣", "🔟" }
export const NONE_OF_THAT = "❌"

View File

@ -6,12 +6,14 @@ import { config } from "../configuration";
import { createDateStringFromEvent } from "../helper/dateHelper"; import { createDateStringFromEvent } from "../helper/dateHelper";
import { Maybe } from "../interfaces"; import { Maybe } from "../interfaces";
import { logger } from "../logger"; import { logger } from "../logger";
import { Emotes, NONE_OF_THAT } from "../constants";
export const name = 'guildScheduledEventCreate' export const name = 'guildScheduledEventCreate'
export enum Emotes { "1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣", "🔟" }
export const NONE_OF_THAT = "❌"
export let task: ScheduledTask | undefined
export async function execute(event: GuildScheduledEvent) { export async function execute(event: GuildScheduledEvent) {
const requestId = uuid() const requestId = uuid()

View File

@ -2,7 +2,6 @@ import { Collection, GuildScheduledEvent, GuildScheduledEventStatus, Message } f
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
import { client } from "../.."; import { client } from "../..";
import { logger } from "../logger"; import { logger } from "../logger";
import { messageIsInitialAnnouncement } from "../helper/messageIdentifiers";
export const name = 'guildScheduledEventUpdate' export const name = 'guildScheduledEventUpdate'
@ -26,7 +25,7 @@ export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildSche
const events = await newEvent.guild.scheduledEvents.fetch() const events = await newEvent.guild.scheduledEvents.fetch()
const wpAnnouncements = (await announcementChannel.messages.fetch()).filter(message => !messageIsInitialAnnouncement(message)) const wpAnnouncements = (await announcementChannel.messages.fetch()).filter(message => !message.cleanContent.includes("[initial]"))
const announcementsWithoutEvent = filterAnnouncementsByPendingWPs(wpAnnouncements, events) const announcementsWithoutEvent = filterAnnouncementsByPendingWPs(wpAnnouncements, events)
logger.info(`Deleting ${announcementsWithoutEvent.length} announcements.`, { guildId, requestId }) logger.info(`Deleting ${announcementsWithoutEvent.length} announcements.`, { guildId, requestId })
announcementsWithoutEvent.forEach(message => message.delete()) announcementsWithoutEvent.forEach(message => message.delete())

View File

@ -2,43 +2,18 @@
import { Message, MessageReaction, User } from "discord.js"; import { Message, MessageReaction, User } from "discord.js";
import { messageIsVoteMessage } from "../helper/messageIdentifiers"; import { messageIsVoteMessage } from "../helper/messageIdentifiers";
import { logger, newRequestId, noGuildId } from "../logger"; import { logger, newRequestId, noGuildId } from "../logger";
import { NONE_OF_THAT } from "../constants";
import { client } from "../..";
import { getMembersWithRoleFromGuild } from "../helper/roleFilter";
import { config } from "../configuration";
export const name = 'messageReactionAdd' export const name = 'messageReactionAdd'
export async function execute(messageReaction: MessageReaction, user: User) { export async function execute(messageReaction: MessageReaction, user: User) {
if (user.id == client.user?.id)
logger.info('Skipping bot reaction')
const requestId = newRequestId const requestId = newRequestId
const guildId = messageReaction.message.inGuild() ? messageReaction.message.guildId : noGuildId const guildId = messageReaction.message.inGuild() ? messageReaction.message.guildId : noGuildId
const reactedUponMessage: Message = messageReaction.message.partial ? await messageReaction.message.fetch() : messageReaction.message
if (!messageReaction.message.guild) return 'No guild'
logger.info(`Got reaction on message`, { requestId, guildId }) const reactedUponMessage: Message = messageReaction.message.partial ? await messageReaction.message.fetch() : messageReaction.message
logger.debug(`reactedUponMessage payload: ${JSON.stringify(reactedUponMessage)}`)
if (messageIsVoteMessage(reactedUponMessage)) { if (messageIsVoteMessage(reactedUponMessage)) {
logger.debug(`${reactedUponMessage.id} is vote message`, { requestId, guildId }) logger.info(`Got reaction on message`, { requestId, guildId })
if (messageReaction.message.reactions.cache.find(reaction => reaction.emoji.toString() == NONE_OF_THAT)) {
const watcherRoleMember = await getMembersWithRoleFromGuild(config.bot.announcement_role, messageReaction.message.guild)
logger.info("ROLE MEMBERS " + JSON.stringify(watcherRoleMember), { requestId, guildId })
const watcherRoleMemberCount = watcherRoleMember.size
logger.info(`MEMBER COUNT: ${watcherRoleMemberCount}`, { requestId, guildId })
let noneOfThatReactions = messageReaction.message.reactions.cache.get(NONE_OF_THAT)?.users.cache.filter(x => x.id !== client.user?.id).size ?? 0
const memberThreshold = (watcherRoleMemberCount / 2)
logger.info(`Reroll ${noneOfThatReactions} > ${memberThreshold} ?`, { requestId, guildId })
if (noneOfThatReactions > memberThreshold) {
logger.info('Starting poll reroll', { requestId, guildId })
messageReaction.message.edit((messageReaction.message.content ?? "").concat('\nDiese Abstimmung muss wiederholt werden.'))
}
logger.info(`No reroll`, { requestId, guildId })
}
} }
return return

View File

@ -1,11 +1,6 @@
import { Message } from "discord.js"; import { Message } from "discord.js";
export function messageIsVoteMessage(msg: Message): boolean { export function messageIsVoteMessage(msg: Message): boolean {
return msg.cleanContent.includes('[Abstimmung]') return msg.content.includes('[Abstimmung]')
}
export function messageIsInitialAnnouncement(msg: Message): boolean {
return msg.cleanContent.includes("[initial]")
}
export function messageIsVoteEndedMessage(msg: Message): boolean {
return msg.cleanContent.includes("[Abstimmung beendet]")
} }

View File

@ -1,5 +1,5 @@
import { Collection, Guild, GuildMember, Role, User } from "discord.js" import { Collection, GuildMember } from "discord.js"
import { ChangedRoles, Maybe, PermissionLevel } from "../interfaces" import { ChangedRoles, PermissionLevel } from "../interfaces"
import { logger } from "../logger" import { logger } from "../logger"
import { config } from "../configuration" import { config } from "../configuration"
@ -16,13 +16,6 @@ export function filterRolesFromMemberUpdate(oldMember: GuildMember, newMember: G
return { addedRoles, removedRoles } return { addedRoles, removedRoles }
} }
export async function getMembersWithRoleFromGuild(roleId: string, guild: Guild): Promise<Collection<string, GuildMember>> {
const emptyResponse = new Collection<string, GuildMember>
const guildRole: Maybe<Role> = guild.roles.resolve(roleId)
if (!guildRole) return emptyResponse
return guildRole.members
}
export function getGuildSpecificTriggerRoleId(): Collection<string, PermissionLevel> { export function getGuildSpecificTriggerRoleId(): Collection<string, PermissionLevel> {
const outVal = new Collection<string, PermissionLevel>() const outVal = new Collection<string, PermissionLevel>()
outVal.set(config.bot.watcher_role, "VIEWER") outVal.set(config.bot.watcher_role, "VIEWER")

View File

@ -9,7 +9,6 @@ import { JellyfinHandler } from "../jellyfin/handler";
import { logger } from "../logger"; import { logger } from "../logger";
import { CommandType } from "../types/commandTypes"; import { CommandType } from "../types/commandTypes";
import { checkForPollsToClose } from "../commands/closepoll"; import { checkForPollsToClose } from "../commands/closepoll";
import { messageIsInitialAnnouncement } from "../helper/messageIdentifiers";
@ -23,7 +22,7 @@ export class ExtendedClient extends Client {
private pollCloseBackgroundTasks: Collection<string, ScheduledTask> = new Collection() private pollCloseBackgroundTasks: Collection<string, ScheduledTask> = new Collection()
public constructor(jf: JellyfinHandler) { public constructor(jf: JellyfinHandler) {
const intents: IntentsBitField = new IntentsBitField() const intents: IntentsBitField = new IntentsBitField()
intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildMessageReactions, IntentsBitField.Flags.GuildVoiceStates) intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildVoiceStates)
const options: ClientOptions = { intents } const options: ClientOptions = { intents }
super(options) super(options)
this.jellyfin = jf this.jellyfin = jf
@ -75,7 +74,6 @@ export class ExtendedClient extends Client {
this.registerCommands(slashCommands, guilds) this.registerCommands(slashCommands, guilds)
this.cacheUsers(guilds) this.cacheUsers(guilds)
await this.cacheAnnouncementServer(guilds) await this.cacheAnnouncementServer(guilds)
this.fetchAnnouncementChannelMessage(this.announcementChannels)
this.startAnnouncementRoleBackgroundTask(guilds) this.startAnnouncementRoleBackgroundTask(guilds)
this.startPollCloseBackgroundTasks() this.startPollCloseBackgroundTasks()
}) })
@ -83,11 +81,6 @@ export class ExtendedClient extends Client {
logger.info(`Error refreshing slash commands: ${error}`) logger.info(`Error refreshing slash commands: ${error}`)
} }
} }
private async fetchAnnouncementChannelMessage(channels: Collection<string, TextChannel>): Promise<void> {
channels.each(async ch => {
ch.messages.fetch()
})
}
private async cacheAnnouncementServer(guilds: Collection<Snowflake, Guild>) { private async cacheAnnouncementServer(guilds: Collection<Snowflake, Guild>) {
for (const guild of guilds.values()) { for (const guild of guilds.values()) {
const channels: TextChannel[] = <TextChannel[]>(await guild.channels.fetch()) const channels: TextChannel[] = <TextChannel[]>(await guild.channels.fetch())
@ -143,7 +136,7 @@ export class ExtendedClient extends Client {
} }
this.announcementRoleHandlerTask.set(guild.id, schedule("*/10 * * * * *", async () => { this.announcementRoleHandlerTask.set(guild.id, schedule("*/10 * * * * *", async () => {
const requestId = uuid() const requestId = uuid()
const messages = (await textChannel.messages.fetchPinned()).filter(message => messageIsInitialAnnouncement(message)) const messages = (await textChannel.messages.fetchPinned()).filter(message => message.cleanContent.includes("[initial]"))
if (messages.size > 1) { if (messages.size > 1) {
logger.error("More than one pinned announcement Messages found. Unable to know which one people react to. Please fix!", { guildId: guild.id, requestId }) logger.error("More than one pinned announcement Messages found. Unable to know which one people react to. Please fix!", { guildId: guild.id, requestId })