Skip to content

Commit

Permalink
fix(tasks): remove bull prefix as unsupported & fix task cache usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutonite committed Feb 22, 2024
1 parent 745d2d4 commit 9d05c4e
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 111 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
DISCORD_TOKEN=

REDIS_URL=redis://127.0.0.1:6379
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DATABASE=0

FIREBASE_CREDENTIALS_PATH=

Expand Down
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,21 @@
"@sapphire/plugin-editable-commands": "^4.0.2",
"@sapphire/plugin-hmr": "^3.0.1",
"@sapphire/plugin-logger": "^4.0.2",
"@sapphire/plugin-pattern-commands": "^6.0.2",
"@sapphire/plugin-scheduled-tasks": "^10.0.1",
"@sapphire/plugin-subcommands": "^6.0.3",
"@sapphire/ratelimits": "^2.4.9",
"@sapphire/result": "^2.6.6",
"@sapphire/stopwatch": "^1.5.2",
"@sapphire/utilities": "^3.15.3",
"@twurple/api": "^7.0.10",
"@twurple/auth": "^7.0.10",
"@twurple/eventsub-http": "^7.0.10",
"any-date-parser": "^1.5.4",
"bufferutil": "^4.0.8",
"bullmq": "^5.2.1",
"common-tags": "^1.8.2",
"dayjs": "^1.11.10",
"discord.js": "^14.14.1",
"dotenv": "^16.4.5",
"erlpack": "github:discord/erlpack",
"firebase-admin": "^12.0.0",
"firelord": "^2.7.1",
"ioredis": "^5.3.2",
"ms": "^2.1.3",
"protobufjs": "^7.2.6",
"ts-proto": "^1.167.8",
Expand Down
47 changes: 4 additions & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import 'dotenv/config';
import './lib/setup';

import { subscribeTwitchEvents } from './services/twitch-events.service';
import { container, LogLevel, SapphireClient } from '@sapphire/framework';
import { LogLevel, SapphireClient } from '@sapphire/framework';
import { GatewayIntentBits } from 'discord.js';

