Discord Spotify Now Playing Self-Bot

Source: https://maxcdn.icons8.com/Android_L/PNG/512/Logos/discord_logo-512.png + http://orig03.deviantart.net/8d01/f/2017/030/9/d/spotify_by_nrddnshn-dax9nry.png

One day a couple of weeks ago I thought to myself…
Discord has a ‘Now Playing’ status message for games, why not co-opt it to show what music you’re playing as well?.
So I did!

The first step was to see if anyone else had done it…
So I Googled DDG‘d around for things like “Discord Now Playing Music plugin” and things like that. This was all before I discovered the phrase ‘self-bot’ (which I’ll get to in a minute), so needless to say, nothing use-able came up.
What I did find was posts requesting Discord integrate all music players like that, and replies saying ‘They can’t possible integrate with EVERY music player’, which led me to believe I’d probably have more luck if I narrowed my search a little from ‘music’ to one particular player.

Since I’d just recently bitten the bullet and installed Spotify (my net is notoriously unreliable, but damn it’s a great service when it’s allowed to be), I decided to try to limit my search to something like “Discord Spotify Now Playing plugin”.

That led me right (4th result) here: https://github.com/ZeroIndice/discord-spotify-nowplaying.
Of course, I didn’t particularly want a whole separate bot called “MusicIAmPlaying”, with a status message set appropriately. I wanted it to be MY status message.
I did eventually realise “Oh, it wants to login as my account… So even though it’s not a modification to my Discord client, it’s still going to transparently show up as me”. I also more or less simultaneously discovered the phrase ‘Self-Bot’, which describes discord bots that authenticate as an existing user, rather than a dedicated Bot user, and act on their behalf. So it seemed more and more like this was exactly what I wanted.

Okay, great! I just follow these instructions, install Node and the dependencies and run this code. Easy-peasy, right?

 

No.

Source: http://colbertpics.com/main.php?g2_view=core.DownloadItem&g2_itemId=480&g2_serialNumber=1

I’m sorry to say, this code did not work, nor did I ever get this code working.
I did however, hack at it to try to get it working myself.

The first error I ran into was happily enough:
A) Already reported, AND
B) Already fixed!

Badabing, badaboom, I manually implement those changes aaaaaaaaand?

 

No.

I’ve forgotten the exact errors I ran into at this point, but they weren’t all that tough to sort out once I saw them. Eventually I got it to the stage where the spotify integration was working just fine, and was spitting out the correct songs (at pre-set intervals) to the console. It just wasn’t updating the now playing message properly, and it didn’t clear the last playing song when playback was paused/stopped.

Incidentally, if you want to try this sort of thing yourself, the selfbot can change the now playing message for everyone EXCEPT you. If you’re logged in to the same account, you don’t see it. So I had to join one of my servers with a throwaway account just to test it.

Nothing I did could get Discord to update the message properly/reliably/successfully. In the end, I figured it’d probably be easier to just switch to a different API wrapper, rather than keep trying in vain to get this one working.

Out goes discord.io
In comes discord.js

Of course, discord.js doesn’t work the same way discord.io works, so I had to do even more fiddling with the code…
That’s fine, as long as it works when I’m done.

So now that I’d totally re-written the Discord integration component (50%) of the bot that I found, did it work?

 

Yes…-ish!

http://images2.fanpop.com/image/photos/11600000/Almost-Can-t-reach-ice-age-scrat-and-scratte-11617330-400-256.jpg

It was talking to the Discord API properly now, yes… But not instantly. The Spotify webhelper was just polling getStatus() every <setInterval> and updating nowPlaying at that point if necessary. Which meant it could take as long as <setInterval>-1 amount of time for the message to change… That’s not the biggest deal in the world, but I didn’t want people saying “hey cool song”, and me thinking it’s the one that’s playing now, when it’s really the one it still says I’m playing from before. Plus it’s terribly inefficient to poll getStatus() instead of responding to events, and I’d found this neat re-write of the spotify webhelper I was using, that used events instead of getStatus().

Okay! Let’s totally re-write the other 50% of the original code!

Really, I could’ve just written it from scratch at this stage, but I’ll never begrudge a learning experience (at least, not once I’ve calmed down)!

So I do eventually get it working with the new event driven webhelper (spotify-web-helper if you’re curious) and it works a treat!

 

But…
Source: https://thumbs.dreamstime.com/t/exasperated-frustrated-woman-leaning-her-laptop-46387585.jpg

I have to run it manually, and it sits in this command prompt window all the time.
I like my UI minimalist! Everything that can be minimised to the system tray is, and if it can run silently in the background? So much the better!

Off on another Google Trek then, this time to figure out how to make it run in the background.

On Unix based systems, it’d be as easy as telling the system to run a bash file on login that executes the script file via Node, and have an & at the end to stick it in the background.
On Windows? Not so much.

Cue hours of me trawling the Internet looking at various angles of attack to save me from having a command prompt window open all the time that I couldn’t even use.

I looked for a way to execute a batch script silently, I looked for a way to USE a batch script to execute a batch script silently, I even looked for a to run Node silently. In the end I wound up with a .vbs script calling a batch script calling the bot script. Admittedly I probably could have called the bot script directly from the .vbs (which was necessary to hide the execution window because batch cannot), but I couldn’t be bothered figuring out the .vbs I found on the net closely enough to modify it to that degree. Especially since it’s a ‘run once on startup’ type of thing.

Great! Now I can run it silently, I just need to get Windows to execute it at startup. Easy right?

 

No.
http://commitnesstofitness.com/wp-content/uploads/2014/08/exasperated.gifhttp://reactiongif.org/wp-content/uploads/GIF/2014/08/GIF-Are-you-fucking-kidding-me-FFS-For-Fucks-Sake-Ugh-OMG-Oh-my-god-My-God-despair-are-you-kidding-me-dont-know-dont-care-Albus-Dumbledore-Harry-Potter-GIF.gifhttp://3.bp.blogspot.com/-_59PeFpw0HQ/VpLWMGfmvJI/AAAAAAAAGfw/WTt0SK36RWA/s1600/tumblr_mf8cor9BgE1rsm741o1_500.gif

More of the problems than I care to admit that happened at this point, were because I didn’t know how to make Windows execute the script within its own directory, but plenty were not.

I tried adding the script as a login script for my user, which didn’t run properly regardless of whether it was the .vbs or the .bat I whipped up to call the .vbs (which itself calls a .bat…).
I even tried adding it to the task scheduler, but that kept failing silently.

After eventually discovering that it kept failing because it was trying to execute the script from a random system directory, because I hadn’t checked the optional ‘start in’ box, which is 100% my bad (but hey, at least now I’ve finally realised ‘start in’ means ‘execute relative to’ and not ‘start looking in’ which I had always felt was incredibly redundant), I finally reached the end of my journey.

The code executes when I login, I don’t have an annoying command prompt loitering around, and the code itself is responsive and reliable.
If you want to use it for yourself, you can check out the Discord Spotify Integration page I made for it!

 

Yes!
http://s2.quickmeme.com/img/5c/5c7dcf024101b083008e90529f42c1e32be6a97d47fc4c0c8f449466b9bc8613.jpg