Skip to content

Commit

Permalink
various changes/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Dec 24, 2024
1 parent 06cbbc0 commit bbc8f08
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 115 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"============DEVELOPMENT": "============",
"watch": "nodemon --delay 1ms -e ts -w src --exec \"pnpm build && node --enable-source-maps dist\"",
"watch": "nodemon --delay 1ms -e ts -w src --exec \"pnpm build:esbuild && node --enable-source-maps dist\"",
"dev:stage1": "pnpm concurrent \"pnpm install\" \"prisma db push\" \"pnpm generate:robochimp\"",
"dev:stage2": "pnpm concurrent \"pnpm commands\" \"pnpm creatables\" \"pnpm wiki\" \"pnpm build\"",
"dev:stage3": "pnpm lint && pnpm test",
Expand Down
12 changes: 6 additions & 6 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
"description": "Oldschoolgg Toolkit",
"exports": {
".": {
"types": "./dist/util.d.ts",
"require": "./dist/cjs/util.cjs",
"import": "./dist/esm/util.mjs",
"types": "./dist/util.d.ts"
"import": "./dist/esm/util.mjs"
},
"./util": {
"types": "./dist/util.d.ts",
"require": "./dist/cjs/util.cjs",
"import": "./dist/esm/util.mjs",
"types": "./dist/util.d.ts"
"import": "./dist/esm/util.mjs"
},
"./structures": {
"types": "./dist/structures.d.ts",
"require": "./dist/cjs/structures.cjs",
"import": "./dist/esm/structures.mjs",
"types": "./dist/structures.d.ts"
"import": "./dist/esm/structures.mjs"
}
},
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,14 @@ model UserEvent {
@@map("user_event")
}

model Badges {
id Int @id @default(autoincrement())
text String
@@map("badges")
}

enum command_name_enum {
testpotato
achievementdiary
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ async function main() {
asyncExitHook(exitCleanup, {
wait: 2000
})
)
),
client.login(globalConfig.botToken)
]);
if (process.env.TEST) return;
await client.login(globalConfig.botToken);
console.log(`Logged in as ${globalClient.user.username}`);
}

Expand Down
8 changes: 8 additions & 0 deletions src/lib/rawSql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ export async function loggedRawPrismaQuery<T>(query: string): Promise<T | null>

return null;
}

export const SQL = {
SELECT_FULL_NAME:
"TRIM(COALESCE(string_agg(b.text, ' '), '') || ' ' || COALESCE(username, 'Unknown')) AS full_name",
LEFT_JOIN_BADGES: 'LEFT JOIN badges b ON b.id = ANY(u.badges)',
GROUP_BY_U_ID: 'GROUP BY u.id',
WHERE_IRON: (ironOnly: boolean) => (ironOnly ? '"minion.ironman" = true' : '')
} as const;
12 changes: 8 additions & 4 deletions src/lib/util/clLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { stringMatches } from '@oldschoolgg/toolkit/util';

import { SQL } from '../rawSql.js';
import { userEventsToMap } from './userEvents';

