[gRIP] Structure of Discord Webhook

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1090
Gender:
United States of America

[gRIP] Structure of Discord Webhook

Post by Archive-Poster »

Click for a readme

Click for a guide

Code: Select all

#include <amxmodx>
#include <grip>

new const url[] = "https://webhook.site/"

new GripJSONValue:object
new GripJSONValue:embeds, GripJSONValue:embeds_array
new GripJSONValue:author
new GripJSONValue:thumbnail
new GripJSONValue:image
new GripJSONValue:footer
new GripJSONValue:fields1, GripJSONValue:fields_array
new GripJSONValue:fields2
new GripJSONValue:fields3
new GripJSONValue:fields4

new GripRequestOptions:options
new GripBody:body

public plugin_init(){
    object       = grip_json_init_object()
    embeds       = grip_json_init_object()
    embeds_array = grip_json_init_array()
    author       = grip_json_init_object()
    thumbnail    = grip_json_init_object()
    image        = grip_json_init_object()
    footer       = grip_json_init_object()
    fields_array = grip_json_init_array()
    fields1      = grip_json_init_object()
    fields2      = grip_json_init_object()
    fields3      = grip_json_init_object()
    fields4      = grip_json_init_object()

    options = grip_create_default_options()
    grip_options_add_header(options, "Content-Type", "application/json")
    grip_options_add_header(options, "User-Agent", "big")
}

public plugin_end(){
    grip_destroy_json_value(object)
    grip_destroy_json_value(embeds)
    grip_destroy_json_value(embeds_array)
    grip_destroy_json_value(author)
    grip_destroy_json_value(thumbnail)
    grip_destroy_json_value(image)
    grip_destroy_json_value(footer)
    grip_destroy_json_value(fields_array)
    grip_destroy_json_value(fields1)
    grip_destroy_json_value(fields2)
    grip_destroy_json_value(fields3)
    grip_destroy_json_value(fields4)
}

public OnConfigsExecuted(){
    grip_json_object_set_string(object, "username", "Webhook")
    grip_json_object_set_string(object, "avatar_url", "https://picsum.photos/100")
    grip_json_object_set_string(object, "content", "Text message. Up to 2000 characters.")

    grip_json_object_set_value(object, "embeds", embeds_array)
        grip_json_object_set_value(embeds, "author", author)
            grip_json_object_set_string(author, "name", "Birdie")
            grip_json_object_set_string(author, "url", "https://www.reddit.com/r/cats/")
            grip_json_object_set_string(author, "icon_url", "https://i.imgur.com/R66g1Pe.jpg")
        

        grip_json_object_set_string(embeds, "title", "Title")
        grip_json_object_set_string(embeds, "url", "https://google.com/")
        grip_json_object_set_string(embeds, "description", "Text message. You can use Markdown here. *Italic* **bold** __underline__ ~~strikeout~~ [hyperlink](<a href="https://google.com" target="_blank" rel="nofollow noopener">https://google.com</a>) `code`")
        grip_json_object_set_number(embeds, "color", 15258703)

        grip_json_object_set_value(embeds, "thumbnail", thumbnail)
            grip_json_object_set_string(thumbnail, "url", "https://picsum.photos/200")

        grip_json_object_set_value(embeds, "image", image)
            grip_json_object_set_string(image, "url", "https://picsum.photos/800")

        grip_json_object_set_value(embeds, "footer", footer)
            grip_json_object_set_string(footer, "text", "Woah! So cool! :smirk:")
            grip_json_object_set_string(footer, "icon_url", "https://i.imgur.com/fKL31aD.jpg")
        
        grip_json_object_set_value(embeds, "fields", fields_array)
            grip_json_array_append_value(fields_array, fields1)
                grip_json_object_set_string(fields1, "name", "Text")
                grip_json_object_set_string(fields1, "value", "More text")
                grip_json_object_set_bool(fields1, "inline", true)

            grip_json_array_append_value(fields_array, fields2)
                grip_json_object_set_string(fields2, "name", "Even more text")
                grip_json_object_set_string(fields2, "value", "Yup")
                grip_json_object_set_bool(fields2, "inline", true)

            grip_json_array_append_value(fields_array, fields3)
                grip_json_object_set_string(fields3, "name", "Use `^"inline^": true` parameter, if you want to display fields in the same line.")
                grip_json_object_set_string(fields3, "value", "okay...")

            grip_json_array_append_value(fields_array, fields4)
                grip_json_object_set_string(fields4, "name", "Thanks!")
                grip_json_object_set_string(fields4, "value", "You're welcome :wink:")

    grip_json_array_append_value(embeds_array, embeds)

    body = grip_body_from_json(object, true)
    grip_request(url, body, GripRequestTypePost, "Grip_Return", options)

    grip_json_object_clear(object)
    grip_json_object_clear(embeds)
    grip_json_array_clear(embeds_array)
    grip_json_object_clear(author)
    grip_json_object_clear(thumbnail)
    grip_json_object_clear(image)
    grip_json_object_clear(footer)
    grip_json_object_clear(fields1)
    grip_json_object_clear(fields2)
    grip_json_object_clear(fields3)
    grip_json_object_clear(fields4)
    grip_destroy_body(body)
}

public Grip_Return(){
    new GripResponseState:responseState = grip_get_response_state()
    if (responseState != GripResponseStateSuccessful){
        log_amx("ERROR: gRIP Response State Failed: Result (%d)", responseState)
    }

    else {
        new GripHTTPStatus:responseStatus = grip_get_response_status_code()
        if (responseStatus != GripHTTPStatusOk){
            log_amx("ERROR: gRIP Response Status Failed: Result (%d)", responseStatus)
        }

        else {
            new data[256]
            grip_get_response_body_string(data, charsmax(data))
            if (data[0])
                server_print("Response:^n%s", data)
        }
    }
}

Who is online

Users browsing this forum: No registered users and 0 guests