-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (176 loc) · 5.46 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
const { Client, Util, Intents, MessageEmbed } = require('discord.js')
const express = require('express')
const parser = require('body-parser')
const axios = require('axios')
const lodash = require('lodash')
const config = require('./config.json')
const app = express()
app.use(parser.json())
const intents = new Intents(283468090368)
intents.add(Intents.FLAGS.GUILDS)
const client = new Client({
allowedMentions: {
parse: [
'everyone'
],
replied_user: true
},
intents
})
const defaultProvider = {
name: 'ComfyTheatre',
url: 'https://comfytheatre.co.uk/'
}
const defaultColor = Util.resolveColor([241, 51, 51])
// Define API key middleware
app.use((req, res, next) => {
let resolved = false
if (req.path === '/') {
resolved = true
res.redirect('https://comfytheatre.co.uk/');
}
if (config.token !== null && config.token !== '' && req.header('X-Auth-Token') === config.token) {
resolved = true
next()
}
if (!resolved) {
res.status(401).json('Missing "X-Auth-Token" header').end()
}
})
// Listen to endpoint to announce on Discord
app.post("/announce", (req, res) => {
const channel = config.discord.announcement_channel_id
if (!channel) {
res.status(500).json({ message: 'Missing channel ID' }).end()
return
}
client.channels.fetch(channel)
.then((channel) => {
const title = req.body.title || ''
const message = req.body.message || null
const fields = req.body.fields || null
const target = req.body.target || null
const url = req.body.url || null
const img = req.body.image || null
channel.send({
content: `${title} ${target ? `@${target}` : ''}`,
embeds: [
new MessageEmbed(lodash.merge(
{
url,
title,
description: message,
provider: defaultProvider,
color: defaultColor,
footer: {
text: url
}
},
(fields ? {
fields
} : {}),
(img ? {
image: {
url: img
}
} : {})
))
]
})
console.log('Sent announcement to Discord!')
res.status(200).json({ status: 'OK' }).end()
return
})
.catch((err) => {
console.error(err)
res.status(500).json({ message: 'Could not fetch channel from ID given' }).end()
return
})
})
// Trigger command on message
client.on('messageCreate', (message) => {
// Ignore other bots
if (message.author.bot) {
return
}
const prefix = config?.discord?.prefix || '='
// Ensure the bot only replies when the input command is used
if (message.content.startsWith(prefix)) {
const args = message.content.slice(1).split(' ')
const action = args[0]
const author = message.member
const reply = {
messageReference: message.id
}
// Help reply
if (action === 'help') {
const commands = [
`**${prefix}help** - Shows this help message`,
// `**${prefix}votes** - Show the current film votes`,
`**${prefix}roll #number** - Roll a random number between 1 and #number`
]
return message.channel.send({
content: `Hi ${author}, I'm Comfy-tan! Here are the commands you can use when mentioning me:${commands.map(val => `\n${val}`).join()}`,
reply
})
}
// Film votes reply
// if (action === 'votes') {
// axios.get(`${process.env.COMFYTHEATRE_API_URL}/votes`)
// .then(({ data }) => {
// if (data.success) {
// message.channel.send(`Here are the current film votes for you, ${author}...`, {
// embed: new MessageEmbed({
// title: 'Film Votes',
// description: `Currently winning: **${data.data[0].title}** with ${data.data[0].votes} votes`,
// thumbnail: {
// url: data.data[0].poster
// },
// provider: defaultProvider,
// color: defaultColor,
// fields: data.data.map((item) => {
// return {
// name: item.title,
// value: `${item.votes} votes`,
// }
// })
// })
// })
// } else {
// message.reply('No votes have been submitted yet, or film voting is closed. Come back later!')
// }
// }).catch((err) => {
// message.reply("I can't get that information at the moment. Go shout at Mic.")
// })
// return
// }
// Dice roll
if (action === 'roll') {
let value = args[1] || null
const min = 2
const max = 10000
if (!value || Number.isNaN(value)) {
return message.channel.send({
content: `Please enter a valid number as the first parameter (ie. **${prefix}roll 6**)`,
reply
})
}
value = Math.max(Number.parseInt(value))
if (value < min || value > max) {
return message.channel.send({
content: `I can't roll ${value}! Must be between ${min} and ${max}.`,
reply
})
}
const result = Math.floor(Math.random() * value)
return message.channel.send({
content: `${author} rolled **${result}**`,
reply
})
}
}
})
client.login(config.discord.token)
app.listen(config.port || 3000, () => {
console.log(`Express server running on port ${config.port || 3000}`)
})