From 6c8cf194a730c00fd853ff551bd5480cb8130a3b Mon Sep 17 00:00:00 2001 From: Amaryllis Date: Sat, 2 Dec 2023 17:23:16 +0100 Subject: [PATCH] Re-add jira logic --- .gitignore | 1 + bot.ts | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++- model/jira.ts | 11 ++++---- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index debb514..bd21bff 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ deploy.sh config.*json* !config.json.sample .env +/tmp \ No newline at end of file diff --git a/bot.ts b/bot.ts index f6eebbc..07d9489 100644 --- a/bot.ts +++ b/bot.ts @@ -1,12 +1,22 @@ import { Client, GatewayIntentBits, GuildMember, Channel, TextBasedChannel, Events, Message, SlashCommandBuilder, Collection } from 'discord.js'; import * as dotenv from 'dotenv'; +import path from 'node:path'; +import fs from 'node:fs' + dotenv.config(); +declare module "discord.js" { + export interface Client { + commands: Collection; + } +} + import config from "./config"; export const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); import handleMessage from './messageHandler'; +import search from './model/jira'; client.on("ready", () => { console.log("successfully logged in"); @@ -23,7 +33,70 @@ client.on("guildMemberAdd", async (member: GuildMember) => { client.on(Events.MessageCreate, async (msg: Message) => { - await handleMessage(msg).catch(console.error); + + if (msg.content.startsWith(".cl ")) { + const version = msg.content.substring(4) + try { + // Test if a number + const versionNumber = Number(version) + const issues = await search(version); + + const issueDataList: { title: string, key: string }[] = [] + + issues.forEach((issue) => { + issueDataList.push({ title: issue.fields.summary, key: issue.key }) + }) + + const issueTextList: string[] = [] + + issueDataList.forEach((issue) => { + issueTextList.push(`[${issue.key}] ${issue.title}`) + }) + + let issueIndex = 0 + + let messagesToSend: string[] = [] + + let parsedAll = false + while (!parsedAll) { + let formattedMsg = "" + + let foundLength = 6 + for (let i = issueIndex; i < issueTextList.length + 1; ++i) { + if (i == issueTextList.length) { + parsedAll = true; + break; + } + + const line = `${issueTextList[i]}\n` + if (foundLength + line.length > 1999) { + break; + } + + formattedMsg += line + foundLength += line.length + issueIndex = i; + } + + messagesToSend.push(`\`\`\`${formattedMsg}\`\`\``) + } + + let index = 0 + messagesToSend.forEach((messageToSend) => { + let waitMultiplier = index + 1 + let waitTime = 1000 * waitMultiplier + setTimeout(() => { + msg.channel.send(messageToSend) + }, waitTime); + index++; + }) + } + catch (e) { + } + } + else { + await handleMessage(msg).catch(console.error); + } }); client.login(process.env.token); diff --git a/model/jira.ts b/model/jira.ts index 52ba88f..fbf3e9f 100644 --- a/model/jira.ts +++ b/model/jira.ts @@ -13,18 +13,19 @@ export default async function search(version?: string): Promise { } const issues: any[] = []; - let response: any | null = null; + let jsonResponse: any | null = null; do { // @ts-expect-error const httpParams = Object.keys(params).map(key => `${key}=${params[key]}`).join('&'); const url = `${config.jira.baseUrl}/search?${httpParams}`; - // what even is this - response = JSON.parse((await new axios.Axios({}).get(url)).data); - issues.push(...response!.issues); + const serverResponse = await new axios.Axios({}).get(url); + jsonResponse = JSON.parse(serverResponse.data); + + issues.push(...jsonResponse!.issues); params.startAt += 100; - } while (response!.issues.length > 99 && response!.total > 100); + } while (jsonResponse!.issues.length > 99 && jsonResponse!.total > 100); return issues; } \ No newline at end of file