Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping #5933

Merged
merged 7 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
node_version: [18.12.0, 20]
node_version: [20]
os: [ubuntu-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
node_version: [18.12.0, 20]
node_version: [20]
os: [ubuntu-latest]

steps:
Expand Down
43 changes: 21 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"scripts": {
"gen": "concurrently \"prisma generate\" \"prisma generate --schema prisma/robochimp.prisma\" && echo \"Generated Prisma Client\"",
"lint": "concurrently \"prettier --use-tabs ./**/*.{js,md,json,yml} --write\" \"eslint *.{ts,mts} \"{src,tests}/**/*.ts\" --fix\"",
"gen": "concurrently \"prisma generate --no-hints\" \"prisma generate --no-hints --schema prisma/robochimp.prisma\" && echo \"Generated Prisma Client\"",
"prettify": "prettier --use-tabs ./**/*.{js,md,json,yml} --write",
"lint": "concurrently \"yarn prettify\" \"eslint --quiet *.{ts,mts} \"{src,tests}/**/*.ts\" --fix\"",
"build:tsc": "tsc -p src",
"build": "concurrently \"yarn wipedist\" \"yarn gen\" && concurrently \"yarn prebuild:scripts\" && yarn build:tsc && echo \"Finished Building\"",
"build": "concurrently \"yarn wipedist\" \"yarn gen\" && yarn pre:build && yarn build:tsc && yarn post:build && echo \"Finished Building\"",
"wipedist": "rimraf \"dist/\"",
"start": "yarn build && concurrently \"tsc -w -p src\" \"node --enable-source-maps dist/\"",
"test": "concurrently \"tsc -p src\" \"yarn test:lint\" \"yarn test:unit\"",
Expand All @@ -12,7 +13,8 @@
"dev": "yarn wipedist && tsc -w -p src",
"test:watch": "vitest --config vitest.unit.config.mts --coverage",
"test:integration": "tsx ./src/scripts/integration-tests.ts",
"prebuild:scripts": "tsx ./src/scripts/render-creatables-file.ts",
"pre:build": "tsx ./src/scripts/render-creatables-file.ts",
"post:build": "tsx ./src/scripts/postbuild.ts",
"build:esbuild": "esbuild --bundle src/index.ts --outdir=dist --platform=node --loader:.node=file --external:canvas",
"run:node": "node --enable-source-maps dist/index.js"
},
Expand All @@ -21,49 +23,45 @@
"@fastify/helmet": "^10.1.0",
"@fastify/rate-limit": "^8.0.0",
"@fastify/sensible": "^5.2.0",
"@napi-rs/canvas": "0.1.38",
"@napi-rs/canvas": "^0.1.53",
"@octokit/graphql": "^4.8.0",
"@oldschoolgg/toolkit": "^0.0.24",
"@prisma/client": "^5.13.0",
"@sapphire/snowflake": "^3.5.3",
"@sapphire/stopwatch": "^1.4.0",
"@sapphire/time-utilities": "^1.6.0",
"@sentry/node": "^7.113.0",
"@sentry/node": "^8.13.0",
"ascii-table3": "^0.9.0",
"bufferutil": "^4.0.8",
"chart.js": "^3.7.0",
"chartjs-node-canvas": "github:gc/ChartjsNodeCanvas#a598b6dd27c44351f235bca07ca4ee660121f289",
"chartjs-plugin-datalabels": "^2.0.0",
"deep-equal": "^2.2.3",
"deepmerge": "^4.3.1",
"discord.js": "^14.14.1",
"dotenv": "^16.0.3",
"discord.js": "14.14.1",
"dotenv": "^16.4.5",
"e": "^0.2.33",
"fast-deep-equal": "^3.1.3",
"fastify": "^4.14.1",
"fastify-raw-body": "^4.2.0",
"he": "^1.2.0",
"lodash": "^4.17.21",
"lru-cache": "^8.0.0",
"lru-cache": "^10.3.0",
"mahoji": "^0.0.7",
"mathjs": "^13.0.0",
"murmurhash": "^2.0.1",
"node-cron": "^3.0.3",
"node-fetch": "^2.6.7",
"oldschooljs": "^2.5.4",
"p-queue": "^6.6.2",
"piscina": "^4.4.0",
"piscina": "^4.6.1",
"random-js": "^2.1.0",
"simple-statistics": "^7.8.3",
"sonic-boom": "^4.0.1",
"tsx": "^4.9.0",
"zlib-sync": "^0.1.9",
"zod": "^3.23.6"
"zod": "^3.23.8"
},
"devDependencies": {
"@oldschoolgg/eslint-config": "^2.0.13",
"@oldschoolgg/ts-config": "^0.0.1",
"@types/deep-equal": "^1.0.3",
"@types/he": "^1.1.2",
"@types/jest-image-snapshot": "^6.1.0",
"@types/lodash": "^4.14.195",
"@types/madge": "^5.0.0",
Expand All @@ -73,10 +71,10 @@
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"@vitest/coverage-v8": "^1.3.1",
"concurrently": "^7.6.0",
"dotenv-cli": "^7.3.0",
"esbuild": "^0.20.2",
"@vitest/coverage-v8": "^1.6.0",
"concurrently": "^8.2.2",
"dotenv-cli": "^7.4.2",
"esbuild": "^0.22.0",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
Expand All @@ -88,11 +86,12 @@
"mitm": "^1.7.2",
"prettier": "^2.7.1",
"prisma": "^5.13.0",
"rimraf": "^5.0.5",
"rimraf": "^5.0.7",
"tsx": "^4.16.0",
"typescript": "5.0.2",
"vitest": "^1.6.0"
},
"engines": {
"node": ">=18.12.0"
"node": ">=20.12.0"
}
}
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import { interactionHook } from './lib/util/globalInteractions';
import { handleInteractionError } from './lib/util/interactionReply';
import { logError } from './lib/util/logError';
import { sonicBoom } from './lib/util/logger';
import { onStartup } from './mahoji/lib/events';
import { postCommand } from './mahoji/lib/postCommand';
import { preCommand } from './mahoji/lib/preCommand';
Expand Down Expand Up @@ -140,7 +139,7 @@
client.mahojiClient = mahojiClient;
global.globalClient = client;
client.on('messageCreate', msg => {
onMessage(msg);

Check warning on line 142 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
});
client.on('interactionCreate', async interaction => {
if (BLACKLISTED_USERS.has(interaction.user.id)) return;
Expand Down Expand Up @@ -176,16 +175,16 @@

client.on(Events.ServerNotification, (message: string) => {
const channel = globalClient.channels.cache.get(Channel.Notifications);
if (channel) (channel as TextChannel).send(message);

Check warning on line 178 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
});

client.on(Events.EconomyLog, async (message: string) => {
economyLog(message);

Check warning on line 182 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
});
client.on('guildCreate', guild => {
if (!guild.available) return;
if (BLACKLISTED_GUILDS.has(guild.id) || BLACKLISTED_USERS.has(guild.ownerId)) {
guild.leave();

Check warning on line 187 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
});

Expand Down Expand Up @@ -218,9 +217,4 @@
logError(err);
});

process.on('exit', exitCode => {
sonicBoom.flushSync();
debugLog('Process Exit', { type: 'PROCESS_EXIT', exitCode });
});

main();

Check warning on line 220 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
95 changes: 0 additions & 95 deletions src/lib/crons.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { channelIsSendable } from '@oldschoolgg/toolkit';
import { EmbedBuilder, PermissionsBitField, resolveColor, TextChannel } from 'discord.js';
import { Time } from 'e';
import he from 'he';
import { schedule } from 'node-cron';
import fetch from 'node-fetch';

import { production } from '../config';
import { analyticsTick } from './analytics';
import { prisma } from './settings/prisma';
import { cacheCleanup } from './util/cachedUserIDs';
import { sendToChannelID } from './util/webhook';

// If API request fails, we'll retry the previous range next time. (Max of +1 hour)
let redditApiFailures = 0;
const MAX_REDDIT_RETRIES = 12;

export function initCrons() {
/**
Expand All @@ -32,90 +21,6 @@
GROUP BY item_id;`);
});

let REDDIT_POSTS_IS_DISABLED = false;
/**
* JMod reddit posts/comments
*/
const redditGranularity = 5;
const alreadySentCache = new Set();
schedule(`*/${redditGranularity} * * * *`, async () => {
if (!production) return;
if (REDDIT_POSTS_IS_DISABLED) return;
async function sendReddit({ post }: { post: any; type: 'comment' | 'submission' }) {
const author = (post.author as string) ?? 'Unknown Author';
const embed = new EmbedBuilder().setAuthor({ name: author }).setColor(resolveColor('#ff9500'));

const url = post.full_link ?? `https://old.reddit.com${post.permalink}`;

if (post.body) {
embed.setDescription(he.decode(post.body));
}

if (post.title) {
embed.setTitle(post.title);
embed.setURL(url);
}

const guildsToSendTo = await prisma.guild.findMany({
where: {
jmodComments: {
not: null
}
},
select: {
id: true,
jmodComments: true
}
});

for (const { id, jmodComments } of guildsToSendTo) {
const guild = globalClient.guilds.cache.get(id);
if (!guild) continue;

if (!jmodComments) continue;
const channel = guild.channels.cache.get(jmodComments);
if (!channel) continue;

const perms = channel.permissionsFor(globalClient.user!);
if (!perms) continue;
if (!channelIsSendable(channel)) continue;

if (
channel instanceof TextChannel &&
perms.has(PermissionsBitField.Flags.EmbedLinks) &&
perms.has(PermissionsBitField.Flags.SendMessages)
) {
await sendToChannelID(channel.id, { content: `<${url}>`, embed });
}
}
}

const retries = Math.min(MAX_REDDIT_RETRIES, redditApiFailures);
const utcTime = Math.floor((Date.now() - Time.Minute * redditGranularity * (retries + 1)) / 1000) - 30;
let retry = false;
for (const type of ['comment', 'submission'] as const) {
const url = `https://api.pushshift.io/reddit/search/${type}/?subreddit=2007scape&size=1000&since=${utcTime}`;
try {
const _result = await fetch(url).then(res => {
if (res.status >= 400) retry = true;
return res.json();
});
if (!_result || !_result.data || !Array.isArray(_result.data)) {
if (_result && _result.error !== null) retry = true;
continue;
}
for (const entity of _result.data) {
if (!entity.author_flair_text || !entity.author_flair_text.includes(':jagexmod:')) continue;
if (alreadySentCache.has(entity.id)) continue;
await sendReddit({ post: entity, type });
alreadySentCache.add(entity.id);
}
redditApiFailures = 0;
} catch {}
}
if (retry) redditApiFailures++;
});

/**
* Analytics
*/
Expand All @@ -137,6 +42,6 @@
*/
schedule('0 0 */1 * *', async () => {
debugLog('Cache cleanup cronjob starting');
cacheCleanup();

Check warning on line 45 in src/lib/crons.ts

View workflow job for this annotation

GitHub Actions / Node v20 - ubuntu-latest

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
});
}
Loading
Loading