generated from zeepk/typescript-node-express-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
127 lines (109 loc) · 3.65 KB
/
index.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
import path from 'path';
import dotenv from 'dotenv';
import express from 'express';
import mongoose from 'mongoose';
import { json } from 'body-parser';
import cors from 'cors';
var bodyParser = require('body-parser');
import { profileRouter } from './src/routes/profiles';
import { workflowRouter } from './src/routes/workflows';
import { Profile } from './src/models/profile';
import Discord, { TextChannel } from 'discord.js';
dotenv.config();
const connectionString = process.env.MONGO_DB_CONN_STRING;
if (connectionString) {
mongoose.connect(connectionString);
}
const client = new Discord.Client({
allowedMentions: {
parse: ['users', 'roles'],
repliedUser: true,
},
intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS'],
});
client.on('ready', () => {
console.log('CodeyBot running...');
});
client.login(process.env.BOT_TOKEN);
// this listens for a message, usefull for !commands
// client.on('messageCreate', async (message) => {
// const content = message.content;
// if (!content) {
// return;
// }
// const response = await getResponse(content);
// if (response) {
// message.channel.send(response);
// }
// // if (content === 'test') {
// // }
// // if (content === '!commands') {
// // message.channel.send(commands.commands);
// // }
// // if (content.indexOf('!villager ') === 0) {
// // const searchTerm = content.split('!villager ')[1];
// // message.channel.send(constants.nookipediaUrlPrefix + searchTerm);
// // }
// });
const app = express();
const allowedOrigins = [process.env.REACT_APP_BASE_URL, process.env.API_URL];
const getCORSOrigin = (origin, callback) => {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error(`Origin "${origin}" is not allowed by CORS`));
}
};
app.use(express.static(path.join(__dirname, 'client', 'build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(json());
app.use(cors({ origin: getCORSOrigin, credentials: true }));
app.use(profileRouter);
app.use(workflowRouter);
app.get('/api/test', (req, res) => {
res.send('ok');
});
app.post('/api/workflows/send', async (req, res) => {
const { channelId, secret, authId, message } = req.body;
if (!channelId || !secret || !authId || !message) {
const errorMessage = 'Invalid arguments';
console.error(errorMessage);
res.status(500).send(errorMessage);
return;
}
const profile = await Profile.findOne({ authId: authId });
if (!profile) {
const errorMessage = 'No profile found';
console.error(errorMessage);
res.status(500).send(errorMessage);
return;
}
if (profile.githubSecret !== secret) {
const errorMessage = 'Invalid secret provided';
console.error(errorMessage);
res.status(500).send(errorMessage);
return;
}
if (!client || !client.channels || !client.channels.cache) {
const errorMessage = 'Error reading client channels';
console.error(errorMessage);
res.status(500).send(errorMessage);
return;
}
const channel = client.channels.cache.get(channelId) as TextChannel;
if (!channel) {
const errorMessage = `Error retrieving channel with id: ${channelId}`;
console.error(errorMessage);
res.status(500).send(errorMessage);
return;
}
channel.send(message);
res.send('success');
});
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
app.listen(process.env.PORT || 8080, () => {
console.log('The application is listening on port 8080!');
});