Skip to content

Instantly share code, notes, and snippets.

@z64
Last active July 19, 2018 21:46
Show Gist options
  • Save z64/c9bc19f81772c3c931ca10fe50ed7c55 to your computer and use it in GitHub Desktop.
Save z64/c9bc19f81772c3c931ca10fe50ed7c55 to your computer and use it in GitHub Desktop.
Code Snippet for playing youtube videos (as mp3s) in discord using youtube-dl and discordrb
#Install libsodium (python3-nacl and libsodium-dev worked for me), libopus (libopusfile0 and libopus0 worked for me), ffmpeg
#Install https://github.com/rg3/youtube-dl
require 'discordrb'
$isplaying = 0
bot = Discordrb::Commands::CommandBot.new token: token, client_id: clientid, prefix: '.'
bot.command(:play) do |event, songlink|
if $isplaying == 1
event.message.delete
event.respond 'Already playing music'
break
end
channel = event.user.voice_channel
unless channel.nil?
voice_bot = bot.voice_connect(channel)
system("youtube-dl --no-playlist --max-filesize 50m -o 'music/s.%(ext)s' -x --audio-format mp3 #{songlink}")
event.respond "Playing"
$isplaying = 1
voice_bot.play_file('./music/s.mp3')
voice_bot.destroy
$isplaying = 0
break
end
'You\'re not in any voice channel!'
end
bot.command(:stop) do |event|
$isplaying = 0
event.voice.stop_playing
bot.voices[event.server.id].destroy
nil
end
@Cyan101
Copy link

Cyan101 commented Jun 7, 2017

Might wanna update this to https://github.com/Cyan101/sapphire/blob/master/modules/youtube.rb minus the youtube search stuff, just because this code can only be playing in 1 server >.<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment