Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/discord bot #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions controller/discord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const Discord = require("discord.js");
const axios = require("axios");
const cron = require("node-cron");

// Set up Discord webhook client
const webhookClient = new Discord.WebhookClient({
id: process.env.DISCORD_WEBHOOK_ID,
token: process.env.DISCORD_WEBHOOK_TOKEN,
});

// Function to send Discord webhook notification
function sendWebhookNotification(liveInfo) {
const name = liveInfo.url_key
? liveInfo.url_key.replace("JKT48_", "") + " JKT48"
: liveInfo.room_url_key.replace("JKT48_", "") + " JKT48";
const link = `https://www.jkt48-showroom.com/room/${
liveInfo.url_key ?? liveInfo.room_url_key
}/${liveInfo.id ?? liveInfo.room_id}`;

const description = new Discord.EmbedBuilder()
.setTitle(`${name} is now live on Showroom!`)
.setURL(link)
.addFields(
{
name: "Watch On JKT48 Showroom:",
value: `[Here](${link})`,
inline: true,
},
{
name: "Watch On Showroom:",
value: `[Here](https://www.showroom-live.com/r/${
liveInfo.url_key ?? liveInfo.room_url_key
})`,
inline: true,
}
)
.setDescription(`Silahkan Pilih Link Streaming`)
.setImage(liveInfo.image_url ?? liveInfo.image)
.setColor("#FF0000")
.setTimestamp();

webhookClient.send({
username: "JKT48 SHOWROOM Live Notification",
avatarURL:
"https://image.showroom-cdn.com/showroom-prod/image/avatar/1028686.png?v=87",
embeds: [description],
});
}

async function getLiveInfo() {
const response = await axios.get(
`https://jkt48-showroom-api.vercel.app/api/rooms`
);
const rooms = response.data;

for (const member of rooms) {
if (member.is_live) {
await sendWebhookNotification(member);
} else {
console.log(`${member.name} not live`);
}
}
}

async function getLiveInfoAcademy() {
const response = await axios.get(
`https://jkt48-showroom-api.vercel.app/api/rooms/academy`
);
const roomsAcademy = response.data;

for (const member of roomsAcademy) {
if (member.is_onlive) {
await sendWebhookNotification(member);
} else {
console.log(`${member.main_name}not live`);
}
}
}

const DiscordApi = {
getLiveNotification: async (req, res) => {
try {
cron.schedule("*/5 * * * *", async () => {
if (req.params.type === "regular") {
await getLiveInfo();
} else {
await getLiveInfoAcademy();
}
});

res.send("Live notification sent!");
} catch (error) {
console.log(error);
res.status(500).send("Error sending live notification");
}
},
};

module.exports = DiscordApi;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
"author": "Ikhbal Dwiyantoro",
"license": "MIT",
"dependencies": {
"@discordjs/builders": "^1.5.0",
"axios": "^0.24.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"discord.js": "^14.8.0",
"express": "^4.17.1",
"node-cron": "^3.0.2",
"nodemon": "^2.0.19"
}
}
2 changes: 2 additions & 0 deletions routes/liveRoute.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const express = require('express');
const Lives = require('../controller/lives');
const Discord = require('../controller/discord');
const router = express.Router();

router.get('/:roomId', Lives.getStreamUrl)
router.get('/info/:roomId', Lives.getTitle)
router.get('/comments/:roomId', Lives.getComments)
router.get('/rank/:roomId', Lives.getRank)
router.get('/gift/:roomId', Lives.getAllGift)
router.get('/notification/:type', Discord.getLiveNotification)

module.exports = router;