const { host, port, password, db } = container.redisClient.options;
const client = new SapphireClient({
defaultPrefix: '!',
intents: [
Expand All @@ -26,17 +25,22 @@ const client = new SapphireClient({
tasks: {
bull: {
connection: {
host: host ?? '',
port: port ?? 6379,
password: password ?? '',
db: db ?? 0,
host: process.env.REDIS_HOST ?? 'localhost',
port: parseInt(process.env.REDIS_PORT ?? '6379', 10),
password: process.env.REDIS_PASSWORD,
db: parseInt(process.env.REDIS_DB ?? '0', 10),
},
defaultJobOptions: {
removeOnComplete: true,
removeOnFail: true,
},
prefix: 'ddc.queue-',
},
queue: 'ddc-tasks',
},
loadMessageCommandListeners: true,
loadDefaultErrorListeners: true,
loadSubcommandErrorListeners: true,
loadScheduledTaskErrorListeners: true,
});

const main = async () => {
Expand All @@ -58,8 +62,6 @@ const main = async () => {

subscribeTwitchEvents(client).catch(client.logger.error);
});

client.logger.info('logged in');
} catch (error) {
client.logger.fatal(error);
client.destroy();
Expand Down
10 changes: 9 additions & 1 deletion src/database/guild-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import { getFirelord } from 'firelord';
export type CounterId = 'beers' | 'hecheTime' | 'rentschTime';

export type GuildCounters = { [key in CounterId]: number };
export interface ChannelConfiguration {
menus?: Snowflake;
stream?: Snowflake;
}

export type GuildDocument = MetaTypeCreator<
{ counters: GuildCounters },
{
counters: GuildCounters;
channels: ChannelConfiguration;
},
'guilds',
Snowflake
>;
Expand Down
7 changes: 2 additions & 5 deletions src/lib/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import '@sapphire/plugin-editable-commands/register';
import '@sapphire/plugin-hmr/register';
import '@sapphire/plugin-logger/register';
import '@sapphire/plugin-scheduled-tasks/register';
import '@sapphire/plugin-subcommands/register';

import dayjsParser from '../utils/dayjs-parser';
import {
ApplicationCommandRegistries,
container,
RegisterBehavior,
} from '@sapphire/framework';

import dayjs from 'dayjs';

import 'dayjs/locale/fr-ch';
import duration from 'dayjs/plugin/duration';
import localizedFormat from 'dayjs/plugin/localizedFormat';
Expand All @@ -23,8 +24,6 @@ import weekday from 'dayjs/plugin/weekday';
import { cert, initializeApp } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';

import IORedis from 'ioredis';

dayjs.extend(utc);
dayjs.extend(weekOfYear);
dayjs.extend(weekday);
Expand All @@ -40,8 +39,6 @@ initializeApp({
});

container.database = getFirestore();
container.redisClient = new IORedis(process.env.REDIS_URL ?? '');

ApplicationCommandRegistries.setDefaultBehaviorWhenNotIdentical(
RegisterBehavior.BulkOverwrite,
);
62 changes: 42 additions & 20 deletions src/listeners/stream-online.listener.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { guildRef } from '../database/guild-data';
import { ApplyOptions } from '@sapphire/decorators';
import { isTextChannel } from '@sapphire/discord.js-utilities';
import { Listener } from '@sapphire/framework';
import type { EventSubStreamOnlineEvent } from '@twurple/eventsub-base';
import { type Snowflake, EmbedBuilder } from 'discord.js';

const GUILDS: { id: Snowflake; channel: Snowflake }[] = [
{ id: '887670429760749569', channel: '981888025095196702' },
];
import { EmbedBuilder, Snowflake } from 'discord.js';
import { getDocs } from 'firelord';

@ApplyOptions<Listener.Options>({
event: 'streamOnline',
})
export default class StreamOnlineListener extends Listener {
async run(event: EventSubStreamOnlineEvent): Promise<unknown> {
const { client } = this.container;
async run(event: EventSubStreamOnlineEvent) {
const guilds = await getDocs(guildRef.collection()).then((docs) =>
docs.docs
.filter((d) => !!d.data().channels?.stream)
.map((doc) => ({
id: doc.id,
channel: doc.data().channels.stream!,
})),
);

const stream = await event.getStream();
const game = await stream?.getGame();

const streamUrl = `https://www.twitch.tv/${event.broadcasterName}`;

const channels = GUILDS.map((sfs) =>
client.channels.cache.get(sfs.channel),
);

const embed = new EmbedBuilder()
.setColor('#9146FF')
.setTitle(
Expand All @@ -42,15 +42,37 @@ export default class StreamOnlineListener extends Listener {

embed.setImage(stream?.thumbnailUrl ?? null);

return channels.map((channel) => {
if (channel && isTextChannel(channel)) {
return channel.send({
content: '<@&981902175850602566>',
embeds: [embed],
});
}
await Promise.all(
guilds.map(async ({ id, channel }) =>
this.handleGuildMessage(id, channel, embed),
),
);
}

private async handleGuildMessage(
guildId: Snowflake,
channelId: Snowflake,
embed: EmbedBuilder,
) {
const { client, logger } = this.container;

const guild = await client.guilds.fetch(guildId);
if (!guild) {
logger.debug(`Could not find guild with id ${guildId}`);
return;
}

const channel = await guild.channels.fetch(channelId);
if (!channel || !channel.isTextBased()) {
logger.debug(
`Could not find menus channel in guild ${guild.toString()}.`,
);
return;
}

return Promise.resolve();
await channel.send({
content: '<@&981902175850602566>',
embeds: [embed],
});
}
}
Loading

0 comments on commit 9d05c4e

Please sign in to comment.