-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
296 lines (239 loc) · 9.98 KB
/
app.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const APP_VERSION = process.env.HEROKU_RELEASE_VERSION ?? "Local"
const APP_BUILD_NUMBER = process.env.HEROKU_SLUG_DESCRIPTION ?? "Local"
const APP_BUILD_DATE = process.env.HEROKU_RELEASE_CREATED_AT
const CREATOR_USER_ID = process.env.CREATOR_USER_ID
const DISCORD_NICKNAME = process.env.DISCORD_NICKNAME
import { Client, GatewayIntentBits, Partials, PermissionFlagsBits, GuildMember, Message, TextChannel } from 'discord.js'
const client = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.MessageContent
], partials: [Partials.User] }) // PARTIALS: https://github.com/discordjs/discord.js/issues/4980#issuecomment-723519865
import {
BotCommand,
BotCommandUserIDRequirement, BotCommandRoleIDRequirement, BotCommandPermissionRequirement,
BotCommandChannelIDRequirement, BotCommandServerIDRequirement,
BotCommandUnionRequirement, BotCommandIntersectionRequirement
} from "./src/botCommand"
import { loginBot, getRestartCommand } from "./src/login"
import { sendMessageResponses } from "./src/messageResponses"
import {
getHelpCommand,
getMessageCommands, getDateCommands, getEmoteSpellCommand, getEchoCommand, getClearCommand,
getRepeatCommand, getSpeakCommand, getCleanReactionsCommand,
getCloseChannelsCommand,
getRerunCommand,
getReactCommand
} from "./src/miscCommands"
import { setupVoiceChannelEventHandler } from "./src/linkedTextChannels"
import { setupMemberStatsEventHandlers, getMessageCountsUpdateCommand, getMessageCountsLeaderboardCommand } from "./src/serverStats"
import { getExportPollResultsCommand } from "./src/poll/sharedPoll"
import { getDMVoteCommand } from "./src/poll/dmPoll"
import { getCreateServerPollCommand } from "./src/poll/serverPoll"
import { getCreatePollCommand, getEditPollCommand, setupPollEditTextInputEventHandlers } from "./src/poll/createPoll"
import { getScheduleCommand } from "./src/scheduledCommands"
import { getCreateRoleGroupCommand } from "./src/roleGroup"
import { setupRoleCounterEventHandlers } from "./src/roleCounter"
import { executeCommandAlias } from "./src/commandAlias"
import { setupFormMessageEventHandlers } from "./src/formChannel"
import { checkWords } from './src/badWords'
const HOME_GUILD_ID = "704218896298934317"
const TECHNICIAN_ROLE_ID = "804147385923403826"
import { Firestore } from "firebase-admin/firestore"
import { initFirestore, initFirestoreCollectionListeners } from "./src/firebase"
var firestoreDB: Firestore
import { initDataFetch } from './src/mapData/mapData'
// Login Bot
loginBot(client)
// Client Init
client.on('ready', async () => {
console.log(`[App] Logged in as ${client.user.tag}!`)
client.user.setPresence({status: "online"})
client.guilds.cache.forEach((guild) => {
guild.members.fetch(client.user).then(member => updateNickname(member))
})
firestoreDB = initFirestore()
initFirestoreCollectionListeners(firestoreDB, client)
setupVoiceChannelEventHandler(client)
setupMemberStatsEventHandlers(client)
setupRoleCounterEventHandlers(client)
setupPollEditTextInputEventHandlers(client, firestoreDB)
setupFormMessageEventHandlers(client)
initCommands()
})
// Nickname Enforcement
client.on('guildMemberUpdate', async (_, newMember) => {
var clientMember = await newMember.guild.members.fetch(client.user)
if (newMember.id !== clientMember.id) { return }
updateNickname(clientMember)
})
function updateNickname(clientMember: GuildMember)
{
if (clientMember.displayName !== DISCORD_NICKNAME)
{
console.log("[App] Updated name in " + clientMember.guild.name + " from " + clientMember.displayName + " to " + DISCORD_NICKNAME)
clientMember.setNickname(DISCORD_NICKNAME)
}
}
// App Build Date String Parsing
function getFormattedBuildDateString(rawBuildDateString: string): string
{
let formattedBuildDateString = "Now"
if (APP_BUILD_DATE)
{
let timeSinceLastBuild = Date.now()-new Date(rawBuildDateString).getTime()
let timeSinceLastBuildComponents = {
minutes: Math.floor(timeSinceLastBuild/(1000*60)%60).toString()+"m",
hours: Math.floor(timeSinceLastBuild/(1000*60*60)%24).toString()+"h",
days: Math.floor(timeSinceLastBuild/(1000*60*60*24)).toString()+"d"
}
if (timeSinceLastBuild < 1000*60)
{
formattedBuildDateString = "Now"
}
else if (timeSinceLastBuild < 1000*60*60)
{
formattedBuildDateString = timeSinceLastBuildComponents.minutes
}
else if (timeSinceLastBuild < 1000*60*60*24)
{
formattedBuildDateString = timeSinceLastBuildComponents.hours
}
else
{
formattedBuildDateString = timeSinceLastBuildComponents.days
}
}
return formattedBuildDateString
}
// Setup Commands
var botCommands: BotCommand[]
function initCommands()
{
var ownerUserRequirement = new BotCommandUserIDRequirement(CREATOR_USER_ID)
var developmentRequirement = new BotCommandIntersectionRequirement(
[
new BotCommandServerIDRequirement(HOME_GUILD_ID),
new BotCommandRoleIDRequirement(TECHNICIAN_ROLE_ID)
]
)
var ownerUserAndDevelopmentRequirement = new BotCommandIntersectionRequirement(
[
ownerUserRequirement,
developmentRequirement
]
)
var botChannelRequirement = new BotCommandUnionRequirement(
[
new BotCommandChannelIDRequirement("720018214448398346"), // #technolog (negativity)
new BotCommandChannelIDRequirement("865504790502965248"), // #betabot (jacksonjude.com)
new BotCommandChannelIDRequirement("781235106832580638"), // #bot-stuff (TMMRAAC)
]
)
var botTesterPermissionRequirement = new BotCommandUnionRequirement([
new BotCommandRoleIDRequirement(TECHNICIAN_ROLE_ID),
new BotCommandRoleIDRequirement("982191695066177557"), // @bot tester (jacksonjude.com)
ownerUserRequirement
])
var manageChannelsPermissionRequirement = new BotCommandUnionRequirement(
[
new BotCommandPermissionRequirement([PermissionFlagsBits.ManageChannels]),
ownerUserRequirement
]
)
var serverAdminPermissionRequirement = new BotCommandUnionRequirement(
[
new BotCommandPermissionRequirement([PermissionFlagsBits.Administrator]),
ownerUserRequirement
]
)
var botAdminPermissionRequirement = new BotCommandUnionRequirement([
new BotCommandRoleIDRequirement(TECHNICIAN_ROLE_ID),
new BotCommandRoleIDRequirement("1002116413051379813"), // @bot admin (jacksonjude.com)
serverAdminPermissionRequirement
])
botCommands = [
...getMessageCommands(),
...getDateCommands(),
getEmoteSpellCommand().withRequirement(botChannelRequirement),
getClearCommand(),
getDMVoteCommand(),
getExportPollResultsCommand(botAdminPermissionRequirement),
getCloseChannelsCommand().withRequirement(manageChannelsPermissionRequirement),
getEchoCommand(botAdminPermissionRequirement),
getScheduleCommand(handleCommandExecution).withRequirement(botAdminPermissionRequirement),
getCreateServerPollCommand().withRequirement(botTesterPermissionRequirement),
getEditPollCommand().withRequirement(botAdminPermissionRequirement),
getCreatePollCommand().withRequirement(botAdminPermissionRequirement),
getCreateRoleGroupCommand().withRequirement(botAdminPermissionRequirement),
getMessageCountsLeaderboardCommand(),
getMessageCountsUpdateCommand().withRequirement(ownerUserRequirement),
getCleanReactionsCommand().withRequirement(ownerUserRequirement),
getReactCommand().withRequirement(ownerUserRequirement),
getRepeatCommand().withRequirement(developmentRequirement),
getSpeakCommand().withRequirement(developmentRequirement),
getRerunCommand(handleCommandExecution).withRequirement(botAdminPermissionRequirement),
getRestartCommand().withRequirement(ownerUserAndDevelopmentRequirement)
]
botCommands.unshift(getHelpCommand(botCommands))
}
// Receive Message
client.on('messageCreate', async msg => {
// console.log(msg.channel.id + " :: " + msg.content)
if (msg.author.id == client.user.id)
{
msg.guild && console.log("[App] Sent '" + msg.content + "' in " + msg.guild.name)
return
}
if (await checkWords(msg)) return
var messageContent = msg.content
if ((msg.mentions.members && msg.mentions.members.has(client.user.id)) || (msg.mentions.roles && msg.mentions.roles.find(role => role.name == DISCORD_NICKNAME)))
{
messageContent = messageContent.replace(/<@!?&?\d+?>/, "")
}
else
{
if (sendMessageResponses(msg)) { return }
return
}
if (client.user.presence.status === "idle")
{
client.user.setPresence({status: 'online'});
(msg.channel as TextChannel).send("\\*yawn\\*")
}
messageContent = messageContent.replace(/^\s*/, "").replace(/\s*$/, "")
console.log("[App] Command from " + msg.author.username + " in " + msg.guild.name + " '" + messageContent + "'")
await handleCommandExecution(messageContent, msg)
})
export async function handleCommandExecution(messageContent: string, msg: Message)
{
const runBotCommands = async function(botCommands: BotCommand[]): Promise<boolean>
{
for (let botCommand of botCommands)
{
if (await botCommand.execute(messageContent, msg, client, firestoreDB)) { return true }
}
return false
}
if (await runBotCommands(botCommands)) { return }
if (await executeCommandAlias(messageContent, msg, handleCommandExecution)) { return }
switch (messageContent)
{
case "info":
(msg.channel as TextChannel).send(`βəταBot **${APP_VERSION}** *(${APP_BUILD_NUMBER}, ${getFormattedBuildDateString(APP_BUILD_DATE)})*\nCreated by <@${CREATOR_USER_ID}> (2020-${new Date(APP_BUILD_DATE ?? "2022-01-02").getFullYear()})\nwith inspiration from We4therman\n*\\*Possibly Powered By DELL OS\\**`)
return
case "ping":
(msg.channel as TextChannel).send("pong")
return
}
}
initDataFetch();
process.on('unhandledRejection', error => {
console.log('[App] Unhandled error: ', error)
})