July 23, 20196 yr 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 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. Then you want to right-click the text-channel you want the messages to appear in, and click "Edit Channel". Now you should be in the channel settings, and you should be able to see a button called "Webhooks". Click that Then you get to the webhook settings, where you need to create a new webhook (Or edit an already existing one) After clicking create (or edit), a UI should open. Here you can see your webhook url. Edited October 3, 20196 yr by Medusa
July 23, 20196 yr Nice little tutorial, if you want even more customization I'd check out the "Hookscord" library on github.
July 23, 20196 yr Thank you for this! Amazing, will definetely be using it for some scripts in the future =]
November 22, 20196 yr 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
November 22, 20196 yr Author 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 Made a gif showing how to get that working. Edited November 22, 20196 yr by Medusa
June 2, 20205 yr Author 5 hours ago, Fishbot said: Medusa you Always be my #1 Any example ideas this can be used for? Can be used for a lot of things tbh. Level gained notifications, Scouts, Out of supplies notifications etc.
June 2, 20205 yr 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 June 2, 20205 yr by Bnywarrior
June 2, 20205 yr Author 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.
June 2, 20205 yr 4 hours ago, Medusa said: Can be used for a lot of things tbh. Level gained notifications, Scouts, Out of supplies notifications etc. Is it possible/you know how to send things In reverse? Like sending commands in discord on that webhook !stop !start !attackMedusa Like that kind of stuff? ^^
August 4, 20205 yr Thank you, Now I can get progress updates on my custom scripts more easily :). facebook get photo by id
Create an account or sign in to comment