Skip to content

Instantly share code, notes, and snippets.

@mome0320
Last active December 11, 2020 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mome0320/0f4e1148187d5a6239cf6a09159f6a83 to your computer and use it in GitHub Desktop.
Save mome0320/0f4e1148187d5a6239cf6a09159f6a83 to your computer and use it in GitHub Desktop.
DiscordInteraction
const express = require('express')
const {verify} = require('noble-ed25519');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');
const app = express()
const port = 80
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.json({hello:'world'})
})
app.post('/discord/integrations',isVailduate, async (req, res) => {
switch(req.body.type){
case 1:
res.status(200).json({"type":1}); break;
case 2:
const token = req.body.token;
const option = req.body.data.options;
switch(req.body.data.name){
case "blep":
const optionselect = option.find(element => element.name =='animal').value;
if(optionselect == "animal_dog"){
res.status(200).json({
"type": 4, //명령어도 보이고 메세지도 보이고!
"data": {
"content": `강아지를 좋아하시는 군요!`,
}
})
}else if(optionselect == "animal_cat"){
res.status(200).json({
"type": 3, //명령어는 안보이지만 메세지는 보이게
"data": {
"content": `고양이를 좋아하시는 군요!`,
}});
}else{
res.status(200).json({
"type": 5 //메세지는 안보내지만 명령어는 보이게
});
}
break;
}
case "neko":
const optionselect = option.find(element => element.name =='isnsfw').value;
const nekourl = optionselect ? "/api/v2/img/lewd" : "api/v2/img/neko";
fetch(`https://nekos.life/${nekourl}`).then(res=>res.json()).then(re=>{
res.status(200).json({
"type": 3,
"data": {
"content": re.url,
}});});
break;
}
})
app.listen(port, () => {
console.log(`작동 시작합니다.`)
})
async function isVailduate(req,res,next){
const publickey = 'PUBLIC_KEY';
const signiture = req.headers['x-signature-ed25519'];
const timestamp = req.headers['x-signature-timestamp'];
const body = JSON.stringify(req.body);
const isSigned = await verify(signiture,Buffer.concat([Buffer.from(timestamp),Buffer.from(body)]),publickey);
return isSigned ? next() : res.status(403).json({code:403,message:"403 Forbidden"});
}
async function send(token,msg){
const id = '683172735081250837';
return fetch(`https://discord.com/api/webhooks/${id}/${token}/`,{
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({content:msg})
},10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment