-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (142 loc) · 4.81 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import 'dotenv/config'
import { Client, Intents, MessageActionRow, MessageSelectMenu } from 'discord.js'
import { compareAsc, isAfter, subMinutes } from 'date-fns'
import { GuildConfigs as GCs } from './db.js'
import { getTwUserId, getTwStreamSubs } from './twitch.js'
const intents = new Intents()
intents.add(
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_SCHEDULED_EVENTS,
Intents.FLAGS.DIRECT_MESSAGES
)
const cl = new Client({
intents,
allowedMentions: { parse: ['everyone'] }
})
cl.once('ready', c => console.log(`ready as ${c.user.tag}`))
cl.on('error', c => console.error(`error: ${c.error}`))
cl.on('interactionCreate', async i => {
if (i.isCommand() && i.commandName === 'vt-select') {
const g = i.guild
for (let chan of g.channels.cache.values()) {
console.log(`type: ${chan.type} id: ${chan.id} name: ${chan.name}`)
}
}
})
// cl.on('guildCreate', async g => {
cl.on('interactionCreate', async i => {
if (!i.isCommand() || !i.commandName === 'setup-ventbot') return
const g = i.guild
const owner = await cl.users.fetch(g.ownerId)
const owchan = await owner.createDM()
owchan.send(`Hi ${owner.username}! I am **Ventbot** and I am here to help you work with (e)vents in the guild *${g.name}*. Right now, let's choose a channel that you want me to ping when an event starts or when you update it while it's in progress..`)
await g.channels.fetch()
const channels = await g.channels.cache
const selNotifyChanOpts = channels
.filter(chan => chan.type === 'GUILD_TEXT' && chan.name)
.mapValues(chan => ({
label: chan.name,
...(chan.topic && { description: chan.topic }),
value: chan.id
}))
console.log([ ...selNotifyChanOpts.values() ])
const selNotifyChan = new MessageActionRow()
selNotifyChan.addComponents(
new MessageSelectMenu()
.setCustomId('notifyChanSelect')
.setPlaceholder('Select a channel...')
.addOptions([ ...selNotifyChanOpts.values() ])
)
const selNCMessage = await owchan.send({
content: 'What\'s the name of your notify channel?',
components: [selNotifyChan]
})
const selNCResponse = await selNCMessage.awaitMessageComponent()
await selNCResponse.reply({
content: 'Cool!'
})
await owchan.send({
content: 'And what\'s your Twitch username?'
})
const twNameResp = await owchan.awaitMessages({ max: 1 })
const twUserName = twNameResp.first().content
const twUserId = await getTwUserId(twUserName)
await GCs.create({
guildId: g.id,
ownerId: owner.id,
notifyChannelId: selNCResponse.values[0],
twitchId: twUserId
})
// await selNCResponse.update({
// content: 'All right, you\'re all set!',
// components: []
// })
owchan.send('Ok, you\'re all set!')
})
async function twStartFunc (e) {
console.log('stream start detected')
console.log(e)
const data = await GCs.findOne({
where: { twitchId: e.broadcaster_user_id }
})
const g = cl.guilds.cache.get(data.guildId)
const ses = await g.scheduledEvents.fetch()
const fses = ses.sort((se0, se1) => compareAsc(
se0.scheduledStartAt,
se1.scheduledStartAt
)).filter(se => isAfter(
se.scheduledStartAt,
subMinutes(new Date(), 30)
))
if (fses.size > 0) {
const firstUp = fses.first()
firstUp.setStatus('ACTIVE')
}
nC = cl.channels.cache.get(data.notifyChannelId)
everyoneId = g.roles.everyone.id
username = cl.users.cache.get(data.ownerId).username
nC.send({
content: `Hey <@${everyoneId}>! ${username} is streaming.`
})
}
async function twEndFunc (e) {
}
// async function twitchListen (twId) {
// }
cl.on('ready', async c => {
c.actives = {}
c.twSubs = {}
for (let g of await c.guilds.fetch()) {
const gData = await GCs.findOne({
where: { guildId: g[0] }
})
c.twSubs[gData.ownerId] = await getTwStreamSubs(
gData.twitchId,
{ startFunc: twStartFunc, endFunc: twEndFunc }
)
}
})
// cl.on('guildScheduledEventUpdate', (oldEv, ev) => {
// console.log('old', oldEv)
// console.log('updated', ev)
// if (oldEv.status === 'SCHEDULED' && ev.status === 'ACTIVE') {
// // stream/event start
// }
// if (oldEv.status === 'ACTIVE' && ev.status === 'ACTIVE') {
// // info has changed while stream/event is ongoing
// }
// })
// cl.on('guildScheduledEventCreate', async ev => {
// await durr.scheduledEvents.fetch({ cache: true })
// })
// cl.on('guildScheduledEventDelete', async dev => {
// await durr.scheduledEvents.fetch({ cache: true })
// })
await cl.login(process.env.TOKEN)
// const channels = durr.channels.cache
// console.log(channels)
// const schevents = durr.scheduledEvents.cache
// const notifyChannel = channels.find(chan => chan.name === 'ventbot-test')
// //const notifyChannelId = notifyChannel.id