-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.js
67 lines (58 loc) · 2.09 KB
/
command.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
const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const ed = require('noble-ed25519')
const config = require('./config.json')
const app = new Koa()
const router = new Router()
app.use(bodyParser());
const publicKey = config.publicKey
// response
router.post('/', verify, async( ctx, next ) => {
// console.log(ctx.request.body.data.options)
if(!ctx.request.body) return
switch(ctx.request.body.type) {
case 1:
return ctx.body = { type: 1 }
case 2:
// commands
// if(ctx.request.body.channel_id !== '705218576021192726') return
switch(ctx.request.body.data.name) {
case 'thanks':
return ctx.body = { type: 3, data: {
tts: false,
content: `<@${ctx.request.body.member.user.id}>님이 <@${ctx.request.body.data.options[0].value}>님에게 감사를 표합니다!`
} }
case '고양이':
return ctx.body = {
type: 4, data: {
content: `https://cataas.com/cat?fakeQueryString=${ctx.request.body.id}&size=${ctx.request.body.data.options[0].value}`
}
}
case '이것저것테스트':
return ctx.body = {
type: 3, data: {
content: '이건 테스트에요! 아무것도 없어요!',
flags: 1 << 6
}
}
case '아이유':
return ctx.body = {
type: 4, data: {
content: `https://iu.wonder.im/images/${ctx.request.body.data.options[0].value}/?preventCache=${ctx.request.body.id}`
}
}
}
}
})
async function verify(ctx, next) {
const signed = await ed.verify(ctx.request.headers['x-signature-ed25519'], Buffer.concat([Buffer.from(ctx.request.headers['x-signature-timestamp']), Buffer.from(JSON.stringify(ctx.request.body))]), publicKey )
console.log(signed)
if(!signed) {
ctx.status = 403
return ctx.body = { success: false, error: "Forbidden", code: 403 }
}
next()
}
app.use(router.routes()).use(router.allowedMethods())
app.listen(config.port || 8918)