forked from discord/discord-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (72 loc) · 2.7 KB
/
app.js
File metadata and controls
80 lines (72 loc) · 2.7 KB
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
import 'dotenv/config';
import express from 'express';
import {
InteractionType,
InteractionResponseType,
} from 'discord-interactions';
import { VerifyDiscordRequest, getRandomEmoji, startCronJob } from './utils.js';
import { getProblemOfTheDay } from './problemOfTheDay.js';
import { publishLcDailyChallenge} from './publishLcData.js'
// Create an express app
const app = express();
// Get port, or default to 3000
const PORT = process.env.PORT || 3000;
// Parse request body and verifies incoming requests using discord-interactions package
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
// Store for in-progress games. In production, you'd want to use a DB
const activeGames = {};
/**
* Interactions endpoint URL where Discord will send HTTP requests
*/
app.post('/interactions', async function (req, res) {
// Interaction type and data
const { type, id, data } = req.body;
/**
* Handle verification requests
*/
if (type === InteractionType.PING) {
return res.send({ type: InteractionResponseType.PONG });
}
/**
* Handle slash command requests
* See https://discord.com/developers/docs/interactions/application-commands#slash-commands
*/
if (type === InteractionType.APPLICATION_COMMAND) {
const { name } = data;
console.log(`Incoming request for ${name} command`)
// "test" command
if (name === 'test') {
// Send a message into the channel where command was triggered from
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content: 'hello world ' + getRandomEmoji(),
},
});
}
else if (name === "get-problem-of-day") {
console.log("LC getter started")
// Send a message into the channel where command was triggered from
var lcProblemResponse = {data: {activeDailyCodingChallengeQuestion: {link: ""}}}
await getProblemOfTheDay()
.then(response => {lcProblemResponse = response; console.log(response)})
.catch(error => console.error(error));
lcProblemResponse = JSON.parse(lcProblemResponse)
console.log(typeof(lcProblemResponse))
console.log(`Got ${lcProblemResponse["data"]}`)
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches today's LC problem of the day
content:"Problem of the day is: " + "https://leetcode.com" + lcProblemResponse["data"]["activeDailyCodingChallengeQuestion"]["link"]
},
});
}
}
});
publishLcDailyChallenge()
startCronJob(publishLcDailyChallenge)
app.listen(PORT, () => {
console.log('Listening on port', PORT);
});