-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
110 lines (91 loc) · 2.87 KB
/
main.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
// Load environment variables in local development
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
// Load modules
const discord = require('discord.js');
const mongoose = require('mongoose');
const Game = require("./src/game");
const http = require("http");
const express = require("express");
const cors = require("cors");
// Initialize API for submitting problems
const app = express();
const server = http.createServer(app);
const api = require("./src/api");
const { list, read, testcases } = require("./src/admin");
const { helpCommand } = require("./src/help");
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use("/api", api);
// Create an instance of a Discord client and the Game
const client = new discord.Client();
const game = new Game();
// Connect to the database
mongoose.connect(
process.env.DATABASE_URL,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
},
(err) => {
if (err) {
console.log(err);
return;
}
console.log("Connected to db...");
// Only after this the bot will start reacting to information received from Discord
client.on('ready', () => {
console.log('BlitzProg is online!');
});
// Handle messages sent
client.on('message', message => {
// Making sure that the author of the message is not a bot
if (message.author.bot) return false;
// Bot should only run in the BlitzProg channel
// if (message.channel.id != '863230229488467978') return false;
// Game commands
if (message.content == '!initiate'){
game.initiate(message);
}
if (message.content == '!join') {
game.join(message);
}
if (message.content == '!start') {
game.start(message);
}
if (message.content.startsWith('!submit')) {
game.submit(message);
}
if (message.content == '!end') {
game.end(message);
}
// Admin commands, only committee members can access
if(message.member.roles.cache.find(role => role.name === 'Committee')) {
if(message.content == '!list') {
list(message);
}
if(message.content.startsWith('!read')) {
read(message);
}
if(message.content.startsWith('!testcases')) {
testcases(message);
}
}
// Help command
if (message.content == '!help') {
helpCommand(message);
}
console.log('Hi');
});
// Login to Discord
client.login(process.env.DISCORD_API_KEY);
// Assign a port to run our API
const port = 4040;
server.listen(process.env.PORT || port, () => {
console.log(`API server listening on port *:${port}`);
});
}
);