From 1e2c6e2a8b629a3b958bf8c32ef7693431355b05 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 1 May 2020 02:08:33 +0100 Subject: [PATCH] added commando --- commands/join.js | 13 ------- commands/leave.js | 13 ------- commands/music/join.js | 24 +++++++++++++ commands/music/leave.js | 23 ++++++++++++ commands/music/play.js | 43 ++++++++++++++++++++++ commands/music/stop.js | 23 ++++++++++++ index.js | 47 +++++++++--------------- package-lock.json | 79 +++++++++++++++++++++++++++++++++++++++++ package.json | 5 ++- 9 files changed, 213 insertions(+), 57 deletions(-) delete mode 100644 commands/join.js delete mode 100644 commands/leave.js create mode 100644 commands/music/join.js create mode 100644 commands/music/leave.js create mode 100644 commands/music/play.js create mode 100644 commands/music/stop.js diff --git a/commands/join.js b/commands/join.js deleted file mode 100644 index 7ac0d61..0000000 --- a/commands/join.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - name: 'join', - description: 'joins a voice channel', - execute: async(message, args) => { - const voiceChannel = message.member.voice.channel; - - if (!voiceChannel) return message.reply('you need to join a channel!'); - - connection = await voiceChannel.join(); - - return message.channel.send('joined!'); - } -}; \ No newline at end of file diff --git a/commands/leave.js b/commands/leave.js deleted file mode 100644 index 90dd4ed..0000000 --- a/commands/leave.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - name: 'leave', - description: 'leaves a voice channel', - execute: async(message, args) => { - if (!message.member.voice.channel) return message.reply('you need to join a channel!'); - - const voiceChannel = message.member.voice.channel; - - voiceChannel.leave(); - - return message.channel.send('left!'); - } -}; \ No newline at end of file diff --git a/commands/music/join.js b/commands/music/join.js new file mode 100644 index 0000000..4c2f0db --- /dev/null +++ b/commands/music/join.js @@ -0,0 +1,24 @@ +const { Command } = require('discord.js-commando'); + +module.exports = class JoinCommand extends Command { + constructor(client) { + super(client, { + name: 'join', + memberName: 'join', + group: 'music', + description: 'joins a voice channel', + aliases: ['summon'], + guildOnly: true + }); + } + + async run(message) { + const voiceChannel = message.member.voice.channel; + + if (!voiceChannel) return message.reply('you need to join a channel!'); + + const connection = await voiceChannel.join(); + + return message.say('joined!'); + } +} \ No newline at end of file diff --git a/commands/music/leave.js b/commands/music/leave.js new file mode 100644 index 0000000..9db04e2 --- /dev/null +++ b/commands/music/leave.js @@ -0,0 +1,23 @@ +const { Command } = require('discord.js-commando'); + +module.exports = class Leave extends Command { + constructor(client) { + super(client, { + name: 'leave', + memberName: 'leave', + group: 'music', + description: 'leaves a voice channel', + guildOnly: true + }); + } + + async run(message) { + if (!message.member.voice.channel) return message.reply('you need to join a channel!'); + + const voiceChannel = message.member.voice.channel; + + voiceChannel.leave(); + + return message.say('left!'); + } +} \ No newline at end of file diff --git a/commands/music/play.js b/commands/music/play.js new file mode 100644 index 0000000..4686254 --- /dev/null +++ b/commands/music/play.js @@ -0,0 +1,43 @@ +const { Command } = require('discord.js-commando'); +const Youtube = require('simple-youtube-api'); +const ytdl = require('ytdl-core-discord'); +const youtube = new Youtube(process.env.YOUTUBE_API_KEY); + +module.exports = class Play extends Command { + constructor(client) { + super(client, { + name: 'play', + memberName: 'play', + group: 'music', + description: 'plays audio from youtube or facebook', + guildOnly: true, + clientPermissions: ['SPEAK', 'CONNECT'], + args: [ + { + key: 'query', + prompt: 'what do you want to listen to?', + type: 'string', + validate: query => query.length > 0 + } + ] + }); + } + + async run(message, { query }) { + const voiceChannel = message.member.voice.channel; + + if (!voiceChannel) return message.reply('you need to join a channel!'); + + const connection = await voiceChannel.join(); + + if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+/)) { + const dispatcher = connection.play(await ytdl(query), { type: 'opus' }); + + dispatcher.on('start', () => { + return message.reply('youtube video is playing!'); + }); + } else { + return message.reply('currently, i only support youtube video links!'); + } + } +} \ No newline at end of file diff --git a/commands/music/stop.js b/commands/music/stop.js new file mode 100644 index 0000000..54d3e16 --- /dev/null +++ b/commands/music/stop.js @@ -0,0 +1,23 @@ +const { Command } = require('discord.js-commando'); + +module.exports = class Play extends Command { + constructor(client) { + super(client, { + name: 'stop', + memberName: 'stop', + group: 'music', + description: 'stops the player and leaves the channel', + guildOnly: true + }); + } + + async run(message) { + if (!message.member.voice.channel) return message.reply('you need to join a channel!'); + + const voiceChannel = message.member.voice.channel; + + voiceChannel.leave(); + + return message.say('stopped!'); + } +} \ No newline at end of file diff --git a/index.js b/index.js index 477c544..2031d92 100644 --- a/index.js +++ b/index.js @@ -1,38 +1,25 @@ require('dotenv').config(); -const fs = require('fs'); -const Discord = require('discord.js'); +const { CommandoClient } = require('discord.js-commando'); +const path = require('path'); -const client = new Discord.Client(); -client.commands = new Discord.Collection(); - -const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); - -for (const file of commandFiles) { - const command = require(`./commands/${file}`); - client.commands.set(command.name, command); -} - -client.on('message', async message => { - if (!message.content.startsWith(process.env.PREFIX) || message.author.bot) return; - - const args = message.content.slice(process.env.PREFIX.length).split(/ +/); - const commandName = args.shift().toLowerCase(); - - if (!client.commands.has(commandName)) return message.reply('this command does not exist!'); - - const command = client.commands.get(commandName); - - try { - command.execute(message, args); - } catch (error) { - console.error(error); - message.reply('there was an error trying to execute that command!'); - } +const client = new CommandoClient({ + commandPrefix: 'b.' }); +client.registry + .registerDefaultTypes() + .registerGroups([ + ['music', 'Music Commands'] + ]) + .registerDefaultGroups() + .registerDefaultCommands() + .registerCommandsIn(path.join(__dirname, 'commands')); + client.once('ready', () => { - console.log('ready'); + client.user.setActivity('amsa7 lak7el'); }); -client.login(process.env.BOT_TOKEN); \ No newline at end of file +client.login(process.env.BOT_TOKEN); + +client.on('error', console.error); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2034425..775b2a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -327,6 +327,11 @@ "delayed-stream": "~1.0.0" } }, + "common-tags": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/common-tags/-/common-tags-1.8.0.tgz", + "integrity": "sha512-6P6g0uetGpW/sdyUy/iQQCbFF0kWVMSIVSyYz7Zgjcgh8mgw8PQzDNZeyZ5DQ2gM7LBoZPHmnjz8rUthkBG5tw==" + }, "component-emitter": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz", @@ -460,6 +465,14 @@ "ws": "^7.2.1" } }, + "discord.js-commando": { + "version": "github:discordjs/Commando#bdbd84e2a978322f3af12c21e729eb7b7cd4a3dc", + "from": "github:discordjs/Commando", + "requires": { + "common-tags": "^1.8.0", + "require-all": "^3.0.0" + } + }, "dotenv": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", @@ -769,6 +782,11 @@ } } }, + "html-entities": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-1.3.1.tgz", + "integrity": "sha512-rhE/4Z3hIhzHAUKbW8jVcCyuT5oJCXXqhN/6mXXVCpzTmvJnoH2HL/bt3EZ6p55jbFJBeAe1ZNpL5BugLujxNA==" + }, "http-response-object": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/http-response-object/-/http-response-object-3.0.2.tgz", @@ -941,6 +959,11 @@ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" }, + "iso8601-duration": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/iso8601-duration/-/iso8601-duration-1.2.0.tgz", + "integrity": "sha512-ErTBd++b17E8nmWII1K1uZtBgD1E8RjyvwmxlCjPHNqHMD7gmcMHOw0E8Ro/6+QT4PhHRSnnMo7bxa1vFPkwhg==" + }, "isobject": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", @@ -967,6 +990,15 @@ "graceful-fs": "^4.1.11" } }, + "m3u8stream": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/m3u8stream/-/m3u8stream-0.6.5.tgz", + "integrity": "sha512-QZCzhcfUliZfsOboi68QkNcMejPKTEhxE+s1TApvHubDeR8ythm4ViWuYFqgUwZeoHe8q0nsPxOvA3lQvdSzyg==", + "requires": { + "miniget": "^1.6.1", + "sax": "^1.2.4" + } + }, "map-cache": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz", @@ -1013,6 +1045,11 @@ "mime-db": "1.44.0" } }, + "miniget": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/miniget/-/miniget-1.7.0.tgz", + "integrity": "sha512-yrgaDSMRzrfYTkudB4Y6xK8pCb7oAH2bvfv6iPY2m6CedZfs9yK4b/ofh0Vzv08hCYXH/HHkoS8an6fkWtOAQA==" + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -1377,6 +1414,11 @@ "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=" }, + "require-all": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/require-all/-/require-all-3.0.0.tgz", + "integrity": "sha1-Rz1JcEvjEBFc4ST3c4Ox69hnExI=" + }, "resolve-url": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/resolve-url/-/resolve-url-0.2.1.tgz", @@ -1472,6 +1514,15 @@ "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz", "integrity": "sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==" }, + "simple-youtube-api": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/simple-youtube-api/-/simple-youtube-api-5.2.1.tgz", + "integrity": "sha512-vmndP9Bkh35tifn2OwY+th2imSsfYtmDqczgdOW5yEARFzvSoR8VSQFsivJnctfV5QHQUL6VrOpNdbmDRLh9Bg==", + "requires": { + "iso8601-duration": "^1.2.0", + "node-fetch": "^2.6.0" + } + }, "slash": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz", @@ -1850,6 +1901,34 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==" + }, + "ytdl-core": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ytdl-core/-/ytdl-core-2.1.1.tgz", + "integrity": "sha512-rg1h6enMeBzShy4ZMam8k28VQfpSdxwZprCy3t38VvHn9DHzYqVk5Lvevy4QGkASScoYr1sIKGkNo0hh7NGRnA==", + "requires": { + "html-entities": "^1.3.1", + "m3u8stream": "^0.6.3", + "miniget": "^1.7.0", + "sax": "^1.1.3" + } + }, + "ytdl-core-discord": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/ytdl-core-discord/-/ytdl-core-discord-1.2.0.tgz", + "integrity": "sha512-BTE0eslSZzovThOAXbT9KZjsh2spu9y0nl9ZNrGNn4rLip8xocmYpDUEN8X03tTcGUybpzyN8J7wwM2/gNLRiA==", + "requires": { + "@types/node": "^13.11.1", + "prism-media": "^1.2.1", + "ytdl-core": "^2.0.1" + }, + "dependencies": { + "@types/node": { + "version": "13.13.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.4.tgz", + "integrity": "sha512-x26ur3dSXgv5AwKS0lNfbjpCakGIduWU1DU91Zz58ONRWrIKGunmZBNv4P7N+e27sJkiGDsw/3fT4AtsqQBrBA==" + } + } } } } diff --git a/package.json b/package.json index 4f98ace..ca8c12c 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,11 @@ "dependencies": { "@discordjs/opus": "^0.2.1", "discord.js": "^12.2.0", + "discord.js-commando": "github:discordjs/Commando", "dotenv": "^8.2.0", "ffmpeg-static": "^4.2.0", - "sodium": "^3.0.2" + "simple-youtube-api": "^5.2.1", + "sodium": "^3.0.2", + "ytdl-core-discord": "^1.2.0" } }