-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
113 lines (90 loc) · 2.95 KB
/
index.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
111
112
// const { clientId, guildId, token, publicKey } = require('./config.json');
require('dotenv').config()
const APPLICATION_ID = process.env.APPLICATION_ID
const TOKEN = process.env.TOKEN
const PUBLIC_KEY = process.env.PUBLIC_KEY || 'not set'
const GUILD_ID = process.env.GUILD_ID
const axios = require('axios')
const express = require('express');
const { InteractionType, InteractionResponseType, verifyKeyMiddleware } = require('discord-interactions');
const app = express();
// app.use(bodyParser.json());
const discord_api = axios.create({
baseURL: 'https://discord.com/api/',
timeout: 3000,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
"Access-Control-Allow-Headers": "Authorization",
"Authorization": `Bot ${TOKEN}`
}
});
app.post('/interactions', verifyKeyMiddleware(PUBLIC_KEY), async (req, res) => {
const interaction = req.body;
if (interaction.type === InteractionType.APPLICATION_COMMAND) {
console.log(interaction.data.name)
if(interaction.data.name == 'yo'){
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `Yo ${interaction.member.user.username}!`,
},
});
}
if(interaction.data.name == 'dm'){
// https://discord.com/developers/docs/resources/user#create-dm
let c = (await discord_api.post(`/users/@me/channels`,{
recipient_id: interaction.member.user.id
})).data
try{
// https://discord.com/developers/docs/resources/channel#create-message
let res = await discord_api.post(`/channels/${c.id}/messages`,{
content:'Yo! I got your slash command. I am not able to respond to DMs just slash commands.',
})
console.log(res.data)
}catch(e){
console.log(e)
}
return res.send({
// https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data:{
content:'👍'
}
});
}
}
});
app.get('/register_commands', async (req,res) =>{
let slash_commands = [
{
"name": "yo",
"description": "replies with Yo!",
"options": []
},
{
"name": "dm",
"description": "sends user a DM",
"options": []
}
]
try
{
// api docs - https://discord.com/developers/docs/interactions/application-commands#create-global-application-command
let discord_response = await discord_api.put(
`/applications/${APPLICATION_ID}/guilds/${GUILD_ID}/commands`,
slash_commands
)
console.log(discord_response.data)
return res.send('commands have been registered')
}catch(e){
console.error(e.code)
console.error(e.response?.data)
return res.send(`${e.code} error from discord`)
}
})
app.get('/', async (req,res) =>{
return res.send('Follow documentation ')
})
app.listen(8999, () => {
})