Skip to content

Commit

Permalink
feat(twitch): show number of streams in past month
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian committed Mar 10, 2023
1 parent 49e0615 commit 592eec7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/components/twitch/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { HelixStream } from '@twurple/api'
import { formatDistanceToNowStrict } from 'date-fns'
import { EmbedBuilder } from 'discord.js'

export const twitchEmbed = (stream: HelixStream) => {
import { formatOrdinal } from '../../utils/index.js'

export const twitchEmbed = (stream: HelixStream, streamsThisMonth: number) => {
const ordinalStreams = formatOrdinal(streamsThisMonth)
const streamingFor = formatDistanceToNowStrict(new Date(stream.startDate))
const title = `${stream.userDisplayName} is streaming ${stream.gameName}!`
const description =

let description =
stream.viewers > 0
? `Streaming for ${streamingFor} with ${stream.viewers} viewers.\nCome say hi!`
: `Just started streaming.\nCome say hi!`
? `Streaming for ${streamingFor} with ${stream.viewers} viewers.`
: `Just started streaming.`

description += `\nCome say hi in their ${ordinalStreams} stream this month!!`

const embed = new EmbedBuilder()
.setTitle(title)
Expand Down
28 changes: 18 additions & 10 deletions src/listeners/twitchStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dotenv/config'

import { ApiClient } from '@twurple/api'
import { AppTokenAuthProvider } from '@twurple/auth'
import { subHours } from 'date-fns'
import { subHours, subMonths } from 'date-fns'
import { Client, Message, TextChannel } from 'discord.js'

import { twitchComponent } from '../components/twitch/component.js'
Expand Down Expand Up @@ -84,6 +84,16 @@ async function cleanupOldStreams() {
}
}

const getMonthlyStreams = async (userId: string) => {
const response = await database('twitch_streams')
.where('userId', userId)
.where('createdAt', '>', subMonths(Date.now(), 1))
.count({ count: 'userId' })
.first()

return Number(response?.count ?? 0)
}

async function announceStreams(client: Client) {
const guild = await client.guilds.fetch(GUILD) // Zeepkist
const channel = await guild.channels.fetch(CHANNEL) // zeep-streams
Expand All @@ -94,14 +104,15 @@ async function announceStreams(client: Client) {
const games = await getGames()

for (const stream of games) {
const streamsThisMonth = await getMonthlyStreams(stream.userId)
const embed = twitchEmbed(stream, streamsThisMonth)
const component = twitchComponent(stream)

if (knownStreams.some(item => item.userName === stream.userName)) {
const data = knownStreams.find(item => item.userName === stream.userName)
if (data === undefined) return

if (data.messageId != undefined && data.viewers != stream.viewers) {
const embed = twitchEmbed(stream)
const component = twitchComponent(stream)

const message = await channel.messages.fetch(data.messageId)
if (message == undefined) {
console.log('Message not found: ' + data.messageId)
Expand All @@ -120,15 +131,12 @@ async function announceStreams(client: Client) {
stream.userName
)

const embed = twitchEmbed(stream)
const component = twitchComponent(stream)

const message = await channel.send({
embeds: [embed],
components: [component]
})

const streamdata: KnownStream = {
const streamData: KnownStream = {
isLive: true,
streamId: stream.id,
messageId: message.id,
Expand All @@ -139,8 +147,8 @@ async function announceStreams(client: Client) {
viewers: stream.viewers
}

await database('twitch_streams').insert(streamdata)
knownStreams.push(streamdata)
await database('twitch_streams').insert(streamData)
knownStreams.push(streamData)
console.log('Added ' + stream.userName + ' to known streams')
}
}
Expand Down

0 comments on commit 592eec7

Please sign in to comment.