curdis

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Curdis v0.1.0

This is a library wrapper around discord API using http and websocket

Changelog

  • moved all files from bot folder to the main

Example

bot, err := curdis.Auth(TOKEN)
if err != nil {
    panic(err)
}

clearCommand := curdis.Command{
    Name:        "clear",
    Description: "Deletes all messages in channel",
    Options: []curdis.Argument{
    {
        Name:        "channel",
        Description: "Channel to clear (if not specified clearing whole server)",
        Required:    false,
        Type:        curdis.ARG_CHANNEL,
    },
    },
}
err = bot.AddGlobalCommand(&clearCommand)
if err != nil {
    panic(err)
}
bot.AddCommandHandler(clearCommand, clearHandler)

defer func() {
    // This funcion clears all commands when bot is stopped
    commands, err := bot.GetGlobalCommands()
    if err != nil {
        panic(err)
    }

    for _, command := range commands {
        bot.DeleteGlobalCommand(&command)
    }
}()
go panic(bot.HandleEvents())
for {
}

func clearHandler(bot *curdis.Bot, user *curdis.User, guildId string, args []curdis.WSArgument) string {
	if len(args) > 0 {
		channelId := (*args[0].Value).(string)

		channel := curdis.Channel{
			Id: channelId,
		}
		err := clearChannel(bot, &channel)
		if err != nil {
			return "Error clearing channel 😡🌋: " + err.Error()
		}
		return "🫰Cleared channel✨"
	} else {
		var notcleared []curdis.Channel

		channels, err := bot.GetChannels(guildId)
		if err != nil {
			return "Got error: " + err.Error()
		}

		for _, channel := range channels {
			err := clearChannel(bot, &channel)
			if err != nil {
				notcleared = append(notcleared, channel)
			}
		}

		message := "🫰Cleared all channels "

		if len(notcleared) > 0 {
			message = fmt.Sprintf("%s instead of [ ", message)

			for _, channel := range notcleared {
				message = fmt.Sprintf("%s\"%s\" ", message, channel.Name)
			}

			message = fmt.Sprintf("%s]", message)
		}

		message = fmt.Sprintf("%s✨", message)

		return message
	}
}

func clearChannel(bot *curdis.Bot, channel *curdis.Channel) error {
	messages, err := bot.GetChannelMessages(channel)
	if err != nil {
		return err
	}

	for _, message := range messages {
		err = bot.DeleteMessage(&message)
		if err != nil {
			return err
		}
	}

	return nil
}

Documentation

Index

Constants

View Source
const API string = "https://discord.com/api/v9"
View Source
const ARG_BOOLEAN uint8 = 5
View Source
const ARG_CHANNEL uint8 = 7
View Source
const ARG_INTEGER uint8 = 4
View Source
const ARG_STRING uint8 = 3
View Source
const ARG_SUBCOMMAND uint8 = 1
View Source
const ARG_SUBCOMMAND_GROUP uint8 = 2
View Source
const ARG_USER uint8 = 6
View Source
const CHANNEL_TEXT uint8 = 0
View Source
const CHANNEL_TEXT_CATEGORY uint8 = 4
View Source
const CHANNEL_VOICE uint8 = 2
View Source
const CHAT_INPUT uint8 = 1
View Source
const WS_API string = "wss://gateway.discord.gg"
View Source
const WS_DISPATCH uint8 = 0
View Source
const WS_HEARTBEAT uint8 = 1
View Source
const WS_INDENTIFY uint8 = 2
View Source
const WS_STATUS_UPDATE uint8 = 3
View Source
const WS_VOICE_STATE_UPDATE uint8 = 4

Variables

This section is empty.

Functions

This section is empty.

Types

type Argument

type Argument struct {
	Type        uint8  `json:"type"`
	Name        string `json:"name"`
	Description string `json:"description"`
	Required    bool   `json:"required"`
}

type Bot

type Bot struct {
	Token string
	AppId string
	// contains filtered or unexported fields
}

func Auth

func Auth(token string) (Bot, error)

func (*Bot) AddCommandHandler

func (bot *Bot) AddCommandHandler(command Command, handler CommandHandler)

func (*Bot) AddGlobalCommand

func (bot *Bot) AddGlobalCommand(command *Command) error

func (*Bot) DeleteGlobalCommand

func (bot *Bot) DeleteGlobalCommand(command *Command) error

func (*Bot) DeleteMessage

func (bot *Bot) DeleteMessage(message *Message) error

func (*Bot) GetChannelMessages

func (bot *Bot) GetChannelMessages(channel *Channel) ([]Message, error)

func (*Bot) GetChannels

func (bot *Bot) GetChannels(serverId string) ([]Channel, error)

func (*Bot) GetGlobalCommands

func (bot *Bot) GetGlobalCommands() ([]Command, error)

func (*Bot) HandleEvents

func (bot *Bot) HandleEvents() error

func (*Bot) SendMessage

func (bot *Bot) SendMessage(channel *Channel, content string) error

func (*Bot) SetMessageDeleteHanler

func (bot *Bot) SetMessageDeleteHanler(handler MessageDeleteHandler)

func (*Bot) SetMessageHandler

func (bot *Bot) SetMessageHandler(handler MessageHandler)

type Channel

type Channel struct {
	Id   string `json:"id"`
	Name string `json:"name"`
	Type uint8  `json:"type"`
}

type Command

type Command struct {
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Type        uint8      `json:"type"`
	Id          string     `json:"id"`
	Options     []Argument `json:"options"`
}

type CommandHandler

type CommandHandler = func(*Bot, *User, string, []WSArgument) string

type Member

type Member struct {
	User User `json:"user"`
}

type Message

type Message struct {
	Content   string `json:"content"`
	ChannelId string `json:"channel_id"`
	Author    User   `json:"author"`
	Pinned    bool   `json:"pinned"`
	Id        string `json:"id"`
}

type MessageDeleteHandler

type MessageDeleteHandler = func(*Bot, *Channel)

type MessageHandler

type MessageHandler = func(*Bot, *Message)

type User

type User struct {
	Id       string `json:"id"`
	Username string `json:"username"`
}

type WSArgument

type WSArgument struct {
	Name  string       `json:"name"`
	Value *interface{} `json:"value,omitempty"`
}

type WSInteraction

type WSInteraction struct {
	Token   string `json:"token"`
	Type    int    `json:"type"`
	Id      string `json:"id"`
	GuildId string `json:"guild_id"`
	Data    struct {
		Id      string       `json:"id"`
		Name    string       `json:"name"`
		Options []WSArgument `json:"options"`
	} `json:"data"`
	Member Member `json:"member"`
}

type WSResponse

type WSResponse struct {
	Type uint8 `json:"type"`
	Data struct {
		Content string `json:"content"`
	} `json:"data"`
}

Jump to

Keyboard shortcuts

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