dishooks

package module
v0.0.0-...-b4fc7c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2020 License: MIT Imports: 11 Imported by: 1

README

dishooks

GoDoc

dishooks is a simple Discord webhook API wrapper.

Example:

package main 

import "github.com/itsTurnip/dishooks"

func main() {
    webhook, _ = dishooks.WebhookFromURL("https://discordapp.com/api/webhooks/671422873239289884/G0ArWEr3hgJ1I4THBIiwxkIbnGkHTGawikf3Z7be3afsZD-hCH-hNwWxU0rQAe3U7nkX")
    _ = webhook.Get()
    _, _ = webhook.SendMessage(&dishooks.WebhookMessage{
        Content: "Hello world!",
    })
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrParseWebhook = errors.New("Provided URL is not a Discord webhook URL")

ErrParseWebhook represents error from `WebhookFromURL`

Functions

func CheckResponse

func CheckResponse(response *http.Response) (body []byte, err error)

CheckResponse is a help func to check responses from discord API for errors `body` contains response from the server and probably will contain error information if it hasn't been parsed to `*APIError` type.

func FormatTime

func FormatTime(Time time.Time) string

FormatTime is a help func to format time in ISO8601 format.

Types

type APIError

type APIError struct {
	HTTPResponse int    `json:"http_response"`
	Code         int    `json:"code"`
	Message      string `json:"message"`
}

APIError represents discord answer to the request if it went wrong. https://discordapp.com/developers/docs/topics/opcodes-and-status-codes

func (*APIError) Error

func (e *APIError) Error() string

Error implements `error` interface.

type Embed

type Embed struct {
	// Title of embed
	Title string `json:"title"`
	// Type of embed (always "rich" for webhook embeds)
	Type string `json:"type"`
	// Description of embed
	Description string `json:"description"`
	// URL of embed
	URL string `json:"url"`
	// Timestamp of embed content. Should be in ISO8601 format.
	Timestamp string `json:"timestamp"`
	// Color code of the embed
	Color     int             `json:"color"`
	Footer    *EmbedFooter    `json:"footer"`
	Image     *EmbedImage     `json:"image"`
	Thumbnail *EmbedThumbnail `json:"thumbnail"`
	Video     *EmbedVideo     `json:"video"`
	Provider  *EmbedProvider  `json:"provider"`
	Author    *EmbedAuthor    `json:"author"`
	Fields    []*EmbedField   `json:"fields"`
}

Embed represents discord embed. https://discordapp.com/developers/docs/resources/channel#embed-object

type EmbedAuthor

type EmbedAuthor struct {
	// Name of author
	Name string `json:"name"`
	// URL of author
	URL string `json:"url"`
	// URL of author icon (only supports http(s) and attachments)
	IconURL string `json:"icon_url"`
	// Proxied URL of author icon
	ProxyIconURL string `json:"proxy_icon_url"`
}

EmbedAuthor represents author of the embed

type EmbedField

type EmbedField struct {
	// Name of the field. Required.
	Name string `json:"name"`
	// Value of the field. Required.
	Value string `json:"value"`
	// Whether or not this field should display inline.
	Inline bool `json:"inline"`
}

EmbedField represents embed field

type EmbedFooter

type EmbedFooter struct {
	// Footer text. Required.
	Text string `json:"text"`
	// URL of footer icon (only supports http(s) and attachments)
	IconURL string `json:"icon_url"`
	// Proxied URL of footer icon
	ProxyIconURL string `json:"proxy_icon_url"`
}

Embed footer represents embed's footer

type EmbedImage

type EmbedImage struct {
	// URL source of image (only supports http(s) and attachments)
	URL string `json:"url"`
	// A proxied url of the image
	ProxyURL string `json:"proxy_url"`
	// Height of image
	Height int `json:"height"`
	// Width of image
	Width int `json:"width"`
}

EmbedImage represents Image that would be inside of the embed

type EmbedProvider

type EmbedProvider struct {
	// Name of provider
	Name string `json:"name"`
	// URL of provider
	URL string `json:"url"`
}

type EmbedThumbnail

type EmbedThumbnail struct {
	// URL source of thumbnail (only supports http(s) and attachments)
	URL string `json:"url"`
	// A proxied url of the thumbnail
	ProxyURL string `json:"proxy_url"`
	// Height of thumbnail
	Height int `json:"height"`
	// Width of thumbnail
	Width int `json:"width"`
}

type EmbedVideo

type EmbedVideo struct {
	// URL source of video
	URL string `json:"url"`
	// Height of video
	Height int `json:"height"`
	// Width of video
	Width int `json:"width"`
}

EmbedVideo represents Video that would be inside of the embed

type Webhook

type Webhook struct {
	// ID of the webhook
	ID string `json:"id"`
	// Type is WebhookType
	Type      WebhookType `json:"type"`
	GuildID   string      `json:"guild_id"`
	ChannelID string      `json:"channel_id"`
	Name      string      `json:"name"`
	Avatar    string      `json:"avatar"`
	Token     string      `json:"token"`
}

Webhook is a representation of discord webhooks

func WebhookFromURL

func WebhookFromURL(webhookURL string) (webhook *Webhook, err error)

WebhookFromURL parses URL and returns Webhook struct.

func (*Webhook) Delete

func (w *Webhook) Delete() (response []byte, err error)

Delete deletes webhook from discord.

func (*Webhook) Get

func (w *Webhook) Get() error

Get gets information about webhook. Useful to get name, guild and channel IDs etc.

func (*Webhook) Modify

func (w *Webhook) Modify(update *WebhookUpdate) (response []byte, err error)

Modify modifies webhook at the client and discord side.

func (*Webhook) SendFile

func (w *Webhook) SendFile(file []byte, filename string, message *WebhookMessage) (response []byte, err error)

SendFile sends file with message to webhook

func (*Webhook) SendMessage

func (w *Webhook) SendMessage(message *WebhookMessage) (response []byte, err error)

SendMessage sends message to webhook

func (*Webhook) URL

func (w *Webhook) URL() string

URL returns discord url of the webhook

type WebhookMessage

type WebhookMessage struct {
	// Content is a message body up to 2000 characters.
	Content   string   `json:"content"`
	Username  string   `json:"username"`
	AvatarURL string   `json:"avatar_url"`
	TTS       bool     `json:"tts"`
	Embeds    []*Embed `json:"embeds"`
}

WebhookMessage represents a message to send using webhook. When sending a message there must be one of content or embeds.

type WebhookType

type WebhookType int

WebhookType is a webhook type for discord webhooks https://discordapp.com/developers/docs/resources/webhook#webhook-object-webhook-types

const (
	// Incoming type Webhooks can post messages to channels with a generated token
	Incoming WebhookType = iota + 1
	// ChannelFollower type Webhooks are internal webhooks used with Channel Following
	// to post new messages into channels
	ChannelFollower
)

type WebhookUpdate

type WebhookUpdate struct {
	Name string `json:"name"`
	// Avatar is a base64 encoded image. https://discordapp.com/developers/docs/reference#image-data
	Avatar string `json:"avatar"`
}

WebhookUpdate is used to update webhooks using tokens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL