Made significant progress toward a working song system
This commit is contained in:
parent
82f775e6c7
commit
b2f360c9ff
|
@ -37,6 +37,10 @@ config.USER_SOUND_PAUSE_TARGET = 1
|
||||||
-- DEFAULT VALUE: 0.25
|
-- DEFAULT VALUE: 0.25
|
||||||
config.VOLUME = 0.25
|
config.VOLUME = 0.25
|
||||||
|
|
||||||
|
-- Ratio that must be met or exceeded to trigger a song skip.
|
||||||
|
-- DEFAULT VALUE: 0.5
|
||||||
|
config.SKIP_RATIO = 0.5
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
-- ADMIN CONFIGURATION
|
-- ADMIN CONFIGURATION
|
||||||
|
@ -124,6 +128,18 @@ config.CHANNEL_DOES_NOT_EXIST_MSG = "The channel you specified does not exist."
|
||||||
-- DEFAULT VALUE: "The URL you submitted does not match the required format. Please submit a valid YouTube URL."
|
-- DEFAULT VALUE: "The URL you submitted does not match the required format. Please submit a valid YouTube URL."
|
||||||
config.INVALID_URL_MSG = "The URL you submitted does not match the required format. Please submit a valid YouTube URL."
|
config.INVALID_URL_MSG = "The URL you submitted does not match the required format. Please submit a valid YouTube URL."
|
||||||
|
|
||||||
|
-- Message shown to users when they attempt to execute the play command when music is already playing.
|
||||||
|
-- DEFAULT VALUE: "A music track is already playing!"
|
||||||
|
config.MUSIC_PLAYING_MSG = "A music track is already playing!"
|
||||||
|
|
||||||
|
-- Message shown to users when they attempt to use the stop command when no music is playing.
|
||||||
|
-- DEFAULT VALUE: "There is no music playing at the moment."
|
||||||
|
config.NO_MUSIC_PLAYING_MSG = "There is no music playing at the moment."
|
||||||
|
|
||||||
|
-- Message shown to users when they attempt to use the play command when there are no songs in the queue.
|
||||||
|
-- DEFAULT VALUE: "There are no songs currently in the queue. Use " .. config.COMMAND_PREFIX .. "add to add a song to the queue."
|
||||||
|
config.NO_SONGS_AVAILABLE = "There are no songs currently in the queue. Use " .. config.COMMAND_PREFIX .. "add to add a song to the queue."
|
||||||
|
|
||||||
|
|
||||||
----------------------
|
----------------------
|
||||||
-- HTML CONFIGURATION
|
-- HTML CONFIGURATION
|
||||||
|
@ -149,4 +165,14 @@ config.SONG_ADDED_HTML = [[
|
||||||
<b>%s</b> has added "%s" to the queue.
|
<b>%s</b> has added "%s" to the queue.
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
-- Message shown to channel when a song is resumed with the play command.
|
||||||
|
config.SONG_PLAY_HTML = [[
|
||||||
|
<b>%s</b> resumed audio playback.
|
||||||
|
]]
|
||||||
|
|
||||||
|
-- Message shown to channel when a song is paused by a user.
|
||||||
|
config.SONG_PAUSED_HTML = [[
|
||||||
|
<b>%s</b> has paused the song.
|
||||||
|
]]
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
|
@ -12,15 +12,22 @@
|
||||||
import pafy
|
import pafy
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from os.path import isfile
|
from os.path import isfile
|
||||||
from os import remove
|
from os import remove, system
|
||||||
|
|
||||||
url = argv[1]
|
url = argv[1]
|
||||||
video = pafy.new(url)
|
video = pafy.new(url)
|
||||||
|
|
||||||
|
def encode_file(stream, downloaded, ratio, rate, eta):
|
||||||
|
if ratio == 1:
|
||||||
|
print('Encoding!')
|
||||||
|
system("ffmpeg -i song.ogg -ar 48000 -ac 1 song-converted.ogg -y")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
video.oggstreams[0].download(filepath = "song.ogg", quiet = True)
|
video.oggstreams[0].download(filepath = "song.ogg", quiet = True, callback = encode_file)
|
||||||
if isfile(".video_fail"):
|
if isfile(".video_fail"):
|
||||||
remove(".video_fail")
|
remove(".video_fail")
|
||||||
except:
|
except:
|
||||||
with open(".video_fail", "w+") as f:
|
with open(".video_fail", "w+") as f:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
local config = require("config")
|
local config = require("config")
|
||||||
local song_queue = require("song_queue")
|
local song_queue = require("song_queue")
|
||||||
|
|
||||||
|
local skippers = {}
|
||||||
|
|
||||||
function piepan.onConnect()
|
function piepan.onConnect()
|
||||||
print("MumbleDJ has connected to the server!")
|
print("MumbleDJ has connected to the server!")
|
||||||
local user = piepan.users["MumbleDJ"]
|
local user = piepan.users["MumbleDJ"]
|
||||||
|
@ -40,7 +42,16 @@ function parseCommand(message)
|
||||||
if config.OUTPUT then
|
if config.OUTPUT then
|
||||||
print(message.user.name .. " has told the bot to start playing music.")
|
print(message.user.name .. " has told the bot to start playing music.")
|
||||||
end
|
end
|
||||||
piepan.me.channel:play("song.ogg")
|
if song_queue.getLength() == 0 then
|
||||||
|
message.user:send(config.NO_SONGS_AVAILABLE)
|
||||||
|
else
|
||||||
|
if piepan.Audio.isPlaying() then
|
||||||
|
message.user:send(config.MUSIC_PLAYING_MSG)
|
||||||
|
else
|
||||||
|
piepan.me.channel:play("song-converted.ogg", config.VOLUME, nextSong)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
message.user:send(config.NO_PERMISSION_MSG)
|
message.user:send(config.NO_PERMISSION_MSG)
|
||||||
end
|
end
|
||||||
|
@ -51,6 +62,13 @@ function parseCommand(message)
|
||||||
if config.OUTPUT then
|
if config.OUTPUT then
|
||||||
print(message.user.name .. " has told the bot to pause music playback.")
|
print(message.user.name .. " has told the bot to pause music playback.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if piepan.Audio.isPlaying() then
|
||||||
|
piepan.me.channel:send(string.format(config.SONG_PAUSED_HTML, message.user.name))
|
||||||
|
piepan.Audio.stop()
|
||||||
|
else
|
||||||
|
message.user:send(config.NO_MUSIC_PLAYING_MSG)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
message.user:send(config.NO_PERMISSION_MSG)
|
message.user:send(config.NO_PERMISSION_MSG)
|
||||||
end
|
end
|
||||||
|
@ -60,7 +78,8 @@ function parseCommand(message)
|
||||||
if has_permission then
|
if has_permission then
|
||||||
if config.OUTPUT then
|
if config.OUTPUT then
|
||||||
print(message.user.name .. " has told the bot to add the following URL to the queue: " .. argument .. ".")
|
print(message.user.name .. " has told the bot to add the following URL to the queue: " .. argument .. ".")
|
||||||
if not song_queue.addSong(argument) then
|
if not song_queue.addSong(argument, message.user.name) then
|
||||||
|
print(debug.traceback())
|
||||||
message.user:send(config.INVALID_URL_MSG)
|
message.user:send(config.INVALID_URL_MSG)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -74,6 +93,8 @@ function parseCommand(message)
|
||||||
if config.OUTPUT then
|
if config.OUTPUT then
|
||||||
print(message.user.name .. " has voted to skip the current song.")
|
print(message.user.name .. " has voted to skip the current song.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
skip(message.user.name)
|
||||||
else
|
else
|
||||||
message.user:send(config.NO_PERMISSION_MSG)
|
message.user:send(config.NO_PERMISSION_MSG)
|
||||||
end
|
end
|
||||||
|
@ -83,6 +104,9 @@ function parseCommand(message)
|
||||||
if has_permission then
|
if has_permission then
|
||||||
if config.OUTPUT then
|
if config.OUTPUT then
|
||||||
print(message.user.name .. " has changed the volume to the following: " .. argument .. ".")
|
print(message.user.name .. " has changed the volume to the following: " .. argument .. ".")
|
||||||
|
if 0.1 < argument < 2 then
|
||||||
|
config.VOLUME = argument
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif command == "move" then
|
elseif command == "move" then
|
||||||
|
@ -105,16 +129,47 @@ function parseCommand(message)
|
||||||
if config.OUTPUT then
|
if config.OUTPUT then
|
||||||
print(message.user.name .. " has told the bot to kill itself.")
|
print(message.user.name .. " has told the bot to kill itself.")
|
||||||
end
|
end
|
||||||
|
kill()
|
||||||
else
|
else
|
||||||
message.user:send(config.NO_PERMISSION_MSG)
|
message.user:send(config.NO_PERMISSION_MSG)
|
||||||
end
|
end
|
||||||
|
elseif command == "test" then
|
||||||
|
piepan.me.channel:play("song-converted.ogg", config.VOLUME, nextSong)
|
||||||
else
|
else
|
||||||
message.user:send("The command you have entered is not valid.")
|
message.user:send("The command you have entered is not valid.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function skip()
|
function skip(username)
|
||||||
return
|
local user_count = 0
|
||||||
|
local skipper_count = 0
|
||||||
|
local already_skipped = false
|
||||||
|
for name,_ in pairs(piepan.users) do
|
||||||
|
user_count = user_count + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
user_count = user_count - 1 -- So that we do not count the bot.
|
||||||
|
|
||||||
|
for name,_ in pairs(skippers) do
|
||||||
|
if name == username then
|
||||||
|
already_skipped = true
|
||||||
|
end
|
||||||
|
skipper_count = skipper_count + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if not already_skipped then
|
||||||
|
table.insert(skippers, username)
|
||||||
|
skipper_count = skipper_count + 1
|
||||||
|
local skip_ratio = skipper_count / user_count
|
||||||
|
if skip_ratio > config.SKIP_RATIO then
|
||||||
|
piepan.me.channel:send("The number of votes required for a skip has been met. Skipping song!")
|
||||||
|
nextSong()
|
||||||
|
else
|
||||||
|
piepan.me.channel:send("<b>" .. username .. "</b> has voted to skip this song.")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
message.user:send("You have already voted to skip this song.")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function move(chan)
|
function move(chan)
|
||||||
|
@ -129,7 +184,9 @@ function move(chan)
|
||||||
end
|
end
|
||||||
|
|
||||||
function kill()
|
function kill()
|
||||||
return
|
os.remove("song.ogg")
|
||||||
|
os.remove("song-converted.ogg")
|
||||||
|
os.exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
function checkPermissions(ADMIN_COMMAND, username)
|
function checkPermissions(ADMIN_COMMAND, username)
|
||||||
|
@ -149,3 +206,12 @@ function isAdmin(username)
|
||||||
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function nextSong()
|
||||||
|
skippers = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
function file_exists(file)
|
||||||
|
local f=io.open(file,"r")
|
||||||
|
if f~=nil then io.close(f) return true else return false end
|
||||||
|
end
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
-- used for queueing up songs. --
|
-- used for queueing up songs. --
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
local deque = require("deque")
|
local deque = require("deque")
|
||||||
|
local config = require("config")
|
||||||
|
|
||||||
local song_queue = deque.new()
|
local song_queue = deque.new()
|
||||||
|
|
||||||
|
@ -25,12 +26,10 @@ function SongQueue.addSong(url, username)
|
||||||
local video_id = string.match(url, pattern)
|
local video_id = string.match(url, pattern)
|
||||||
if video_id ~= nil and string.len(video_id) < 20 then
|
if video_id ~= nil and string.len(video_id) < 20 then
|
||||||
print("YouTube URL is valid!")
|
print("YouTube URL is valid!")
|
||||||
piepan.Thread.new(getYoutubeInfo, youtubeInfoCompleted, {video_id, username})
|
--piepan.Thread.new(getYoutubeInfo, youtubeInfoCompleted, {video_id, username})
|
||||||
return true
|
getYoutubeInfo(video_id, username)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function getYoutubeInfo(id, username)
|
function getYoutubeInfo(id, username)
|
||||||
|
@ -49,25 +48,30 @@ function getYoutubeInfo(id, username)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
print("Finished getting info.")
|
||||||
|
youtubeInfoCompleted({
|
||||||
id = id,
|
id = id,
|
||||||
title = name,
|
title = name,
|
||||||
duration = string.format("%d:%02d", duration / 60, duration % 60),
|
duration = string.format("%d:%02d", duration / 60, duration % 60),
|
||||||
thumbnail = thumbnail,
|
thumbnail = thumbnail,
|
||||||
username = username
|
username = username
|
||||||
}
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function youtubeInfoCompleted(info)
|
function youtubeInfoCompleted(info)
|
||||||
if info == nil then
|
if info == nil then
|
||||||
return
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
song_queue.push_left(info)
|
song_queue:push_right(info)
|
||||||
|
|
||||||
if song_queue:length() == 1 then
|
if song_queue:length() == 1 then
|
||||||
os.execute("python download_audio.py")
|
os.execute("python download_audio.py " .. info.id)
|
||||||
piepan.me.channel:play("song.ogg")
|
while not file_exists("song-converted.ogg") do
|
||||||
|
os.execute("sleep " .. tonumber(2))
|
||||||
|
end
|
||||||
|
print("we done here")
|
||||||
|
piepan.me.channel:play("song-converted.ogg", config.VOLUME, nextSong)
|
||||||
end
|
end
|
||||||
|
|
||||||
if piepan.Audio:isPlaying() then
|
if piepan.Audio:isPlaying() then
|
||||||
|
@ -77,6 +81,8 @@ function youtubeInfoCompleted(info)
|
||||||
local message = string.format(config.SONG_ADDED_HTML, info.username, info.title)
|
local message = string.format(config.SONG_ADDED_HTML, info.username, info.title)
|
||||||
piepan.me.channel:send(message)
|
piepan.me.channel:send(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function SongQueue.getNextSong(url)
|
function SongQueue.getNextSong(url)
|
||||||
|
@ -87,4 +93,9 @@ function SongQueue.getLength()
|
||||||
return song_queue:length()
|
return song_queue:length()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function file_exists(file)
|
||||||
|
local f=io.open(file,"r")
|
||||||
|
if f~=nil then io.close(f) return true else return false end
|
||||||
|
end
|
||||||
|
|
||||||
return SongQueue
|
return SongQueue
|
||||||
|
|
Reference in a new issue