Skip to content

Commit 6969626

Browse files
authored
Migrate ready off sapphire (#312)
* Migrate ready off sapphire * Rename initAll to initReady * Delete sapphire's ready.ts
1 parent fceb170 commit 6969626

File tree

5 files changed

+61
-96
lines changed

5 files changed

+61
-96
lines changed

src/bot.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { container, LogLevel, SapphireClient, SapphirePrefix } from '@sapphire/f
55
import '@sapphire/plugin-logger/register';
66
import * as colorette from 'colorette';
77
import { inspect } from 'util';
8+
import { initReady } from './events/ready';
89

910
// Set default inspection depth
1011
inspect.defaultOptions.depth = 3;
@@ -38,11 +39,11 @@ container.botPrefix = client.options.defaultPrefix!;
3839

3940
export const startBot = async (): Promise<void> => {
4041
try {
41-
client.on('error', client.logger.error);
42-
client.on('ready', () => {
43-
client.user!.setActivity('CSC | .help');
42+
client.logger.info({
43+
event: 'init'
4444
});
45-
45+
client.on('error', client.logger.error);
46+
client.on('ready', initReady);
4647
client.login();
4748
} catch (e) {
4849
console.log('Bot failure');

src/components/cron.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { container } from '@sapphire/framework';
1+
import { Client } from 'discord.js';
22
import { CronJob } from 'cron';
33
import { MessageEmbed, TextChannel } from 'discord.js';
44
import _ from 'lodash';
@@ -15,21 +15,20 @@ const NOTIF_CHANNEL_ID: string = vars.NOTIF_CHANNEL_ID;
1515
const OFFICE_STATUS_CHANNEL_ID: string = vars.OFFICE_STATUS_CHANNEL_ID;
1616
const OFFICE_HOURS_STATUS_API = 'https://csclub.uwaterloo.ca/~n3parikh/office-status.json';
1717

18-
export const initCrons = async (): Promise<void> => {
19-
createSuggestionCron().start();
18+
export const initCrons = async (client: Client): Promise<void> => {
19+
createSuggestionCron(client).start();
2020
createBonusInterviewerListCron().start();
21-
createCoffeeChatCron().start();
22-
createOfficeStatusCron().start();
21+
createCoffeeChatCron(client).start();
22+
createOfficeStatusCron(client).start();
2323
};
2424

2525
interface officeStatus {
2626
status: number;
2727
time: number;
2828
}
2929
// Updates office status based on webcom API
30-
export const createOfficeStatusCron = (): CronJob =>
30+
export const createOfficeStatusCron = (client: Client): CronJob =>
3131
new CronJob('0 */1 * * * *', async function () {
32-
const { client } = container;
3332
const response = (await (await fetch(OFFICE_HOURS_STATUS_API)).json()) as officeStatus;
3433
const messageChannel = client.channels.cache.get(OFFICE_STATUS_CHANNEL_ID);
3534
if (!messageChannel) {
@@ -51,9 +50,8 @@ export const createOfficeStatusCron = (): CronJob =>
5150
});
5251

5352
// Checks for new suggestions every min
54-
export const createSuggestionCron = (): CronJob =>
53+
export const createSuggestionCron = (client: Client): CronJob =>
5554
new CronJob('0 */1 * * * *', async function () {
56-
const { client } = container;
5755
const createdSuggestions = await getSuggestions(SuggestionState.Created);
5856
const createdSuggestionIds = createdSuggestions.map((a) => Number(a.id));
5957
if (!_.isEmpty(createdSuggestionIds)) {
@@ -89,10 +87,8 @@ export const createBonusInterviewerListCron = (): CronJob =>
8987
});
9088

9189
// Match coffeechat users every week on Friday
92-
export const createCoffeeChatCron = (): CronJob =>
90+
export const createCoffeeChatCron = (client: Client): CronJob =>
9391
new CronJob('0 0 14 * * 5', async function () {
94-
const { client } = container;
95-
9692
const matches = await getMatch();
9793
await CoffeeChatCommand.alertMatches(matches);
9894
await writeHistoricMatches(matches);

src/components/emojis.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { container } from '@sapphire/framework';
2-
import { Emoji } from 'discord.js';
1+
import { Client, Emoji } from 'discord.js';
32

43
const emojiList: { [key: string]: Emoji } = {};
54

6-
export const initEmojis = (): void => {
7-
const { client } = container;
5+
export const initEmojis = (client: Client): void => {
86
client.emojis.cache.forEach((emoji) => {
97
if (emoji.name) emojiList[emoji.name] = emoji;
108
});

src/events/ready.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Client, TextChannel } from 'discord.js';
2+
import { blue, green, magenta, magentaBright, white } from 'colorette';
3+
import { initCrons } from '../components/cron';
4+
import { initEmojis } from '../components/emojis';
5+
import { vars } from '../config';
6+
import { getRepositoryReleases } from '../utils/github';
7+
8+
const dev = process.env.NODE_ENV !== 'production';
9+
10+
const NOTIF_CHANNEL_ID = vars.NOTIF_CHANNEL_ID;
11+
12+
const printBanner = (): void => {
13+
const success = green('+');
14+
15+
const llc = dev ? magentaBright : white;
16+
const blc = dev ? magenta : blue;
17+
18+
const line01 = llc('');
19+
const line02 = llc('');
20+
const line03 = llc('');
21+
22+
// Offset Pad
23+
const pad = ' '.repeat(7);
24+
25+
console.log(
26+
String.raw`
27+
${line01} ${pad}${blc('1.0.0')}
28+
${line02} ${pad}[${success}] Gateway
29+
${line03}${dev ? ` ${pad}${blc('<')}${llc('/')}${blc('>')} ${llc('DEVELOPMENT MODE')}` : ''}
30+
`.trim()
31+
);
32+
};
33+
34+
const sendReady = async (client: Client): Promise<void> => {
35+
const notif = (await client.channels.fetch(NOTIF_CHANNEL_ID)) as TextChannel;
36+
const latestRelease = (await getRepositoryReleases('uwcsc', 'codeybot'))[0];
37+
notif.send(`Codey is up! App version: ${latestRelease.tag_name}`);
38+
};
39+
40+
export const initReady = (client: Client): void => {
41+
printBanner();
42+
client.user!.setActivity('CSC | .help');
43+
sendReady(client);
44+
initCrons(client);
45+
initEmojis(client);
46+
};

src/listeners/ready.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)