-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (54 loc) · 1.77 KB
/
app.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
require("dotenv").config();
const Discord = require("discord.js");
const fs = require("fs");
// Express Server
const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const client = new Discord.Client();
client.commands = new Discord.Collection();
const prefix = process.env.PREFIX;
client.login(process.env.DISCORD_BOT_TOKEN);
let channel;
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('ready', async () => {
channel = await client.channels.fetch(process.env.DISCORD_CHANNEL_NUMBER);
console.log("Bot is ready");
})
client.on('message', (msg) => {
// ignore if a bot sends the message
if (msg.author.bot || !msg.content.startsWith(prefix)) return;
args = msg.content.slice(prefix.length).trim().split(/ +/);
command = args.shift().toLowerCase();
if (!client.commands.get(command)) {
return msg.reply("No such command found");
}
command = client.commands.get(command);
try {
command.execute(msg, args);
} catch (error) {
console.log(error);
return msg.channel.send("There was an error executing the command");
}
})
client.on("githubMessage", (body) => {
const action = body.action;
const issueUrl = body.issue.html_url;
if (action === "opened") {
const message = `New issue created : ${issueUrl}`;
channel.send(message);
}
})
app.post("/github", (req, res) => {
client.emit("githubMessage", req.body);
return res.status(200).json({ success: true });
})
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log("Server started on port : ", port);
})