I've gotten bored lately and started making things that make me look really lazy, mostly just to learn, gonna start releasing a few.

Okay, so this one, are you currently in a discord channel that has a bot that queues youtube music?

I made a quick java program + chrome extension to queue music for me, and I thought I'd show you how.

Okay, so the chrome extension.

So our chrome extension is going to be basic af, and will only need the manifest, background script, and icons if you choose, though these are optional.
(If not using icons, remove this value from the manifest)

Your manifest is gonna be a JSON file and will look like this:
Code:
{
  "manifest_version": 2,
  "name": "Discord Music Queuer",
  "version": "0.1.0",

  "description": "Adds context menu to queue youtube vids in discord",
  "icons": {
      "16": "icon16.png",
      "48": "icon48.png"
   },

  "author": "Cody",
  "background": {
    "scripts": [ "background.js" ]
  },
  "permissions": [
    "contextMenus"
  ]
}
Most values are straightforward but,

'background'->'script' is our general background script that handles... things in the background holy fuck.
Most scripts will also have a 'content script' but this is not needed on our extension

permissions:
Because we'll be adding a right-click option, we'll need contextmenu permissions

Now to create our background script.

background.js
Code:
function connect(theUrl, callback) {
	var xmlHttp = new XMLHttpRequest();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
			callback(xmlHttp.responseText);
	}
	xmlHttp.open("GET", theUrl, true); // true for asynchronous
	xmlHttp.send(null);
}

chrome.contextMenus.create({
	title: "Queue to Discord",
	contexts: [
		"all"
	],
	onclick: function (info, tab) {
		if (info['linkUrl'] != '') {
			var link = info['linkUrl'];
			if (!link.includes('youtube'))
				return;
			link = link.substring(link.indexOf('=') + 1)
			connect('http://localhost:4448/play/' + link, function () {
				alert('played');
			});
		}
	},
})
The connect function we will explain later, for now let's just focus on the context menu code

the create function takes a few different parameters, we're going to use 3. Name, contexts, and onclick

Name: straightforward, this is the name that will appear on the right-click menu. Do not include the extension name in this as, if there are more than 1 context menu added, chrome will show the extension name in a dropdown menu anyway.
Contexts: these are the element types chrome will display the right-click option on, we're going to set this to 'all' just to be sure, and so we can add more options later on, such as simply right clicking a youtube link
onclick: the callback function that will fire when the right click option is clicked.


Our chrome extension is now finished. With 1 exception that we'll talk about later.
Time for our Java application.

For this, we're first going to need a discord wrapper.
I'm going to be using JDA in this example.

Spoiler for jda-maven:

Code:
<repositories>
		<repository>
			<id>jcenter</id>
			<name>jcenter-bintray</name>
			<url>http://jcenter.bintray.com</url>
		</repository>
	</repositories>
	<dependencies>
		<dependency>
			<groupId>net.dv8tion</groupId>
			<artifactId>JDA</artifactId>
			<version>3.5.0_329</version>
		</dependency>
	</dependencies>


Next, you have two options, 1 is to make a discord bot and add it to whatever server you're going to be using this on
Second is to simply use your own account.

If you're using a bot, go here to see how to make a discord bot and get the token id, this is a good guide to use

If you're using your own account, it's a bit more complicated... Go to http://discordapp.com/login and login, you will be redirected to the browser discord app.
From here, open the developer console (F12 or CTRL+SHFT+I) and navigate to the 'Application' tab.
On the left, you will see a few tabs, underneath 'Storage', expand 'Local Storage', and click on the url inside that should say http://discordapp.com/

Now, in the key/value page, look for 'token' and save the value.

Finally, let's make our Java program.
To make a JDA discord program, it's extremely easy.

The following code will start up and log us in:

Code:
jda = new JDABuilder(AccountType.CLIENT).setToken(TOKEN)
									.buildBlocking();
TOKEN is the token we got before, and if you're using a bot account, change account type to AccountType.BOT.

We now have a few options, how do we want to communicate between our chrome extension and our java program.

I have quite a few things running off mine, and turned it into an api of sorts, so I'm using spark for Java.
Now depending on what you use, you'll have to change your chrome extension to reflect it.
Because I'm using spark, my extension has a simple bit of code to request from spark.

Spoiler for spark-maven:

Code:
<dependency>
			<groupId>com.sparkjava</groupId>
			<artifactId>spark-core</artifactId>
			<version>2.5.5</version>
			<exclusions>
				<exclusion>
					<artifactId>jade4j</artifactId>
					<groupId>de.neuland-bfi</groupId>
				</exclusion>
			</exclusions>
		</dependency>


Before we get the connection going, let's get a method to send a message to our discord channel.
You're going to need the ID for the channel you would like to send the message to. (Either the general, or most discord servers have a specific channel that their youtube bot works in)
To get this, we have a few options, personally I simply listen for messages in the bot, and print out the channel the occurred in, then say something in whatever channel I wanna know the ID of.

To do this in JDA, we create a new class to listen for Messages,
MessageListener
Code:
package com.cryo;

import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.SubscribeEvent;

public class MessageListener {
	
	@SubscribeEvent
	public void onMessage(MessageReceivedEvent event) {
		System.out.println(event.getChannel().getIdLong());
	}

}
Similar to Discord4J's event manager, JDA uses an annotated event style to listen for events.
We will register this class as a listener, and JDA will loop through any methods with with the SubscribeEvent annotation, and will then fire the method based on the first parameter.
So in this class, onMessage gets fired because it has the annotation, and MessagedReceivedEvent is it's first parameter.

Once we have the channel id, the sendMessage code is fairly simple:
Code:
public void sendMessage(String message) {
		MessageAction action = jda.getTextChannelById(BOT_CHANNEL).sendMessage(message);
		action.queue();
	}
JDA is asynchronous, but we don't really need to worry about that, so we're simply going to queue the action, and not worry about a response.
(If it fails, we right-click again, we're not making a professional, released to customers program here or anything)

BOT_CHANNEL being the channel id we just found.

We are very close to being done, we simply need to get spark listening for requests now.
The following code will achieve that for us:
Code:
port(4448);
			get("/play/:id", (req, res) -> {
				String id = req.params(":id");
				String link = "http://youtube.com/watch?v="+id;
				sendMessage("=qp "+link);
				return "success";
			});
The port function starts our program on the specified port. Since we're doing this on the same computer, no need to port forward or anything, though if you would like to do this remotely, simply portforward whatever port you specify (4448 in this case), and connect to http://YOUR_IP:4448/play/YOUTUBE_ID and this will work remotely

the get function is how we listen for requests, obviously just get requests.
We're gonna listen for any requests on /play/, and whatever is specified after is gonna be our id, that's the :id (not sure how to explain it)
We can retrieve this by doing request.params(":id")
This is different than queryParams which is the ?name=value

Now all this does is create a youtube link given the id you send, and send a message through discord

Personally I'm using Ayana for this, so the command is "=qp LINK", you'll have to change this if you're using a different bot.