-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stream): add twitch stream start notifications
resolves #16
- Loading branch information
Showing
7 changed files
with
1,637 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ApplyOptions } from '@sapphire/decorators'; | ||
import { Listener } from '@sapphire/framework'; | ||
import type { EventSubStreamOnlineEvent } from '@twurple/eventsub/lib/events/EventSubStreamOnlineEvent'; | ||
import type { Snowflake } from 'discord-api-types/globals'; | ||
import { MessageEmbed } from 'discord.js'; | ||
|
||
const GUILDS: { id: Snowflake; channel: Snowflake }[] = [ | ||
{ id: '887670429760749569', channel: '981888025095196702' }, | ||
]; | ||
|
||
@ApplyOptions<Listener.Options>({ | ||
event: 'streamOnline', | ||
}) | ||
export default class StreamOnlineListener extends Listener { | ||
async run(event: EventSubStreamOnlineEvent): Promise<unknown> { | ||
const { client } = this.container; | ||
|
||
const stream = await event.getStream(); | ||
const game = await stream?.getGame(); | ||
|
||
const streamUrl = `https://www.twitch.tv/${event.broadcasterName}`; | ||
|
||
const channels = await Promise.all( | ||
GUILDS.map((sfs) => client.channels.cache.get(sfs.channel)), | ||
); | ||
|
||
const embed = new MessageEmbed() | ||
.setColor('#9146FF') | ||
.setTitle( | ||
`:tv: ${event.broadcasterDisplayName} - Stream en ligne !`, | ||
) | ||
.setURL(streamUrl) | ||
.addField('Titre', stream?.title ?? 'Aucun titre'); | ||
|
||
if (game) { | ||
embed.addField('Jeu', game.name); | ||
embed.setThumbnail(game.boxArtUrl); | ||
} | ||
|
||
embed.setImage(stream?.thumbnailUrl); | ||
|
||
return channels.map((channel) => { | ||
if (channel && channel.isText()) { | ||
return channel.send({ | ||
content: '<@&981902175850602566>', | ||
embeds: [embed], | ||
}); | ||
} | ||
|
||
return Promise.resolve(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { SapphireClient } from '@sapphire/framework'; | ||
import { ApiClient } from '@twurple/api'; | ||
import { ClientCredentialsAuthProvider } from '@twurple/auth'; | ||
import { EventSubListener, ReverseProxyAdapter } from '@twurple/eventsub'; | ||
import type { EventSubStreamOnlineEvent } from '@twurple/eventsub/lib/events/EventSubStreamOnlineEvent'; | ||
import { randomBytes } from 'crypto'; | ||
|
||
const TWITCH_USER_IDS = ['756569499']; | ||
const SUBSCRIPTION_SECRET: string = randomBytes(48).toString('hex'); | ||
|
||
export const twitchAuthProvider = new ClientCredentialsAuthProvider( | ||
process.env.TWITCH_CLIENT_ID ?? '', | ||
process.env.TWITCH_CLIENT_SECRET ?? '', | ||
); | ||
|
||
export const twitchApiClient = new ApiClient({ | ||
authProvider: twitchAuthProvider, | ||
}); | ||
|
||
export const twitchEventSubListener = new EventSubListener({ | ||
apiClient: twitchApiClient, | ||
adapter: new ReverseProxyAdapter({ | ||
hostName: 'ddc.cc4.ch', | ||
port: 1042, | ||
}), | ||
secret: SUBSCRIPTION_SECRET, | ||
}); | ||
|
||
export const subscribeTwitchEvents = async (client: SapphireClient) => { | ||
await twitchEventSubListener.listen().catch(console.error); | ||
const subscriptions = await Promise.all( | ||
TWITCH_USER_IDS.map(async (userId) => | ||
twitchEventSubListener.subscribeToStreamOnlineEvents( | ||
userId, | ||
(e) => { | ||
client.emit('streamOnline', e); | ||
}, | ||
), | ||
), | ||
); | ||
|
||
await Promise.all( | ||
subscriptions.map(async (sub, i) => | ||
client.logger.info(`[${i}] => ${await sub.getCliTestCommand()}`), | ||
), | ||
); | ||
}; | ||
|
||
declare module 'discord.js' { | ||
interface ClientEvents { | ||
streamOnline: [event: EventSubStreamOnlineEvent]; | ||
} | ||
} |