Compare commits

..

No commits in common. "1e912b20ef710388c6de1a9cd29f1fec4cb6c4c9" and "2ebc7fbdbee55b29bc967392eadc0634c1a9437b" have entirely different histories.

5 changed files with 77 additions and 289 deletions

View File

@ -34,7 +34,7 @@
"monitor": "nodemon build/index.js", "monitor": "nodemon build/index.js",
"lint": "eslint . --ext .ts", "lint": "eslint . --ext .ts",
"lint-fix": "eslint . --ext .ts --fix", "lint-fix": "eslint . --ext .ts --fix",
"test": "jest --runInBand", "test": "jest",
"test-watch": "jest --watch" "test-watch": "jest --watch"
}, },
"devDependencies": { "devDependencies": {

View File

@ -71,6 +71,6 @@ export const config: Config = {
yavin_jellyfin_token: process.env.YAVIN_TOKEN ?? "", yavin_jellyfin_token: process.env.YAVIN_TOKEN ?? "",
yavin_jellyfin_collection_user: process.env.YAVIN_COLLECTION_USER ?? "", yavin_jellyfin_collection_user: process.env.YAVIN_COLLECTION_USER ?? "",
jf_user: process.env.JELLYFIN_USER ?? "", jf_user: process.env.JELLYFIN_USER ?? "",
random_movie_count: parseInt(process.env.RANDOM_MOVIE_COUNT ?? "5") ?? 5 random_movie_count: parseInt(process.env.RANDOM_MOVIE_COUNT ?? "") ?? 5
} }
} }

View File

