• Print

Author Topic: Custom !votesong error  (Read 8335 times)

0 Members and 1 Guest are viewing this topic.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Custom !votesong error
« Reply #15 on: October 30, 2015, 05:39:28 pm »
I know this is dragging on, but that would make the vote say the song title?
You can use this function to grab information about a YouTube video:
Code: Lua
  1. local function videoDetails(videoID, callback)
  2.     local url = {}
  3.     url[1] = "https://www.youtube.com/oembed"
  4.     url[2] = "?format=json"
  5.     url[3] = "&url=http://www.youtube.com/watch?v="
  6.     url[4] = videoID
  7.     url = table.concat(url)
  8.  
  9.     http.Fetch(url, function(body, length, headers, code)
  10.         callback(util.JSONToTable(body))
  11.     end)
  12. end
  13.  
  14. -- Example usage:
  15. videoDetails("Y6ljFaKRTrI", function(data)
  16.     print(data.title) -- Prints video title
  17. end)
« Last Edit: October 30, 2015, 05:55:05 pm by Timmy »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Custom !votesong error
« Reply #16 on: October 30, 2015, 07:26:14 pm »
Oh, okay then. I'll pop that in the code somewhere? (In-between maybe?)

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Custom !votesong error
« Reply #17 on: October 31, 2015, 05:18:11 am »
Since I just remade it from typing it off my iPhone, its not perfect, but I think I got it. The one question is that I used data.title to use the song name in the !votesong. I was also unsure about the example, but I think thats just a help reference like if you type !SOMETHING. (?)

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.votesong( calling_ply, string )
  4.         v = v + 1
  5.         PrintMessage( HUD_PRINTTALK, "A vote has been started to play the song," .. data.title .. ". Type !votesong" .. string .. ", to vote!" )
  6.         if v == 3 then
  7.         game.ConsoleCommand( "ulx youtube " .. string .. "" )
  8.         ulx.fancyLogAdmin( "Console has passed the vote to play the song " .. data.title .. "" )
  9.         v = 0
  10.         if ply:IsUserGroup( "superadmin" ) then
  11.                 hook.Add( "VetoSong", "VetoMusic", function( calling_ply, text )
  12.                         text = string.lower( text )
  13.                         if ( text == "!vetosong" ) then
  14.                                 v = 0
  15.                                 return ""
  16.                         end
  17.                 end
  18.                 end )  
  19.         end
  20. end
  21.  
  22. local function videoDetails( VideoID, callback )
  23.         local URL - ()
  24.         url[ 1 ] =
  25.         "https://youtube.com/oembed"
  26.         url[ 2 ] =
  27.         "?format=json"
  28.         url[ 3 ] =
  29.         "&url=http://youtube.com/watch?v="
  30.         url[ 4 ] =
  31.         url = table.concat( url )
  32.        
  33.         http:Fetch( url, function (body, length, headers, code)
  34.                 callback(util.JSONToTable(body))
  35.                 end )
  36.         end
  37.         videoDetails("Y61jFaKRTrI", function(data)
  38.         print(data.title)
  39. end)
  40.  
  41. if SERVER then
  42.     util.AddNetworkString("PlayYouTubeAudio")
  43. end
  44.  
  45. local function APIRequest(videoID, callback)
  46.     local url = {}
  47.     url[1] = "https://youtubeinmp3.com/fetch/?format=JSON&video="
  48.     url[2] = "https://www.youtube.com/watch?v="
  49.     url[3] = videoID
  50.     url = table.concat(url)
  51.  
  52.     http.Fetch(url, function(body, len, headers, code)
  53.         if code ~= 200 then
  54.             return callback(false, "API request failed")
  55.         end
  56.  
  57.         local json = util.JSONToTable(body)
  58.  
  59.         if not json then
  60.             local shortenerUrl = "http://tny.im/yourls-api.php"
  61.             local shortenerParams = {
  62.                 action = "shorturl",
  63.                 url = url,
  64.                 format = "json"
  65.             }
  66.             http.Post(shortenerUrl, shortenerParams, function(body, len, headers, code)
  67.                 return callback(false, "Go to " .. util.JSONToTable(body).shorturl .. ", click \"Convert & Download MP3\" then try requesting this song again.")
  68.             end)
  69.         elseif json.error then
  70.             return callback(false, json.error)
  71.         else
  72.             return callback(true, json)
  73.         end
  74.     end)
  75. end
  76.  
  77. if CLIENT then
  78.     local station
  79.     local function play(url)
  80.         if IsValid(station) then station:Stop() end
  81.         sound.PlayURL(url, "", function(_station)
  82.             station = _station
  83.             if IsValid(station) then
  84.                 station:Play()
  85.             end
  86.         end)
  87.     end
  88.  
  89.     net.Receive("PlayYouTubeAudio", function()
  90.         APIRequest(net.ReadString(), function(ok, data)
  91.             if not ok then
  92.                 print(data)
  93.                 return false
  94.             end
  95.             play(data.link)
  96.         end)
  97.     end)
  98. end
  99.  
  100. function ulx.youtube(calling_ply, videoID)
  101.     if string.len(videoID) ~= 11 then
  102.         ULib.tsayError(calling_ply, "Invalid video ID. Example: !youtube Y6ljFaKRTrI")
  103.         return false
  104.     end
  105.  
  106.     APIRequest(videoID, function(ok, data)
  107.         if not ok then
  108.             ULib.tsayError(calling_ply, data)
  109.             return false
  110.         end
  111.  
  112.         net.Start("PlayYouTubeAudio")
  113.         net.WriteString(videoID)
  114.         net.Broadcast()
  115.  
  116.         ulx.fancyLogAdmin(calling_ply, "#A played song: #s", data.title)
  117.     end)
  118. end
  119. local youtube = ulx.command(CATEGORY_NAME, "ulx youtube", ulx.youtube, "!youtube")
  120. youtube:addParam{type=ULib.cmds.StringArg, hint="YouTube video ID", ULib.cmds.takeRestOfLine}
  121. youtube:defaultAccess(ULib.ACCESS_ADMIN)
  122. youtube:help("Plays audio from a YouTube video.")

