Jump to content

[Tutorial] Make your script send discord messages


Medusa

Recommended Posts

unknown.png 

I'll be teaching you how to send messages to your discord channel from your botting script.

This can be useful for a number of things; Level messages, PvE drops, Out of food and a lot of other things.

 

I'll be using this json library, but you can re-write the code snippet so you won't need the library (If you know how).

If you want to know how you can "import" the library to your compiled script, just watch this spoiler.

Spoiler

TZEoT8rZLQ.gif

Official Discord webhook API is here

Let's get started :) 


The following code is the function to send a discord message. You want to add this anywhere in your code.

It has come to my attention that I'm a retard, and that I initialize variables inside methods. Please fix this yourself :))

Spoiler

 


public static void discordMessage(String url, String content, String username) throws IOException {
		URL webhookUrl = new URL(url);
	        HttpURLConnection con = (HttpURLConnection)webhookUrl.openConnection();
	     
  		//Set the request headers.
	        con.setDoOutput(true);
	        con.setRequestMethod("POST");
	        con.addRequestProperty("Content-Type", "application/json");
	        con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");

  		//Create the needed parameters as a json object.
	        JSONObject json = new JSONObject();
	        json.put("content", content);
	        json.put("username", username);
  
  		//Add to the body of the request (The content needed gets added to the request)
	        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
	        writer.write(json.toString());
	        writer.close();
  
  		//Create a new StringBuilder to store the request response (Not needed if you don't check if the request was successful
	  	StringBuilder sb = new StringBuilder();
  
  		//Actually send the request to the discord webhook
	  	Scanner scanner = new Scanner(con.getInputStream());
	        while(scanner.hasNextLine()) {
	            sb.append(scanner.nextLine());
	        }
	        scanner.close();
}

The response code for invalid webhook is: 


{
    "code": 50027,
    "message": "Invalid Webhook Token"
}

Empty message response code:


{
    "code": 50006,
    "message": "Cannot send an empty message"
}

 

Once you've got the function in your project you're ready to use it.

discordMessage("Your webhook url", "the message you want to send", "Any username really. You can use the name of the account logged in if you want");

 

If you don't know how to create a discord webhook, look at this spoiler.

Spoiler

 

First your want to find the channel you want the messages to appear in. You need to have access to webhooks in order to do this.

step_one.png 

Then you want to right-click the text-channel you want the messages to appear in, and click "Edit Channel".

step_2.png

Now you should be in the channel settings, and you should be able to see a button called "Webhooks". Click that

step_three.png

Then you get to the webhook settings, where you need to create a new webhook (Or edit an already existing one)

step_four.png

After clicking create (or edit), a UI should open. Here you can see your webhook url.

unknown.png 

 

 

 

 

Edited by Medusa
  • Like 10
  • Boge 1
  • Heart 1
Link to comment
Share on other sites

  • 1 month later...
5 hours ago, Gpstacker57 said:

tips on how to fix the code to get it working?  would love to be able to use this but I'm getting a:

java.lang.NoClassDefFoundError: org/json/JSONObject

 

Much love <3

Made a gif showing how to get that working.

TZEoT8rZLQ.gif 

Edited by Medusa
Link to comment
Share on other sites

  • 6 months later...

Do you reckon you can extend this bot to respond to any1 taking to you? ie; it sends you discord msg and you can reply. For example:

bot: “name”:”wc lvl?”

you: !reply “60, wbu?”

Actually, using a discord bot to configure script behavior would be pretty neat,

!stop !break 20 minutes !status 

This is some cool stuff, Medusa

Edited by Bnywarrior
Link to comment
Share on other sites

1 hour ago, Bnywarrior said:

Do you reckon you can extend this bot to respond to any1 taking to you? ie; it sends you discord msg and you can reply. For example:

bot: “name”:”wc lvl?”

you: !reply “60, wbu?”

Actually, using a discord bot to configure script behavior would be pretty neat,

!stop !break 20 minutes !status 

This is some cool stuff, Medusa

Don't think there is a way to implement something like that without writing an actual discord bot.

Link to comment
Share on other sites

  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...