Re-add jira logic

This commit is contained in:
Amaryllis 2023-12-02 17:23:16 +01:00
parent 0ee296033c
commit 6c8cf194a7
3 changed files with 81 additions and 6 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ deploy.sh
config.*json*
!config.json.sample
.env
/tmp

75
bot.ts
View file

@ -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<any, any>;
}
}
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);

View file

@ -13,18 +13,19 @@ export default async function search(version?: string): Promise<any[]> {
}
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;
}