Jump to content

[Tutorial] Make your script send discord messages


Medusa

Recommended Posts

Started a new bot script and tried implementing this again ( tried to even re-write my firemaker script by copying project files and re-writing the code apart from the discord part ) if I use normal firemaker jar i didnt edit it sends the messages but if I don't and create a new project it gives me this error Error in bot executor or from Error class (and not Exception)!
java.lang.NoClassDefFoundError: org/json/JSONObject
    at core.Main.discordMessage(Main.java:30)
    at core.Main.onLoop(Main.java:58)
    at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(jf:86)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 4 more

 

You use to have a gif that would show you how to fix this error but it doesn't work for me anymore. 

Sorry for posting on such a old thread but wanted to implement it back into some of my scripts again.

Edited by Jueix
Link to comment
Share on other sites

  • 4 weeks later...
On 2/21/2023 at 2:28 PM, Jueix said:

Started a new bot script and tried implementing this again ( tried to even re-write my firemaker script by copying project files and re-writing the code apart from the discord part ) if I use normal firemaker jar i didnt edit it sends the messages but if I don't and create a new project it gives me this error Error in bot executor or from Error class (and not Exception)!
java.lang.NoClassDefFoundError: org/json/JSONObject
    at core.Main.discordMessage(Main.java:30)
    at core.Main.onLoop(Main.java:58)
    at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(jf:86)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 4 more

 

You use to have a gif that would show you how to fix this error but it doesn't work for me anymore. 

Sorry for posting on such a old thread but wanted to implement it back into some of my scripts again.

If anyone is having the same thing here's an updated script that works with my newest script that doesn't use the JSON libary.

 

private static final String 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";

    /*send discord messages */
    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", USER_AGENT);

        //Create the JSON string to send in the request body.
        String json = "{ \"content\": \"" + content + "\", \"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);
        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();
    }

 

Link to comment
Share on other sites

5 hours ago, Jueix said:

If anyone is having the same thing here's an updated script that works with my newest script that doesn't use the JSON libary.

 

private static final String 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";

    /*send discord messages */
    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", USER_AGENT);

        //Create the JSON string to send in the request body.
        String json = "{ \"content\": \"" + content + "\", \"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);
        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();
    }

 

if u every wanna work with json, osbot has Gson lib packaged with it

Link to comment
Share on other sites

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...