Skip to content

typical-developers/discord-webhooks-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Webhooks

A module to handle sending messages to Discord webhooks in Golang.

Example Usage

Sending a Message

package main

import "github.com/typical-developers/discord-webhooks-go"

func main() {
    webhook := webhooks.NewWebhookClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
    // Alternatively, you can use a webhook URL:
       // webhook := webhooks.NewWebhookClientFromURL("https://discord.com/api/webhooks/YOUR_CLIENT_ID/YOUR_CLIENT_SECRET")

    payload := webhooks.WebhookPayload{}
    payload.SetContent("Hello, world!")

    _, err := webhook.SendMessage(&payload)
    if err != nil {
        println(err.Error())
    }
}

Sending a Message (with embeds)

package main

import "github.com/typical-developers/discord-webhooks-go"

func main() {
    webhook := webhooks.NewWebhookClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
    // Alternatively, you can use a webhook URL:
       // webhook := webhooks.NewWebhookClientFromURL("https://discord.com/api/webhooks/YOUR_CLIENT_ID/YOUR_CLIENT_SECRET")

    payload := webhooks.WebhookPayload{}
    payload.SetContent("Hello, world!")

    embed := payload.AddEmbed()
    embed.SetTitle("Hello, world!")
    embed.SetDescription("This is a test embed.")
    embed.SetTimestamp(time.Now())

    customFieldOne := embed.AddField()
    customFieldOne.SetName("Custom Field 1")
    customFieldOne.SetValue("This is a custom field.")
    customFieldOne.SetInline(true)

    customFieldTwo := embed.AddField()
    customFieldTwo.SetName("Custom Field 2")
    customFieldTwo.SetValue("This is another custom field.")
    customFieldTwo.SetInline(true)

    _, err := webhook.SendMessage(&payload)
    if err != nil {
        println(err.Error())
    }
}

Sending a Message (with attachments)

package main

import "github.com/typical-developers/discord-webhooks-go"

func main() {
    webhook := webhooks.NewWebhookClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
    // Alternatively, you can use a webhook URL:
       // webhook := webhooks.NewWebhookClientFromURL("https://discord.com/api/webhooks/YOUR_CLIENT_ID/YOUR_CLIENT_SECRET")

    payload := webhooks.WebhookPayload{}
    payload.SetContent("Hello, world!")

    file, err := os.Open("./example.txt")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    payload.AddAttachment("example.txt", file)

    _, err := webhook.SendMessage(&payload)
    if err != nil {
        println(err.Error())
    }
}

In-Progress Features

  • Building the client with a URL or the client id and secret.
  • Setting a default user profile for the webhook client (username and avatar).
  • Sending basic messages.
    • Including embeds.
    • Including attachments.
  • Message builders.
  • Editing/Deleting existing webhook messages.
  • Deleting the webhook.

About

A Golang webhook handler for Discord webhooks.

Resources

License

Stars

Watchers

Forks

Languages