@ -50,60 +50,37 @@ export default class VoteController {
logger.info(`Reroll ${noneOfThatReactions} > ${memberThreshold} ?`, { requestId, guildId }) logger.info(`Reroll ${noneOfThatReactions} > ${memberThreshold} ?`, { requestId, guildId })
if (noneOfThatReactions > memberThreshold) if (noneOfThatReactions > memberThreshold)
logger.info(`No reroll`, { requestId, guildId }) logger.info(`No reroll`, { requestId, guildId })
else { else
logger.info('Starting poll reroll', { requestId, guildId }) logger.info('Starting poll reroll', { requestId, guildId })
await this.handleReroll(reactedUponMessage, guild, guild.id, requestId) await this.handleReroll(reactedUponMessage, guild, guild.id, requestId)
logger.info(`Finished handling NONE_OF_THAT vote`, { requestId, guildId }) logger.info(`Finished handling NONE_OF_THAT vote`, { requestId, guildId })
} }
}
private async removeMessage(msg: Message): Promise<Message<boolean>> {
if (msg.pinned) {
await msg.unpin()
}
return await msg.delete()
}
public isAboveThreshold(vote: Vote): boolean {
const aboveThreshold = (vote.count - 1) >= 1
logger.debug(`${vote.movie} : ${vote.count} -> above: ${aboveThreshold}`)
return aboveThreshold
}
public async handleReroll(voteMessage: VoteMessage, guild: Guild, guildId: string, requestId: string) { public async handleReroll(voteMessage: VoteMessage, guild: Guild, guildId: string, requestId: string) {
//get movies that already had votes to give them a second chance //get movies that already had votes to give them a second chance
const voteInfo: VoteMessageInfo = await this.parseVoteInfoFromVoteMessage(voteMessage, requestId) const voteInfo: VoteMessageInfo = await this.parseVoteInfoFromVoteMessage(voteMessage, requestId)
const votedOnMovies = voteInfo.votes.filter(this.isAboveThreshold).filter(x => x.emote !== NONE_OF_THAT)
logger.info(`Found ${votedOnMovies.length} with votes`, { requestId, guildId })
// get movies from jellyfin to fill the remaining slots // get movies from jellyfin to fill the remaining slots
const newMovieCount: number = config.bot.random_movie_count - votedOnMovies.length const newMovieCount: number = config.bot.random_movie_count - voteInfo.votes.filter(x => x.count > 2).length
logger.info(`Fetching ${newMovieCount} from jellyfin`)
const newMovies: string[] = await this.yavinJellyfinHandler.getRandomMovieNames(newMovieCount, guildId, requestId) const newMovies: string[] = await this.yavinJellyfinHandler.getRandomMovieNames(newMovieCount, guildId, requestId)
// merge // merge
const movies: string[] = newMovies.concat(votedOnMovies.map(x => x.movie)) const movies: string[] = newMovies.concat(voteInfo.votes.map(x => x.movie))
// create new message // create new message
await this.closePoll(guild, requestId)
logger.info(`Creating new poll message with new movies: ${movies}`, { requestId, guildId }) const message = this.createVoteMessageText(guild.id, voteInfo.eventDate, movies, guildId, requestId)
const message = this.createVoteMessageText(voteInfo.eventId, voteInfo.eventDate, movies, guildId, requestId)
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId) const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
if (!announcementChannel) { if (!announcementChannel) {
logger.error(`No announcementChannel found for ${guildId}, can't post poll`) logger.error(`No announcementChannel found for ${guildId}, can't post poll`)
return return
} }
try {
logger.info(`Trying to remove old vote Message`, { requestId, guildId })
this.removeMessage(voteMessage)
} catch (err) {
logger.error(`Error during removeMessage: ${err}`)
}
const sentMessage = await this.sendVoteMessage(message, movies.length, announcementChannel) const sentMessage = await this.sendVoteMessage(message, movies.length, announcementChannel)
sentMessage.pin() sentMessage.pin()
logger.info(`Sent and pinned new poll message`, { requestId, guildId })
} }
private async fetchEventStartDateByEventId(guild: Guild, eventId: string, requestId: string): Promise<Maybe<Date>> { private async fetchEventStartDateByEventId(guild: Guild, eventId: string, requestId: string): Promise<Maybe<Date>> {
const guildEvent: GuildScheduledEvent = await guild.scheduledEvents.fetch(eventId) const guildEvent: GuildScheduledEvent = await guild.scheduledEvents.fetch(eventId)
if (!guildEvent) logger.error(`GuildScheduledEvent with id${eventId} could not be found`, { requestId, guildId: guild.id }) if (!guildEvent) logger.error(`GuildScheduledEvent with id${eventId} could not be found`, { requestId, guildId: guild.id })
@ -218,15 +195,14 @@ export default class VoteController {
logger.info("Deleting vote message") logger.info("Deleting vote message")
await lastMessage.delete() await lastMessage.delete()
const event = await this.getEvent(guild, guild.id, requestId) const event = await this.getEvent(guild, guild.id, requestId)
if (event && votes?.length > 0) { if (event) {
this.updateEvent(event, votes, guild, guildId, requestId) this.updateEvent(event, votes, guild, guildId, requestId)
this.sendVoteClosedMessage(event, votes[0].movie, guildId, requestId) this.sendVoteClosedMessage(event, votes[0].movie, guildId, requestId)
} }
lastMessage.unpin() //todo: uncomment when bot has permission to pin/unpin lastMessage.unpin() //todo: uncomment when bot has permission to pin/unpin
} }
/**
* gets votes for the movies without the NONE_OF_THAT votes
*/
public async getVotesByEmote(message: Message, guildId: string, requestId: string): Promise<Vote[]> { public async getVotesByEmote(message: Message, guildId: string, requestId: string): Promise<Vote[]> {
const votes: Vote[] = [] const votes: Vote[] = []
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, { guildId, requestId }) logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, { guildId, requestId })
@ -264,8 +240,8 @@ export default class VoteController {
logger.info("Updating event.", { guildId, requestId }) logger.info("Updating event.", { guildId, requestId })
voteEvent.edit(options) voteEvent.edit(options)
} }
public async sendVoteClosedMessage(event: GuildScheduledEvent, movie: string, guildId: string, requestId: string): Promise<Message<boolean>> { public async sendVoteClosedMessage(event: GuildScheduledEvent, movie: string, guildId: string, requestId: string) {
const date = event.scheduledStartAt ? format(event.scheduledStartAt, "dd.MM.") : "Fehler, event hatte kein Datum" const date = event.scheduledStartAt ? format(event.scheduledStartAt, "dd.MM") : "Fehler, event hatte kein Datum"
const time = event.scheduledStartAt ? format(event.scheduledStartAt, "HH:mm") : "Fehler, event hatte kein Datum" const time = event.scheduledStartAt ? format(event.scheduledStartAt, "HH:mm") : "Fehler, event hatte kein Datum"
const body = `[Abstimmung beendet] für https://discord.com/events/${event.guildId}/${event.id}\n<@&${config.bot.announcement_role}> Wir gucken ${movie} am ${date} um ${time}` const body = `[Abstimmung beendet] für https://discord.com/events/${event.guildId}/${event.id}\n<@&${config.bot.announcement_role}> Wir gucken ${movie} am ${date} um ${time}`
const options: MessageCreateOptions = { const options: MessageCreateOptions = {
@ -275,14 +251,13 @@ export default class VoteController {
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId) const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
logger.info("Sending vote closed message.", { guildId, requestId }) logger.info("Sending vote closed message.", { guildId, requestId })
if (!announcementChannel) { if (!announcementChannel) {
const errorMessages = "Could not find announcement channel. Please fix!" logger.error("Could not find announcement channel. Please fix!", { guildId, requestId })
logger.error(errorMessages, { guildId, requestId }) return
throw errorMessages
} }
return announcementChannel.send(options) announcementChannel.send(options)
} }
private extractMovieFromMessageByEmote(lastMessages: Message, emote: string): string { private extractMovieFromMessageByEmote(message: Message, emote: string): string {
const lines = lastMessages.cleanContent.split("\n") const lines = message.cleanContent.split("\n")
const emoteLines = lines.filter(line => line.includes(emote)) const emoteLines = lines.filter(line => line.includes(emote))
if (!emoteLines) { if (!emoteLines) {

View File

@ -1,96 +0,0 @@
import { Guild, GuildScheduledEvent, Message } from "discord.js"
import VoteController from "../../server/helper/vote.controller"
import { JellyfinHandler } from "../../server/jellyfin/handler"
import { ExtendedClient } from "../../server/structures/client"
import { Emoji, NONE_OF_THAT } from "../../server/constants"
describe('vote controller - none_of_that functions', () => {
const testEventId = '1234321'
const testEventDate = new Date('2023-01-01')
const testGuildId = "888999888"
const testMovies = [
'Movie1',
'Movie2',
'Movie3',
'Movie4',
'Movie5',
]
const votesList = [
{ emote: Emoji.one, count: 1, movie: testMovies[0] },
{ emote: Emoji.two, count: 2, movie: testMovies[1] },
{ emote: Emoji.three, count: 3, movie: testMovies[2] },
{ emote: Emoji.four, count: 1, movie: testMovies[3] },
{ emote: Emoji.five, count: 1, movie: testMovies[4] },
{ emote: NONE_OF_THAT, count: 2, movie: NONE_OF_THAT },
]
const mockClient: ExtendedClient = <ExtendedClient><unknown>{
user: {
id: 'mockId'
}
}
const mockJellyfinHandler: JellyfinHandler = <JellyfinHandler><unknown>{
getRandomMovieNames: jest.fn().mockReturnValue(["movie1"])
}
const votes = new VoteController(mockClient, mockJellyfinHandler)
const mockMessageContent = votes.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")
test('sendVoteClosedMessage', async () => {
mockClient.getAnnouncementChannelForGuild = jest.fn().mockReturnValue({
send: jest.fn().mockImplementation((options: any) => {
return new Promise((resolve) => {
resolve(options)
})
})
})
const scheduledEvent: GuildScheduledEvent = <GuildScheduledEvent>{
scheduledStartAt: testEventDate,
guildId: testGuildId,
id: testEventId
}
const res = await votes.sendVoteClosedMessage(scheduledEvent, 'MovieNew', 'guild', 'request')
expect(res).toEqual({
allowedMentions: {
parse: ["roles"]
},
content: `[Abstimmung beendet] für https://discord.com/events/${testGuildId}/${testEventId}\n<@&> Wir gucken MovieNew am 01.01. um 01:00`
})
})
// test('checkForPollsToClose', async () => {
//
// const testGuild: Guild = <Guild><unknown>{
// scheduledEvents: {
// fetch: jest.fn().mockImplementation(() => {
// return new Promise(resolve => {
// resolve([
// { name: "Event Name" },
// { name: "Event: VOTING OFFEN", scheduledStartTimestamp: "" },
// { name: "another voting" },
// ]
// )
// })
// })
// }
// }
//
// const result = await votes.checkForPollsToClose(testGuild)
//
//
//
//
// })
test('getVotesByEmote', async () => {
const mockMessage: Message = <Message><unknown>{
cleanContent: mockMessageContent,
reactions: {
resolve: jest.fn().mockImplementation((input: any) => {
return votesList.find(e => e.emote === input)
})
}
}
const result = await votes.getVotesByEmote(mockMessage, 'guildId', 'requestId')
expect(result.length).toEqual(5)
expect(result).toEqual(votesList.filter(x => x.movie != NONE_OF_THAT))
})
})

View File

@ -3,7 +3,6 @@ import VoteController, { Vote, VoteMessageInfo } from "../../server/helper/vote.
import { JellyfinHandler } from "../../server/jellyfin/handler" import { JellyfinHandler } from "../../server/jellyfin/handler"
import { ExtendedClient } from "../../server/structures/client" import { ExtendedClient } from "../../server/structures/client"
import { VoteMessage } from "../../server/helper/messageIdentifiers" import { VoteMessage } from "../../server/helper/messageIdentifiers"
import { Message, MessageReaction } from "discord.js"
test('parse votes from vote message', async () => { test('parse votes from vote message', async () => {
const testMovies = [ const testMovies = [
'Movie1', 'Movie1',
@ -84,93 +83,3 @@ test('parse votes from vote message', () => {
const result = voteController.parseGuildIdAndEventIdFromWholeMessage(testMessage) const result = voteController.parseGuildIdAndEventIdFromWholeMessage(testMessage)
expect(result).toEqual({ guildId: testGuildId, eventId: testEventId }) expect(result).toEqual({ guildId: testGuildId, eventId: testEventId })
}) })
test.skip('handles complete none_of_that vote', () => {
const mockJellyfinHandler: JellyfinHandler = <JellyfinHandler><unknown>{
getRandomMovieNames: jest.fn().mockReturnValue(["movie1"])
}
const testMovies = [
'Movie1',
'Movie2',
'Movie3',
'Movie4',
'Movie5',
]
const testEventId = '1234321'
const testEventDate = new Date('2023-01-01')
const testGuildId = "888999888"
const mockClient: ExtendedClient = <ExtendedClient><unknown>{
user: {
id: 'mockId'
}
}
const voteController = new VoteController(mockClient, mockJellyfinHandler)
const mockMessageContent = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")
const reactedUponMessage: VoteMessage = <VoteMessage><unknown>{
cleanContent: mockMessageContent,
guild: {
id: 'id',
roles: {
resolve: jest.fn().mockReturnValue({
members: [{}, {}, {}, {}, {}]//content does not matter
})
},
scheduledEvents: {
fetch: jest.fn().mockReturnValue([
{
name: 'voting offen'
}
])
}
},
unpin: jest.fn().mockImplementation(() => {
}),
delete: jest.fn().mockImplementation(() => {
}),
reactions: {
resolve: jest.fn().mockImplementation((input: any) => {
console.log(JSON.stringify(input))
}),
cache: {
get: jest.fn().mockReturnValue({
users: {
cache: [
{
id: "mockId"//to filter out
},
{
id: "userId1"
},
{
id: "userId2"
},
{
id: "userId3"
}
]
}
})
}
}
}
const msgReaction: MessageReaction = <MessageReaction><unknown>{
message: reactedUponMessage
}
mockClient.getAnnouncementChannelForGuild = jest.fn().mockReturnValue({
messages: {
fetch: jest.fn().mockReturnValue([
reactedUponMessage
])
}
})
const res = voteController.handleNoneOfThatVote(msgReaction, reactedUponMessage, 'requestId', 'guildId')
})