Skip to content

Commit

Permalink
Discord bot: Add yt playing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebual committed Jan 27, 2021
1 parent 189efa2 commit 894a574
Show file tree
Hide file tree
Showing 5 changed files with 625 additions and 370 deletions.
10 changes: 6 additions & 4 deletions gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
"local": "npm start"
},
"dependencies": {
"axios": "^0.19.0",
"@discordjs/opus": "^0.3.3",
"axios": "^0.21.0",
"cors": "^2.8.5",
"discord.js": "^12.2.0",
"discord.js": "^12.5.0",
"express": "^4.17.1",
"express-prettify": "^0.1.1",
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^1.6.0",
"jwks-rsa": "^1.12.0",
"md5": "^2.3.0",
"minimist": "^1.2.2",
"moment": "^2.29.1",
"moment-timezone": "^0.5.32",
"otplib": "^12.0.1",
"ws": "^7.2.5"
"ws": "^7.4.2",
"ytdl-core": "^4.4.5"
}
}
33 changes: 24 additions & 9 deletions gateway/src/routes/bedtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const momentTz = require("moment-timezone");
const ytdl = require("ytdl-core");

const { findMember } = require("./discordUtil");
const { findMember, arrayRandom } = require("./discordUtil");

let bedtimes = {};
const bedtimeReplies = [
Expand All @@ -10,6 +11,13 @@ const bedtimeReplies = [
"Oh noes, its that time again for nick!",
"From all of us at http://wwn.nebcorp.com - we wish nick good night.",
];
const bedtimeSounds = [
"https://www.youtube.com/watch?v=eISzv8Ry45U",
"https://www.youtube.com/watch?v=uxkVSLv1dRQ",
"https://www.youtube.com/watch?v=n0XaSvhTYd4",
"https://www.youtube.com/watch?v=_FCp8wCm2Sw",
"https://www.youtube.com/watch?v=4q1Zs3vbX8M",
];
const reBedtime = /\bset.+\b(\w{2,})(?:'s?)?\b.+bed.+to ([\d: \-AMPamp]+)\b/i;
const reBedtimeClear = /\b(?:clear|cancel|remove).+\b(\w{2,})(?:'s?)?\b.+bed/i;
function parseTime(timeString) {
Expand Down Expand Up @@ -48,19 +56,26 @@ function parseTime(timeString) {

return d;
}
function processBedtime(memberId) {
function processBedtime(memberId, playSound = false) {
if (!bedtimes[memberId]) {
return;
}
const member = bedtimes[memberId].member;
member.fetch().then(member => {
member.fetch().then(async member => {
console.log("inside fetch", member, member.voice.channel);
if (member && member.voice.channel) {
member.voice.kick("Bedtime!");
const connection = await member.voice.channel.join();
const stream = connection.play(
ytdl(arrayRandom(bedtimeSounds), { filter: "audioonly" }),
{ volume: 0.5 }
);
stream.on("finish", () => {
member.voice.kick("Bedtime!");
connection.disconnect();
});

bedtimes[memberId].reply(
bedtimeReplies[
Math.floor(Math.random() * bedtimeReplies.length)
].replace("nick", bedtimes[memberId].name)
arrayRandom(bedtimeReplies).replace("nick", bedtimes[memberId].name)
);
}
if (Date.now() > bedtimes[memberId].time + 60000 * 60) {
Expand Down Expand Up @@ -100,9 +115,9 @@ function handleCommandBedtime(msg, bedtimeMatch) {
name: target.displayName,
time: targetTime,
timer: setTimeout(() => {
processBedtime(targetId);
processBedtime(targetId, true);
bedtimes[targetId].interval = setInterval(
() => processBedtime(targetId),
() => processBedtime(targetId, Math.random() < 0.333),
(1 + Math.random() * 4) * 60000
);
}, delay),
Expand Down
5 changes: 5 additions & 0 deletions gateway/src/routes/discordUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ function findMember(members, nick) {
return member;
}

function arrayRandom(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}

module.exports = {
findMember,
arrayRandom,
};
20 changes: 19 additions & 1 deletion gateway/src/routes/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router();
const WebSocket = require("ws");
const fs = require("fs");

const ytdl = require("ytdl-core");
const Discord = require("discord.js");
const client = new Discord.Client();

Expand Down Expand Up @@ -39,7 +40,7 @@ async function getChannel(id) {
return channels[id];
}

client.on("message", msg => {
client.on("message", async msg => {
msg = /** @type Message */ msg;
if (msg.author.id === client.user.id) {
return;
Expand Down Expand Up @@ -80,6 +81,23 @@ client.on("message", msg => {
if (bedtimeClearMatch) {
handleCommandBedtimeClear(msg, bedtimeClearMatch);
}
const playMatch = msg.content.match(/\bplay \b(http.*youtu.*)\b/i);
if (playMatch) {
const url = playMatch[1];
if (msg.member.voice.channel) {
const connection = await msg.member.voice.channel.join();
const stream = connection.play(ytdl(url, { filter: "audioonly" }), {
volume: 0.5,
});
stream.on("finish", () => {
connection.disconnect();
});
} else {
msg.reply(
"If I play a Youtube in a forest and no one is around to hear it, is it still a meme?"
);
}
}
if (msg.content.match(/\bwho\b.*\bam\b.*\bi\b/i)) {
// Send the user's avatar URL
msg.reply(msg.author.displayAvatarURL());
Expand Down
Loading

0 comments on commit 894a574

Please sign in to comment.