Put Offline/Online in their own Events
This commit is contained in:
@@ -71,7 +71,7 @@ module.exports = {
|
|||||||
{ "name": "When a user left this server", "value": "GuildMemberRemove" },
|
{ "name": "When a user left this server", "value": "GuildMemberRemove" },
|
||||||
{ "name": "When it's a users birthday", "value": "Discord/Birthday" },
|
{ "name": "When it's a users birthday", "value": "Discord/Birthday" },
|
||||||
{ "name": "When debug logs are sent", "value": "Discord/Logs" },
|
{ "name": "When debug logs are sent", "value": "Discord/Logs" },
|
||||||
{ "name": "When stream is live", "value": "Twitch/Live" }
|
{ "name": "When stream is live", "value": "Twitch/Online" }
|
||||||
])
|
])
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
)
|
)
|
||||||
@@ -96,7 +96,7 @@ module.exports = {
|
|||||||
{ "name": "When a user left this server", "value": "GuildMemberRemove" },
|
{ "name": "When a user left this server", "value": "GuildMemberRemove" },
|
||||||
{ "name": "When it's a users birthday", "value": "Discord/Birthday" },
|
{ "name": "When it's a users birthday", "value": "Discord/Birthday" },
|
||||||
{ "name": "When debug logs are sent", "value": "Discord/Logs" },
|
{ "name": "When debug logs are sent", "value": "Discord/Logs" },
|
||||||
{ "name": "When stream is live", "value": "Twitch/Live" }
|
{ "name": "When stream is live", "value": "Twitch/Online" }
|
||||||
])
|
])
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,8 +12,11 @@ module.exports = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const streamer = 'x__hel__x'
|
const stream = {
|
||||||
interaction.client.emit('Twitch/Live', streamer)
|
broadcaster_user_login: 'x__hel__x'
|
||||||
|
}
|
||||||
|
|
||||||
|
interaction.client.emit('Twitch/Online', stream)
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content: "Done!",
|
content: "Done!",
|
||||||
flags: MessageFlags.Ephemeral
|
flags: MessageFlags.Ephemeral
|
||||||
|
|||||||
+3
-121
@@ -1,129 +1,11 @@
|
|||||||
// Event triggered by client connected to server
|
const { dClient } = require("../..")
|
||||||
|
|
||||||
const ReconnectingWebSocket = require("reconnecting-websocket");
|
// Event triggered by client connected to server
|
||||||
const { mClient, dClient } = require("../..");
|
|
||||||
const { getIDByName } = require("../../functions");
|
|
||||||
const { WebSocket, EventEmitter } = require("ws");
|
|
||||||
const { default: axios } = require("axios");
|
|
||||||
const events = new EventEmitter()
|
|
||||||
const WebsocketEvents = {
|
|
||||||
CONNECTED: "connected",
|
|
||||||
DISCONNECTED: "disconnected"
|
|
||||||
}
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'Twitch/Connected',
|
name: 'Twitch/Connected',
|
||||||
once: false,
|
once: false,
|
||||||
async execute(address, port) {
|
async execute(address, port) {
|
||||||
console.log(`Connected to ${address}:${port}`)
|
console.log(`Connected to ${address}:${port}`)
|
||||||
const endpoint = `wss://eventsub.wss.twitch.tv/ws`
|
dClient.emit('Twitch/EventSub')
|
||||||
|
|
||||||
const db = mClient.db('clients')
|
|
||||||
const coll = db.collection('credentials')
|
|
||||||
const credentials = await coll.findOne({ service: 'twitch' })
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
'Client-Id': credentials.client_id,
|
|
||||||
'Authorization': `Bearer ${credentials.token.access_token}`,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
var connection
|
|
||||||
var transport = {}
|
|
||||||
var subscribedEvents = {}
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
debug: true
|
|
||||||
}
|
|
||||||
function onConnect(data) {
|
|
||||||
transport.session_id = data.payload.session.id,
|
|
||||||
transport.conncted_at = data.payload.session.connected_at,
|
|
||||||
events.emit(WebsocketEvents.CONNECTED)
|
|
||||||
}
|
|
||||||
function onDisconnect(reason){
|
|
||||||
console.log(`Disconnecting due to reason`, reason)
|
|
||||||
}
|
|
||||||
function connect() {
|
|
||||||
connection = new ReconnectingWebSocket(endpoint, [], {
|
|
||||||
WebSocket: WebSocket,
|
|
||||||
maxRetries: Infinity
|
|
||||||
})
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
connection.onclose = ({ reason }) => onDisconnect(reason)
|
|
||||||
connection.onmessage = ({ data }) => onMessage(data)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
function onMessage(data){
|
|
||||||
const parsed = JSON.parse(data)
|
|
||||||
|
|
||||||
if(options.debug){
|
|
||||||
console.log(`[Debug] EvenSub Data:`, parsed)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.metadata.message_type === 'session_welcome'){
|
|
||||||
return onConnect(parsed)
|
|
||||||
}
|
|
||||||
return onEventMessage(parsed)
|
|
||||||
}
|
|
||||||
function onEventMessage(data){
|
|
||||||
const messageType = data.metadata.message_type
|
|
||||||
|
|
||||||
switch (messageType) {
|
|
||||||
case 'notification':
|
|
||||||
const subscriptionType = data.metadata.subscription_type
|
|
||||||
return subscribedEvents[subscriptionType]?.(data.payload?.event)
|
|
||||||
default:
|
|
||||||
return subscribedEvents[messageType]?.(data.payload)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async function subscribe(type, condition, listener, version = 1){
|
|
||||||
const sessionId = transport.session_id
|
|
||||||
const subscriptionPayload = {
|
|
||||||
type: type,
|
|
||||||
version: version,
|
|
||||||
condition: condition,
|
|
||||||
transport: {
|
|
||||||
method: 'websocket',
|
|
||||||
session_id: sessionId,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const res = await axios.post('https://api.twitch.tv/helix/eventsub/subscriptions',
|
|
||||||
subscriptionPayload, {
|
|
||||||
headers: headers
|
|
||||||
})
|
|
||||||
console.log(`Subscribed to ${type}`)
|
|
||||||
subscribedEvents[type] = listener
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
connect(options)
|
|
||||||
if (options.debug){
|
|
||||||
events.on(WebsocketEvents.CONNECTED, () => {
|
|
||||||
console.log("Connected to EventSub");
|
|
||||||
})
|
|
||||||
|
|
||||||
events.on(WebsocketEvents.DISCONNECTED, () => {
|
|
||||||
console.log("Disconnected from EventSub");
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const conditions = [{
|
|
||||||
broadcaster_user_id: String(await getIDByName("x__hel__x"))
|
|
||||||
}]
|
|
||||||
|
|
||||||
subscribe(
|
|
||||||
"stream.online",
|
|
||||||
conditions[0],
|
|
||||||
stream => {
|
|
||||||
dClient.emit('Twitch/Online', stream)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
subscribe(
|
|
||||||
"stream.offline",
|
|
||||||
conditions[0],
|
|
||||||
stream => {
|
|
||||||
dClient.emit('Twitch/Offline', stream);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
const ReconnectingWebSocket = require("reconnecting-websocket");
|
||||||
|
const { mClient, dClient } = require("../..");
|
||||||
|
const { getIDByName } = require("../../functions");
|
||||||
|
const { WebSocket, EventEmitter } = require("ws");
|
||||||
|
const axios = require("axios")
|
||||||
|
const events = new EventEmitter()
|
||||||
|
const WebsocketEvents = {
|
||||||
|
CONNECTED: "connected",
|
||||||
|
DISCONNECTED: "disconnected"
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
name: 'Twitch/EventSub',
|
||||||
|
once: false,
|
||||||
|
async execute() {
|
||||||
|
const endpoint = `wss://eventsub.wss.twitch.tv/ws`
|
||||||
|
|
||||||
|
const db = mClient.db('clients')
|
||||||
|
const coll = db.collection('credentials')
|
||||||
|
const credentials = await coll.findOne({ service: 'twitch' })
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
'Client-Id': credentials.client_id,
|
||||||
|
'Authorization': `Bearer ${credentials.token.access_token}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
|
||||||
|
var connection
|
||||||
|
var transport = {}
|
||||||
|
var subscribedEvents = {}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
debug: true
|
||||||
|
}
|
||||||
|
function onConnect(data) {
|
||||||
|
transport.session_id = data.payload.session.id,
|
||||||
|
transport.conncted_at = data.payload.session.connected_at,
|
||||||
|
events.emit(WebsocketEvents.CONNECTED)
|
||||||
|
}
|
||||||
|
function onDisconnect(reason) {
|
||||||
|
console.log(`Disconnecting due to reason`, reason)
|
||||||
|
}
|
||||||
|
function connect() {
|
||||||
|
connection = new ReconnectingWebSocket(endpoint, [], {
|
||||||
|
WebSocket: WebSocket,
|
||||||
|
maxRetries: Infinity
|
||||||
|
})
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.onclose = ({ reason }) => onDisconnect(reason, reject)
|
||||||
|
connection.onmessage = ({ data }) => onMessage(data, resolve)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
function onMessage(data) {
|
||||||
|
const parsed = JSON.parse(data)
|
||||||
|
|
||||||
|
if (options.debug) {
|
||||||
|
console.log(`[Debug] EvenSub Data:`, parsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.metadata.message_type === 'session_welcome') {
|
||||||
|
return onConnect(parsed)
|
||||||
|
}
|
||||||
|
return onEventMessage(parsed)
|
||||||
|
}
|
||||||
|
function onEventMessage(data) {
|
||||||
|
const messageType = data.metadata.message_type
|
||||||
|
|
||||||
|
switch (messageType) {
|
||||||
|
case 'notification':
|
||||||
|
const subscriptionType = data.metadata.subscription_type
|
||||||
|
return subscribedEvents[subscriptionType]?.(data.payload?.event)
|
||||||
|
default:
|
||||||
|
return subscribedEvents[messageType]?.(data.payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function subscribe(type, condition, listener, version = 1) {
|
||||||
|
const sessionId = transport.session_id
|
||||||
|
const subscriptionPayload = {
|
||||||
|
type: type,
|
||||||
|
version: version,
|
||||||
|
condition: condition,
|
||||||
|
transport: {
|
||||||
|
method: 'websocket',
|
||||||
|
session_id: sessionId,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const res = await axios.post('https://api.twitch.tv/helix/eventsub/subscriptions',
|
||||||
|
subscriptionPayload, {
|
||||||
|
headers: headers
|
||||||
|
})
|
||||||
|
console.log(`Subscribed to ${type}`)
|
||||||
|
subscribedEvents[type] = listener
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
connect(options)
|
||||||
|
if (options.debug) {
|
||||||
|
events.on(WebsocketEvents.CONNECTED, () => {
|
||||||
|
console.log("Connected to EventSub");
|
||||||
|
})
|
||||||
|
|
||||||
|
events.on(WebsocketEvents.DISCONNECTED, () => {
|
||||||
|
console.log("Disconnected from EventSub");
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const conditions = [{
|
||||||
|
broadcaster_user_id: String(await getIDByName("x__hel__x"))
|
||||||
|
},{
|
||||||
|
broadcaster_user_id: String(await getIDByName("desq_blocki"))
|
||||||
|
}]
|
||||||
|
|
||||||
|
subscribe(
|
||||||
|
"stream.online",
|
||||||
|
conditions[0],
|
||||||
|
stream => {
|
||||||
|
dClient.emit('Twitch/Online', stream)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
subscribe(
|
||||||
|
"stream.offline",
|
||||||
|
conditions[0],
|
||||||
|
stream => {
|
||||||
|
dClient.emit('Twitch/Offline', stream);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
const { updateChatMode } = require('../../functions')
|
||||||
|
module.exports = {
|
||||||
|
name: 'Twitch/Offline',
|
||||||
|
once: false,
|
||||||
|
execute(stream) {
|
||||||
|
console.log(`${stream.broadcaster_user_login} went offline!`)
|
||||||
|
updateChatMode(stream.broadcaster_user_id, "on")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,18 +3,19 @@ const { mClient, dClient } = require("../..")
|
|||||||
const { getStreams } = require('../../functions')
|
const { getStreams } = require('../../functions')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'Twitch/Live',
|
name: 'Twitch/Online',
|
||||||
once: false,
|
once: false,
|
||||||
async execute(streamer) {
|
async execute(stream) {
|
||||||
const db = mClient.db('guilds')
|
const db = mClient.db('guilds')
|
||||||
const res = await db.listCollections().toArray()
|
const res = await db.listCollections().toArray()
|
||||||
|
const streamer = stream.broadcaster_user_login
|
||||||
|
|
||||||
res.forEach(async (guildColl) => {
|
res.forEach(async (guildColl) => {
|
||||||
const guildId = guildColl.name
|
const guildId = guildColl.name
|
||||||
const coll = db.collection(guildId)
|
const coll = db.collection(guildId)
|
||||||
const res = await coll.findOne({ event: 'Twitch/Live' })
|
const res = await coll.findOne({ event: 'Twitch/Online' })
|
||||||
if(!res){
|
if(!res){
|
||||||
console.log('No Live Nofitication Channel')
|
return // ignore if no live channel set
|
||||||
}
|
}
|
||||||
const guild = dClient.guilds.cache.get(guildId)
|
const guild = dClient.guilds.cache.get(guildId)
|
||||||
const channel = guild.channels.cache.get(res.channel)
|
const channel = guild.channels.cache.get(res.channel)
|
||||||
@@ -60,18 +61,10 @@ module.exports = {
|
|||||||
return channel.send({
|
return channel.send({
|
||||||
embeds: [embed],
|
embeds: [embed],
|
||||||
flags: MessageFlags.Ephemeral
|
flags: MessageFlags.Ephemeral
|
||||||
}).then(message => setTimeout(() => {
|
})
|
||||||
message.deleteReply()
|
|
||||||
}, 60000))
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
return console.error
|
||||||
return channel.send({
|
|
||||||
content: `**[ERROR]:** Could not get stream info for *${escapeMarkdown(channelName)}*!\r\n${error}`,
|
|
||||||
ephemeral: true
|
|
||||||
}).then(message => setTimeout(() => {
|
|
||||||
message.deleteReply()
|
|
||||||
}, 60000))
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
+2
-2
@@ -159,8 +159,8 @@ async function updateChatMode(broadcaster_id, setTo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get channel information
|
// Get channel information
|
||||||
async function getChannelInformation(streamer) {
|
async function getChannelInformation(user_login) {
|
||||||
const broadcaster_id = await getIDByName(streamer)
|
const broadcaster_id = await getIDByName(user_login)
|
||||||
return await makeHelixRequest('GET', 'channels', { broadcaster_id });
|
return await makeHelixRequest('GET', 'channels', { broadcaster_id });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user