EDIT: I just proof read, and saw that I did date.title .-.
« Last Edit: October 31, 2015, 05:19:43 am by WispySkies »

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Custom !votesong error
« Reply #18 on: October 31, 2015, 11:40:19 am »
In your code, you're expecting magical things developers can only dream of. (:

There's a lot of mistakes in your code. Are you trying to write all of this in one go?

Why not start out with a small piece of working code? Then make small incremental changes, and, test your code after every significant change. Now, if there's an error, you will know exactly where you have to look. Makes debugging so much easier!

Start with the bare minimum for a ULX command to work properly. This one simply prints "Hello world" in the server console when you call it - a great starting point.
Code: Lua
  1. function ulx.helloworld( calling_ply )
  2.     print( "Hello world" )
  3. end
  4. local helloworld = ulx.command( "My commands", "ulx helloworld", ulx.helloworld, "!helloworld" )
  5. helloworld:defaultAccess( ULib.ACCESS_ALL )
  6. helloworld:help( "Prints 'Hello world' in the server console." )

Test your command. Works as expected? No errors? Continue ->

Now modify the command to take 1 argument, it will be a string, the YT videoID. Try to print that argument instead of "Hello world":
Code: Lua
  1. function ulx.helloworld( calling_ply, video_id )
  2.     print( video_id )
  3. end
  4. local helloworld = ulx.command( "My commands", "ulx helloworld", ulx.helloworld, "!helloworld" )
  5. helloworld:addParam{ type=ULib.cmds.StringArg, hint="Video ID" }
  6. helloworld:defaultAccess( ULib.ACCESS_ALL )
  7. helloworld:help( "Prints first command argument in the server console." )

Test your command. Do you get the expected output? No errors? Continue ->

And so on.
« Last Edit: October 31, 2015, 12:18:59 pm by Timmy »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Custom !votesong error
« Reply #19 on: October 31, 2015, 06:37:06 pm »
In your code, you're expecting magical things developers can only dream of. (:
Then it shall stay that way! I dont think its that necessary, just a pain to keep talking about and having to change. Thanks anyway, and I dont mean to waste your time!

  • Print