-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
104 lines (96 loc) · 2.73 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Discord = require('discord.js')
const fs = require('fs')
const config = require('./discord.config.json')
const icy = require('icy')
const axios = require('axios')
process.on('unhandledRejection', up => {
throw up
})
const client = new Discord.Client()
client.login(config.token)
client.on('ready', () => {
const guild = client.guilds.get(config.guildId)
if (!guild) {
throw new Error('Cannot find guild.')
}
const voiceChannel = guild.channels.find(ch => {
return ch.name === config.channelName && ch.type === 'voice'
})
if (!voiceChannel) {
throw new Error('Cannot find voice channel.')
}
console.log('Joining...')
join(client, voiceChannel)
})
client.on('message', msg => {
try {
if (msg.channel.type !== 'dm') {
return
}
if (msg.author.id === client.user.id) {
return
}
console.log('DM', msg.author.id, msg.content)
const reply = text => {
console.log('DM reply', text)
msg.reply(text)
}
axios.post(config.dmURL, { userId: msg.author.id, username: msg.author.username, content: msg.content })
.then(response => {
return reply(String(response.data))
})
.catch(error => {
return reply(`Sorry, I ran into a problem... ${error}`)
})
} catch(e) {
console.error(e)
}
})
/**
* @param {Discord.Client} client
* @param {Discord.VoiceChannel} voiceChannel
*/
async function join(client, voiceChannel) {
const voiceConnection = await voiceChannel.join()
icy.get('http://cloud.spacet.me:8000/be-music-surge', stream => {
stream.on('metadata', async function(metadata) {
var parsed = icy.parse(metadata)
console.error(parsed)
try {
const presence = await client.user.setActivity(parsed.StreamTitle, {
type: 'PLAYING',
url: 'http://be-music.surge.sh/'
})
} catch (e) {
console.error('Failed to set activity', e)
}
})
setTimeout(() => {
voiceConnection.playStream(stream)
}, 500)
})
}
async function sendAnalytics() {
try {
const response = await axios.get('http://cloud.spacet.me:8000/', { responseType: 'text' })
const text = response.data
const listeners = Math.max(0, require('cheerio').load(text)('table tr:nth-of-type(6) .streamdata').text() - 1)
console.log('Listeners:', listeners)
await axios.get(`https://api.amplitude.com/httpapi`, {
params: {
api_key: config.amplitudeApiKey,
event: JSON.stringify({
user_id: 'bmsurge-bot',
event_type: 'Listeners',
event_properties: {
listeners: listeners
}
})
}
})
} catch (e) {
console.error('Failed to send analytics', e)
}
}
setInterval(sendAnalytics, 300000)
sendAnalytics()