Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOtterlord authored and astrobot-houston committed Aug 15, 2024
1 parent d26466c commit 7e02fbf
Showing 1 changed file with 39 additions and 66 deletions.
105 changes: 39 additions & 66 deletions src/scheduled/weeklyStatistics.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import type {
Client,
EmbedBuilder,
ForumChannel,
Guild,
GuildForumTag,
TextChannel,
} from "discord.js";
import { getDefaultEmbed } from "../utils/embeds.js";

const getTagName = async (
guild: Guild,
fullTagList: GuildForumTag[],
id: string,
) => {
import type { Client, EmbedBuilder, ForumChannel, Guild, GuildForumTag, TextChannel } from 'discord.js';
import { getDefaultEmbed } from '../utils/embeds.js';

const getTagName = async (guild: Guild, fullTagList: GuildForumTag[], id: string) => {
const forumTag = fullTagList.find((tag) => tag.id === id);

let emoji = "";
let emoji = '';

if (forumTag) {
if (forumTag.emoji) {
if (forumTag.emoji.id) {
const guildEmoji = await guild.emojis.fetch(forumTag.emoji.id);
emoji = `<${guildEmoji.animated? "a" : ""}:${guildEmoji.name}:${guildEmoji.id}> `;
emoji = `<${guildEmoji.animated ? 'a' : ''}:${guildEmoji.name}:${guildEmoji.id}> `;
} else {
emoji = `${forumTag.emoji.name!} `;
}
Expand All @@ -35,48 +24,38 @@ export default {
time: process.env.STATS_SCHEDULE,
async execute(client: Client) {
if (!process.env.GUILD_ID) {
console.warn(
"No GUILD_ID enviroment variable was set. Skipping weekly statistics",
);
console.warn('No GUILD_ID enviroment variable was set. Skipping weekly statistics');
return;
}

const guild = await client.guilds.fetch(process.env.GUILD_ID);
await guild.fetch();

if (!process.env.SUPPORT_CHANNEL) {
console.warn(
"No SUPPORT_CHANNEL enviroment variable was set. Skipping weekly statistics",
);
console.warn('No SUPPORT_CHANNEL enviroment variable was set. Skipping weekly statistics');
return;
}

const forum: ForumChannel = (await guild.channels.fetch(
process.env.SUPPORT_CHANNEL,
)) as ForumChannel;
const forum: ForumChannel = (await guild.channels.fetch(process.env.SUPPORT_CHANNEL)) as ForumChannel;

const lastInterval = new Date();
lastInterval.setDate(lastInterval.getDate() - 7);

const _threads = (
await Promise.all([forum.threads.fetch(), forum.threads.fetchArchived()])
).map((t) => t.threads.filter((x) => x.createdAt! > lastInterval));
const _threads = (await Promise.all([forum.threads.fetch(), forum.threads.fetchArchived()])).map((t) =>
t.threads.filter((x) => x.createdAt! > lastInterval)
);
const threads = [..._threads[0].values(), ..._threads[1].values()];

// const threads = (await forum.threads.fetch()).threads.filter((x) => x.createdAt! > lastInterval)

if (!process.env.SUPPORT_SQUAD_CHANNEL) {
console.warn(
"No SUPPORT_SQUAD_CHANNEL enviroment variable was set. Skipping weekly statistics",
);
console.warn('No SUPPORT_SQUAD_CHANNEL enviroment variable was set. Skipping weekly statistics');
return;
}

const channel = (await client.channels.fetch(
process.env.SUPPORT_SQUAD_CHANNEL,
)!) as TextChannel;
const channel = (await client.channels.fetch(process.env.SUPPORT_SQUAD_CHANNEL)!) as TextChannel;

const titleEmbed = getDefaultEmbed().setTitle("Weekly support statistics");
const titleEmbed = getDefaultEmbed().setTitle('Weekly support statistics');

const embeds: EmbedBuilder[] = [];
embeds.push(titleEmbed);
Expand All @@ -93,26 +72,20 @@ export default {
try {
const first = (await thread.messages.fetch()).at(-2);
const starterMessage = await thread.fetchStarterMessage();
if (first?.content.includes("https://docs.astro.build")) linkedToDocs++;
if (first && starterMessage)
cumulativeResponse +=
first?.createdTimestamp - starterMessage?.createdTimestamp;
if (first?.content.includes('https://docs.astro.build')) linkedToDocs++;
if (first && starterMessage) cumulativeResponse += first?.createdTimestamp - starterMessage?.createdTimestamp;

// check for posts from new members
const owner = await thread.fetchOwner();

if(owner)
{
const member = (await guild.members.fetch(owner.id));
if (
member?.joinedAt &&
member.joinedAt > lastInterval
) {
if (owner) {
const member = await guild.members.fetch(owner.id);
if (member?.joinedAt && member.joinedAt > lastInterval) {
newMembers.add(owner.user?.id);
postsByNewMembers++;
}
} else {
console.log('NO OWNER')
console.log('NO OWNER');
}

thread.appliedTags.forEach((tag) => {
Expand All @@ -129,42 +102,42 @@ export default {
});
});
} catch (err: any) {
err = err.toString()
err = err.toString();
// 10008: unknown message (e.g. deleted message)
if (err.includes('10008')) continue
if (err.includes('10008')) continue;
errors.push([err, thread.id]);
}
}

const openEmbed = getDefaultEmbed();
openEmbed.setTitle("New posts");
openEmbed.setTitle('New posts');
openEmbed.setDescription(
`${_threads[0].size} open posts\n${_threads[1].size} closed posts\n${linkedToDocs} (${Math.round(
(linkedToDocs / threads.length) * 100,
(linkedToDocs / threads.length) * 100
)}%) include a link to docs in first response\nAverage response time of ${Math.round(
cumulativeResponse / threads.length / 1000 / 60,
)} minutes`,
cumulativeResponse / threads.length / 1000 / 60
)} minutes`
);
embeds.push(openEmbed);

const memberEmbed = getDefaultEmbed();
memberEmbed.setTitle("Posts from new members");
memberEmbed.setTitle('Posts from new members');
memberEmbed.setDescription(
`${newMembers.size} new members posting in <#${process.env.SUPPORT_CHANNEL}>\n${Math.round(
(postsByNewMembers / threads.length) * 100,
)}% of posts by new members`,
(postsByNewMembers / threads.length) * 100
)}% of posts by new members`
);
embeds.push(memberEmbed);

let description = "";
let description = '';
let embedCount = 0;

let tags = Object.fromEntries(
Object.entries(unsortedTags)
.sort((a, b) => {
return unsortedTags[a[0]][a[0]] - unsortedTags[b[0]][b[0]];
})
.reverse(),
.reverse()
);

for (const tagId in tags) {
Expand All @@ -185,17 +158,17 @@ export default {
// localDescription += `+ ${subDescriptions.join(' / ')}\n`;
// }

localDescription += "\n";
localDescription += '\n';

if (description.length + localDescription.length > 4096) {
let embed = getDefaultEmbed();

if (embedCount == 0) {
embed.setTitle("Tags");
embed.setTitle('Tags');
}

embed.setDescription(description);
description = "";
description = '';
embeds.push(embed);
embedCount += 1;
}
Expand All @@ -206,17 +179,17 @@ export default {
let embed = getDefaultEmbed();

if (embedCount == 0) {
embed.setTitle("Tags");
embed.setTitle('Tags');
}

embed.setDescription(description || "failed");
embed.setDescription(description || 'failed');
embeds.push(embed);

if (errors.length) {
const errorEmbed = getDefaultEmbed();
errorEmbed.setTitle("Errors");
errorEmbed.setTitle('Errors');
errorEmbed.setDescription(errors.map(([err, id]) => `<#${id}>: ${err}`).join('\n'));
embeds.push(errorEmbed)
embeds.push(errorEmbed);
}

for (let i = 0; i < embeds.length; i++) {
Expand Down

0 comments on commit 7e02fbf

Please sign in to comment.