export async function fetchMultipleCLLeaderboards(
Expand Down Expand Up @@ -34,12 +36,14 @@ export async function fetchMultipleCLLeaderboards(
const userIdsList = userIds.length > 0 ? userIds.map(i => `'${i}'`).join(', ') : 'NULL';

const query = `
SELECT id, qty
SELECT id, qty, full_name
FROM (
SELECT id, CARDINALITY(cl_array & ${SQL_ITEMS}) AS qty
FROM users
SELECT u.id, CARDINALITY(cl_array & ${SQL_ITEMS}) AS qty, ${SQL.SELECT_FULL_NAME}
FROM users u
${SQL.LEFT_JOIN_BADGES}
WHERE (cl_array && ${SQL_ITEMS}
${ironmenOnly ? 'AND "users"."minion.ironman" = true' : ''}) ${userIds.length > 0 ? `OR id IN (${userIdsList})` : ''}
${ironmenOnly ? 'AND "u"."minion.ironman" = true' : ''}) ${userIds.length > 0 ? `OR u.id IN (${userIdsList})` : ''}
${SQL.GROUP_BY_U_ID}
) AS subquery
ORDER BY qty DESC
LIMIT ${resultLimit};
Expand Down
9 changes: 9 additions & 0 deletions src/mahoji/commands/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,15 @@ Guilds Blacklisted: ${BLACKLISTED_GUILDS.size}`;
}

if (options.sync_commands) {
if (!globalConfig.isProduction) {
await bulkUpdateCommands({
client: globalClient.mahojiClient,
commands: Array.from(globalClient.mahojiClient.commands.values()),
guildID: globalConfig.supportServerID
});
return 'Done.';
}

const global = Boolean(globalConfig.isProduction);
const totalCommands = Array.from(globalClient.mahojiClient.commands.values());
const globalCommands = totalCommands.filter(i => !i.guildID);
Expand Down
67 changes: 46 additions & 21 deletions src/mahoji/commands/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { masteryKey } from '../../lib/constants';
import { allClNames, getCollectionItems } from '../../lib/data/Collections';
import { effectiveMonsters } from '../../lib/minions/data/killableMonsters';
import { allOpenables } from '../../lib/openables';
import { SQL } from '../../lib/rawSql.js';
import { Minigames } from '../../lib/settings/minigames';

import Skills from '../../lib/skilling/skills';
import Agility from '../../lib/skilling/skills/agility';
import Hunter from '../../lib/skilling/skills/hunter/hunter';
Expand Down Expand Up @@ -83,7 +83,7 @@ function doMenuWrapper({
formatter
}: {
ironmanOnly: boolean;
users: { id: string; score: number }[];
users: { id: string; score: number; full_name?: string }[];
title: string;
interaction: ChatInputCommandInteraction;
user: MUser;
Expand All @@ -97,7 +97,7 @@ function doMenuWrapper({
const chnk = chunked[c];
const unwaited = chnk.map(
async (user, i) =>
`${getPos(c, i)}**${await getUsername(user.id)}:** ${formatter ? formatter(user.score) : user.score.toLocaleString()}`
`${getPos(c, i)}**${user.full_name ?? (await getUsername(user.id))}:** ${formatter ? formatter(user.score) : user.score.toLocaleString()}`
);
const pageText = (await Promise.all(unwaited)).join('\n');
return { embeds: [new EmbedBuilder().setTitle(title).setDescription(pageText)] };
Expand Down Expand Up @@ -212,13 +212,20 @@ async function sacrificeLb(
) {
if (type === 'value') {
const list = (
await prisma.$queryRawUnsafe<{ id: string; amount: number }[]>(
`SELECT "id", "sacrificedValue"
FROM users
WHERE "sacrificedValue" > 0
${ironmanOnly ? 'AND "minion.ironman" = true' : ''}
ORDER BY "sacrificedValue"
DESC LIMIT 2000;`
await prisma.$queryRawUnsafe<{ id: string; full_name: string; amount: number }[]>(
`SELECT
u.id,
${SQL.SELECT_FULL_NAME},
"sacrificedValue"
FROM
users u
${SQL.LEFT_JOIN_BADGES}
WHERE
"sacrificedValue" > 10000
${ironmanOnly ? 'AND "minion.ironman" = true' : ''}
${SQL.GROUP_BY_U_ID}
ORDER BY "sacrificedValue" DESC
LIMIT 400;`
)
).map((res: any) => ({ ...res, amount: Number.parseInt(res.sacrificedValue) }));

Expand All @@ -229,8 +236,7 @@ async function sacrificeLb(
chunk(list, LB_PAGE_SIZE).map((subList, i) =>
subList
.map(
({ id, amount }, j) =>
`${getPos(i, j)}**${getUsernameSync(id)}:** ${amount.toLocaleString()} GP `
({ full_name, amount }, j) => `${getPos(i, j)}**${full_name}:** ${amount.toLocaleString()} GP `
)
.join('\n')
),
Expand All @@ -240,13 +246,32 @@ async function sacrificeLb(
return lbMsg('Most Value Sacrificed');
}

const mostUniques: { id: string; sacbanklength: number }[] = await prisma.$queryRawUnsafe(
`SELECT u.user_id::text AS id, u.sacbanklength
FROM (
SELECT (SELECT COUNT(*)::int FROM JSONB_OBJECT_KEYS(sacrificed_bank)) sacbanklength, user_id FROM user_stats
${ironmanOnly ? 'INNER JOIN users ON users.id::bigint = user_stats.user_id WHERE "minion.ironman" = true' : ''}
) u
ORDER BY u.sacbanklength DESC LIMIT 10;
const mostUniques: { full_name: string; sacbanklength: number }[] = await prisma.$queryRawUnsafe(
`
SELECT
${SQL.SELECT_FULL_NAME},
u.sacbanklength
FROM (
SELECT
(SELECT COUNT(*)::int FROM JSONB_OBJECT_KEYS(sacrificed_bank)) AS sacbanklength,
u.id AS user_id,
u.username,
u.badges
FROM
user_stats
INNER JOIN
users u ON u.id::bigint = user_stats.user_id
WHERE
sacrificed_bank::text != '{}'
${ironmanOnly ? 'AND "minion.ironman" = true' : ''}
) u
LEFT JOIN
badges b ON b.id = ANY(u.badges)
GROUP BY
u.username, u.sacbanklength
ORDER BY
u.sacbanklength DESC
LIMIT 10;
`
);
doMenu(
Expand All @@ -256,8 +281,8 @@ async function sacrificeLb(
chunk(mostUniques, LB_PAGE_SIZE).map((subList, i) =>
subList
.map(
({ id, sacbanklength }, j) =>
`${getPos(i, j)}**${getUsernameSync(id)}:** ${sacbanklength.toLocaleString()} Unique Sac's`
({ full_name, sacbanklength }, j) =>
`${getPos(i, j)}**${full_name}:** ${sacbanklength.toLocaleString()} Unique Sac's`
)
.join('\n')
),
Expand Down
103 changes: 25 additions & 78 deletions src/mahoji/lib/events.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,49 @@
import type { ItemBank } from 'oldschooljs/dist/meta/types';

import { bulkUpdateCommands } from '@oldschoolgg/toolkit/util';
import { ActivityType, bold, time } from 'discord.js';
import { startBlacklistSyncing } from '../../lib/blacklists';
import { Channel, META_CONSTANTS, globalConfig } from '../../lib/constants';
import { Channel, META_CONSTANTS, badges, globalConfig } from '../../lib/constants';
import { initCrons } from '../../lib/crons';
import { initTickers } from '../../lib/tickers';
import { logWrapFn } from '../../lib/util';
import { mahojiClientSettingsFetch } from '../../lib/util/clientSettings';
import { sendToChannelID } from '../../lib/util/webhook';
import { CUSTOM_PRICE_CACHE } from '../commands/sell';

export async function updateTestBotStatus(online = true) {
try {
if (globalConfig.isProduction) return;
const idMap: Record<string, string> = {
'829398443821891634': '1265571664142270464',
'577488230539067403': '1265582554644217977',
'353484579840983042': '1265582554644217977',
'897549995446779964': '1265582743970910259',
'1158785741028081696': '1265583194108067925'
};
const catChannelID = idMap[globalConfig.clientID];
if (!catChannelID) return;
const cat = await globalClient.channels.fetch(catChannelID);
if (!cat || !cat.isTextBased() || cat.isDMBased()) {
console.log('Could not find status channel');
return;
}

const emoji = online ? '🟢' : '🔴';
let text = '';
if (online) {
text = `${emoji} ${globalClient.user.username} is ONLINE ${emoji}
Turned on ${time(new Date(), 'R')}`;
text = bold(text);
} else {
text = `${emoji} ${globalClient.user.username} is offline ${emoji}
Turned off ${time(new Date(), 'R')}`;
}
const message = await cat.messages
.fetch({ limit: 5 })
.then(messages => messages.filter(m => m.author.id === globalClient.user!.id))
.then(msg => msg.first());
if (!message) {
await cat.send(text);
} else {
await message.edit(text);
}
if (online) {
await globalClient.user.setPresence({
status: 'online',
activities: [
{
name: `${emoji} ONLINE`,
type: ActivityType.Custom
}
]
});
}
} catch (err) {
console.error(err);
}
}
export async function syncCustomPrices() {
const clientData = await mahojiClientSettingsFetch({ custom_prices: true });
for (const [key, value] of Object.entries(clientData.custom_prices as ItemBank)) {
CUSTOM_PRICE_CACHE.set(Number(key), Number(value));
}
}

export const onStartup = logWrapFn('onStartup', async () => {
const syncTestBotCommands = globalConfig.isProduction
? null
: bulkUpdateCommands({
client: globalClient.mahojiClient,
commands: Array.from(globalClient.mahojiClient.commands.values()),
guildID: globalConfig.supportServerID
async function updateBadgeTable() {
const badgesInDb = await prisma.badges.findMany();
for (const [_id, emojiString] of Object.entries(badges)) {
const id = Number(_id);
if (!badgesInDb.find(b => b.id === id)) {
await prisma.badges.create({
data: {
id,
text: emojiString
}
});
}
}
}

export const onStartup = logWrapFn('onStartup', async () => {
initCrons();
initTickers();

const sendStartupMessage = globalConfig.isProduction
? sendToChannelID(Channel.GeneralChannel, {
content: `I have just turned on!\n\n${META_CONSTANTS.RENDERED_STR}`
}).catch(console.error)
: null;
if (globalConfig.isProduction) {
sendToChannelID(Channel.GeneralChannel, {
content: `I have just turned on!\n\n${META_CONSTANTS.RENDERED_STR}`
}).catch(console.error);
}

await Promise.all([
globalClient.application.commands.fetch({
guildId: globalConfig.isProduction ? undefined : globalConfig.supportServerID
}),
updateTestBotStatus(),
sendStartupMessage,
syncTestBotCommands,
startBlacklistSyncing()
]);
globalClient.application.commands.fetch({
guildId: globalConfig.isProduction ? undefined : globalConfig.supportServerID
});
updateBadgeTable();
startBlacklistSyncing();
});
3 changes: 0 additions & 3 deletions src/mahoji/lib/exitHandler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { TimerManager } from '@sapphire/timer-manager';

import { updateTestBotStatus } from './events';

export async function exitCleanup() {
try {
globalClient.isShuttingDown = true;
console.log('Cleaning up and exiting...');
TimerManager.destroy();
await updateTestBotStatus(false);
await Promise.all([globalClient.destroy(), prisma.$disconnect(), roboChimpClient.$disconnect()]);
console.log('\nCleaned up and exited.');
} catch (err) {
Expand Down

0 comments on commit bbc8f08

Please sign in to comment.