summaryrefslogtreecommitdiff
path: root/src/commands/voice/itemshop.ts
diff options
context:
space:
mode:
author8cy <[email protected]>2020-04-15 02:58:49 -0700
committer8cy <[email protected]>2020-04-15 02:58:49 -0700
commitf4776cce45fba4380d6652fef5b9a6fbf78f8e16 (patch)
tree1fbdc404a6d4d8b6a8cd17cc26602b6e7d6c639e /src/commands/voice/itemshop.ts
parentThe Start (diff)
downloaddep-core-test.tar.xz
dep-core-test.zip
The Uwuficationtest
Diffstat (limited to 'src/commands/voice/itemshop.ts')
-rw-r--r--src/commands/voice/itemshop.ts209
1 files changed, 209 insertions, 0 deletions
diff --git a/src/commands/voice/itemshop.ts b/src/commands/voice/itemshop.ts
new file mode 100644
index 0000000..f8c0b2b
--- /dev/null
+++ b/src/commands/voice/itemshop.ts
@@ -0,0 +1,209 @@
+import ytdl from 'ytdl-core';
+import { Command } from 'discord.js-commando';
+import { MessageEmbed, Message } from 'discord.js';
+import Youtube from 'simple-youtube-api';
+//const { youtubeAPI } = require('../../config.json');
+import config from '../../config.json';
+const youtube = new Youtube(config['yt-api-key']);
+import emoji from 'emoji-random';
+
+module.exports = class ABeeVoice extends Command {
+ constructor(client) {
+ super(client, {
+ name: 'itemshop',
+ aliases: ['item-shop'],
+ group: 'voice',
+ memberName: 'itemshop',
+ description: 'Use code \'Frozen\' in the Fortnite item shop.',
+ guildOnly: true,
+ clientPermissions: ['SPEAK', 'CONNECT', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
+ userPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
+ examples: ['uwu!itemshop', 'uwu!item-shop']
+ });
+ }
+ async run(msg: Message) {
+ const voiceChannel = msg.member.voice.channel;
+ if (!voiceChannel) return msg.say('Please join a channel and try again. ' + emoji.random());
+
+ const id = 'pBiI1hTwU7E';
+ const video = await youtube.getVideoByID(id).catch(function () {
+ return msg.say(
+ 'There was a problem getting the video you provided. ' + emoji.random()
+ );
+ });
+ // // can be uncommented if you don't want the bot to play live streams
+ // if (video.raw.snippet.liveBroadcastContent === 'live') {
+ // return msg.say("I don't support live streams!");
+ // }
+ // // can be uncommented if you don't want the bot to play videos longer than 1 hour
+ // if (video.duration.hours !== 0) {
+ // return msg.say('I cannot play videos longer than 1 hour');
+ // }
+ // // can be uncommented if you want to limit the queue
+ // if (msg.guild.musicData.queue.length > 10) {
+ // return msg.say(
+ // 'There are too many songs in the queue already, skip or wait a bit'
+ // );
+ // }
+ msg.guild.musicData.queue.push(
+ this.constructSongObj(video, voiceChannel)
+ );
+ if (
+ msg.guild.musicData.isPlaying == false ||
+ typeof msg.guild.musicData.isPlaying == 'undefined'
+ ) {
+ msg.guild.musicData.isPlaying = true;
+ return this.playSong(msg.guild.musicData.queue, msg);
+ } else if (msg.guild.musicData.isPlaying == true) {
+ return msg.say(`${video.title} added to queue. ` + emoji.random());
+ }
+
+ var that = this;
+ msg.channel
+ .awaitMessages(
+ function (msg) {
+ return (msg.content > 0 && msg.content < 6) || msg.content === 'exit';
+ }, {
+ max: 1,
+ time: 60000,
+ errors: ['time']
+ }
+ )
+ .then(function (response) {
+ const videoIndex = parseInt(response.first().content);
+ if (response.first().content === 'exit') return songEmbed.delete();
+ youtube
+ .getVideoByID(videos[videoIndex - 1].id)
+ .then(function (video) {
+ // // can be uncommented if you don't want the bot to play live streams
+ // if (video.raw.snippet.liveBroadcastContent === 'live') {
+ // songEmbed.delete();
+ // return msg.say("I don't support live streams!");
+ // }
+
+ // // can be uncommented if you don't want the bot to play videos longer than 1 hour
+ // if (video.duration.hours !== 0) {
+ // songEmbed.delete();
+ // return msg.say('I cannot play videos longer than 1 hour');
+ // }
+
+ // // can be uncommented if you don't want to limit the queue
+ // if (msg.guild.musicData.queue.length > 10) {
+ // songEmbed.delete();
+ // return msg.say(
+ // 'There are too many songs in the queue already, skip or wait a bit'
+ // );
+ // }
+ msg.guild.musicData.queue.push(
+ that.constructSongObj(video, voiceChannel)
+ );
+ if (msg.guild.musicData.isPlaying == false) {
+ msg.guild.musicData.isPlaying = true;
+ if (songEmbed) {
+ songEmbed.delete();
+ }
+ that.playSong(msg.guild.musicData.queue, msg);
+ } else if (msg.guild.musicData.isPlaying == true) {
+ if (songEmbed) {
+ songEmbed.delete();
+ }
+ msg.say(`${video.title} added to queue. ` + emoji.random());
+ return;
+ }
+ })
+ .catch(function () {
+ if (songEmbed) {
+ songEmbed.delete();
+ }
+ return msg.say(
+ 'An error has occured when trying to get the video ID from Youtube.' + emoji.random()
+ );
+ });
+ });
+ }
+ playSong(queue, msg) {
+ const classThis = this; // use classThis instead of 'this' because of lexical scope below
+ queue[0].voiceChannel
+ .join()
+ .then(function (connection) {
+ const dispatcher = connection
+ .play(
+ ytdl(queue[0].url, {
+ quality: 'highestaudio',
+ highWaterMark: 1024 * 1024 * 10
+ })
+
+ )
+ .on('start', function () {
+ msg.guild.musicData.songDispatcher = dispatcher;
+ const volume = 100 / 100;
+ msg.guild.musicData.volume = volume;
+ dispatcher.setVolume(msg.guild.musicData.volume);
+ const videoEmbed = new MessageEmbed()
+ .setThumbnail(queue[0].thumbnail)
+ .setColor(0xF97DAE)
+ .addField('Now playing:', queue[0].title)
+ .addField('Duration:', queue[0].duration);
+ if (queue[1]) videoEmbed.addField('Next song:', queue[1].title);
+ msg.say(videoEmbed);
+ msg.guild.musicData.nowPlaying = queue[0];
+ return queue.shift();
+ })
+ .on('finish', function () {
+ if (queue.length >= 1) {
+ return classThis.playSong(queue, msg);
+ } else {
+ msg.guild.musicData.isPlaying = false;
+ msg.guild.musicData.nowPlaying = null;
+ msg.guild.musicData.songDispatcher = null;
+ return msg.guild.me.voice.channel.leave();
+ }
+ })
+ .on('error', function (e) {
+ msg.say('Can\'t play song. ' + emoji.random());
+ console.error(e);
+ msg.guild.musicData.queue.length = 0;
+ msg.guild.musicData.isPlaying = false;
+ msg.guild.musicData.nowPlaying = null;
+ msg.guild.musicData.songDispatcher = null;
+ return msg.guild.me.voice.channel.leave();
+ });
+ function timeCheck() {
+ if (dispatcher.streamTime >= 6000) {
+ msg.guild.musicData.isPlaying = false;
+ msg.guild.musicData.nowPlaying = null;
+ msg.guild.musicData.songDispatcher = null;
+ }
+ }
+ setInterval(timeCheck, 500);
+ })
+ .catch(function (e) {
+ console.error(e);
+ return msg.guild.me.voice.channel.leave();
+ });
+ }
+ constructSongObj(video, voiceChannel) {
+ let duration = this.formatDuration(video.duration);
+ if (duration == '00:00') duration = 'Live Stream';
+ return {
+ url: `https://www.youtube.com/watch?v=${video.raw.id}`,
+ title: video.title,
+ duration,
+ thumbnail: video.thumbnails.high.url,
+ voiceChannel
+ };
+ }
+ // prettier-ignore
+ formatDuration(durationObj) {
+ const duration = `${durationObj.hours ? (durationObj.hours + ':') : ''}${
+ durationObj.minutes ? durationObj.minutes : '00'
+ }:${
+ (durationObj.seconds < 10)
+ ? ('0' + durationObj.seconds)
+ : (durationObj.seconds
+ ? durationObj.seconds
+ : '00')
+ }`;
+ return duration;
+ }
+}; \ No newline at end of file