dggchat

package module
v0.0.0-...-915d599 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 11 Imported by: 1

README

GoDoc Go Report Card

dggchat

Destinygg chat go bindings. You can acquire a login key here.

Simple example

package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/MemeLabs/dggchat"
)

func main() {
	// Create a new client
	dgg, err := dggchat.New("loginkey")
	if err != nil {
		log.Fatalln(err)
	}

	// Open a connection
	err = dgg.Open()
	if err != nil {
		log.Fatalln(err)
	}

	// Cleanly close the connection
	defer dgg.Close()

	dgg.AddMessageHandler(onMessage)
	dgg.AddErrorHandler(onError)

	// Wait for ctr-C to shut down
	sc := make(chan os.Signal, 1)
	signal.Notify(sc, syscall.SIGINT)
	<-sc
}

func onMessage(m dggchat.Message, s *dggchat.Session) {
	log.Printf("New message from %s: %s\n", m.Sender.Nick, m.Message)

	if m.Message == "!test" {
		s.SendPrivateMessage(m.Sender.Nick, "testing")
	}
}

func onError(e string, s *dggchat.Session) {
	log.Printf("error %s\n", e)
}

For a more complex example, see FerretBot

Documentation

Overview

Package dggchat provides destiny gg chat binding for Go

Index

Constants

View Source
const (
	FeatureSubscriber    = "subscriber"
	FeatureBot           = "bot"
	FeatureProtected     = "protected"
	FeatureVIP           = "vip"
	FeatureModerator     = "moderator"
	FeatureAdministrator = "admin"
	FeatureTier2         = "flair1"
	FeatureNotable       = "flair2"
	FeatureTier3         = "flair3"
	FeatureTrusted       = "flair4"
	FeatureContributor   = "flair5"
	FeatureCompChallenge = "flair6"
	FeatureEve           = "flair7"
	FeatureTier4         = "flair8"
	FeatureTwitch        = "flair9"
	FeatureSC2           = "flair10"
	FeatureBot2          = "flair11"
	FeatureBroadcaster   = "flair12"
	FeatureTier1         = "flair13"
	FeatureBirthday      = "flair15"
)

Constants for different types of features a user can have

View Source
const (
	ErrorTooManyConnections = "toomanyconnections"
	ErrorProtocol           = "protocolerror"
	ErrorNeedLogin          = "needlogin"
	ErrorNoPermission       = "nopermission"
	ErrorInvalidMessage     = "invalidmsg"
	ErrorMuted              = "muted"
	ErrorSubMode            = "submode"
	ErrorThorttled          = "throttled"
	ErrorDuplicate          = "duplicate"
	ErrorNotFound           = "notfound"
	ErrorNeedBanReason      = "needbanreason"
)

Constants for different types of errors the chat can return

Variables

View Source
var ErrAlreadyOpen = errors.New("web socket is already open")

ErrAlreadyOpen is thrown when attempting to open a web socket connection on a websocket that is already open.

View Source
var ErrReadOnly = errors.New("session is read-only")

ErrReadOnly is thrown when attempting to send messages using a read-only session.

View Source
var ErrTooManyArgs = errors.New("function called with unexcepted amount of arguments")

ErrTooManyArgs is thrown when a funcion is called with an unexpeted number of arguments

Functions

This section is empty.

Types

type Ban

type Ban struct {
	Sender    User
	Timestamp time.Time
	Target    User
	// Online indicates whether the target was online when targeted
	Online bool
}

Ban represents (un)bans issued by chat moderators

type Broadcast

type Broadcast struct {
	Sender    User
	Timestamp time.Time
	Message   string `json:"data"`
	UUID      string `json:"uuid"`
}

Broadcast represents a chat broadcast

type Donation

type Donation struct {
	Sender    User
	Timestamp time.Time
	Message   string
	Amount    int64
	UUID      string
}

Donation represents a dgg donation message

type Message

type Message struct {
	Sender    User
	Timestamp time.Time
	Message   string
}

Message reprents a normal dgg chat message

func (*Message) IsAction

func (m *Message) IsAction() bool

IsAction returns true if the message was an action (/me)

type Mute

type Mute struct {
	Sender    User
	Timestamp time.Time
	Target    User
	// Online indicates whether the target was online when targeted
	Online bool
}

Mute represents (un)mutes issued by chat moderators

type Names

type Names struct {
	Connections int    `json:"connectioncount"`
	Users       []User `json:"users"`
}

Names reprents the initial status message containing user information

type Pin

type Pin struct {
	Sender    User
	Timestamp time.Time
	Message   string
	UUID      string
}

Pin represents a pinned dgg message, AKA message of the day (MOTD)

type Ping

type Ping struct {
	Timestamp int64 `json:"timestamp"`
}

Ping represents a pong response from the server

type PrivateMessage

type PrivateMessage struct {
	User      User
	Message   string
	Timestamp time.Time
	ID        int
}

PrivateMessage represents a received private message from a user

type RoomAction

type RoomAction struct {
	User      User
	Timestamp time.Time
}

RoomAction represents a user joining or quitting the chat

type Session

type Session struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

A Session represents a connection to destinygg chat.

func New

func New(args ...string) (*Session, error)

New creates a new destinygg session. Accepts either 0 or 1 arguments. If no login key is provided, a read-only session is returned

func (*Session) AddBanHandler

func (s *Session) AddBanHandler(fn func(Ban, *Session))

AddBanHandler adds a function that will be called every time a ban message is received

func (*Session) AddBroadcastHandler

func (s *Session) AddBroadcastHandler(fn func(Broadcast, *Session))

AddBroadcastHandler adds a function that will be called every time a broadcast is sent to the chat

func (*Session) AddDonationHandler

func (s *Session) AddDonationHandler(fn func(Donation, *Session))

AddDonationHandler adds a function that will be called every time a donation message is received

func (*Session) AddErrorHandler

func (s *Session) AddErrorHandler(fn func(string, *Session))

AddErrorHandler adds a function that will be called every time an error message is received

func (*Session) AddJoinHandler

func (s *Session) AddJoinHandler(fn func(RoomAction, *Session))

AddJoinHandler adds a function that will be called every time a user join the chat

func (*Session) AddMessageHandler

func (s *Session) AddMessageHandler(fn func(Message, *Session))

AddMessageHandler adds a function that will be called every time a message is received

func (*Session) AddMuteHandler

func (s *Session) AddMuteHandler(fn func(Mute, *Session))

AddMuteHandler adds a function that will be called every time a mute message is received

func (*Session) AddNamesHandler

func (s *Session) AddNamesHandler(fn func(Names, *Session))

AddNamesHandler adds a function that will be called every time a names message is received

func (*Session) AddPMHandler

func (s *Session) AddPMHandler(fn func(PrivateMessage, *Session))

AddPMHandler adds a function that will be called every time a private message is received

func (*Session) AddPinHandler

func (s *Session) AddPinHandler(fn func(Pin, *Session))

AddPinHandler adds a function that will be called every time a pin message is received

func (*Session) AddPingHandler

func (s *Session) AddPingHandler(fn func(Ping, *Session))

AddPingHandler adds a function that will be called when a server responds with a pong

func (*Session) AddQuitHandler

func (s *Session) AddQuitHandler(fn func(RoomAction, *Session))

AddQuitHandler adds a function that will be called every time a user quits the chat

func (*Session) AddSocketErrorHandler

func (s *Session) AddSocketErrorHandler(fn func(error, *Session))

AddSocketErrorHandler adds a function that will be called every time a socket error occurs

func (*Session) AddSubOnlyHandler

func (s *Session) AddSubOnlyHandler(fn func(SubOnly, *Session))

AddSubOnlyHandler adds a function that will will be called every time a subonly message is received

func (*Session) AddSubscriptionHandler

func (s *Session) AddSubscriptionHandler(fn func(Subscription, *Session))

AddSubscriptionHandler adds a function that will be called every time a (regular, gifted, or a mass gift) subscription message is received

func (*Session) AddUnbanHandler

func (s *Session) AddUnbanHandler(fn func(Ban, *Session))

AddUnbanHandler adds a function that will be called every time an unban message is received

func (*Session) AddUnmuteHandler

func (s *Session) AddUnmuteHandler(fn func(Mute, *Session))

AddUnmuteHandler adds a function that will be called every time an unmute message is received

func (*Session) AddUserUpdateHandler

func (s *Session) AddUserUpdateHandler(fn func(User, *Session))

AddUserUpdateHandler adds a function that will be called every time user's information gets updated

func (*Session) Close

func (s *Session) Close() error

Close cleanly closes the connection and stops running listeners

func (*Session) GetUser

func (s *Session) GetUser(name string) (User, bool)

GetUser attempts to find the user in the chat room state. If the user is found, returns the user and true, otherwise false is returned as the second parameter.

func (*Session) GetUsers

func (s *Session) GetUsers() []User

GetUsers returns a list of users currently online

func (*Session) Open

func (s *Session) Open() error

Open opens a websocket connection to destinygg chat.

func (*Session) SendAction

func (s *Session) SendAction(message string) error

SendAction calls the SendMessage method but also adds "/me" in front of the message to make it a chat action same caveat with the returned error value applies.

func (*Session) SendBan

func (s *Session) SendBan(nick string, reason string, duration time.Duration, banip bool) error

SendBan bans the user with the given nick. Bans require a ban reason to be specified. If duration is <= 0, the server uses its built-in default duration

func (*Session) SendBroadcast

func (s *Session) SendBroadcast(message string) error

SendBroadcast sends a broadcast message to chat

func (*Session) SendMessage

func (s *Session) SendMessage(message string) error

SendMessage sends the given string as a message to chat. Note: a return error of nil does not guarantee successful delivery. Monitor for error events to ensure the message was sent with no errors.

func (*Session) SendMute

func (s *Session) SendMute(nick string, duration time.Duration) error

SendMute mutes the user with the given nick. If duration is <= 0, the server uses its built-in default duration

func (*Session) SendPermanentBan

func (s *Session) SendPermanentBan(nick string, reason string, banip bool) error

SendPermanentBan bans the user with the given nick permanently. Bans require a ban reason to be specified.

func (*Session) SendPing

func (s *Session) SendPing() error

SendPing sends a ping to the server with the current timestamp.

func (*Session) SendPrivateMessage

func (s *Session) SendPrivateMessage(nick string, message string) error

SendPrivateMessage sends the given user a private message.

func (*Session) SendSubOnly

func (s *Session) SendSubOnly(subonly bool) error

SendSubOnly modifies the chat subonly mode. During subonly mode, only subscribers and some other special user classes are allowed to send messages.

func (*Session) SendUnban

func (s *Session) SendUnban(nick string) error

SendUnban unbans the user with the given nick. Unbanning also removes mutes.

func (*Session) SendUnmute

func (s *Session) SendUnmute(nick string) error

SendUnmute unmutes the user with the given nick.

func (*Session) SetDialer

func (s *Session) SetDialer(d websocket.Dialer)

SetDialer changes the websocket dialer that will be used when connecting to the socket server.

func (*Session) SetURL

func (s *Session) SetURL(u url.URL)

SetURL changes the url that will be used when connecting to the socket server. This should be done before calling *session.Open()

type SubOnly

type SubOnly struct {
	Sender    User
	Timestamp time.Time
	Active    bool
}

SubOnly represents a subonly message from the server if active, only subscribers and some other special user classes are allowed to send messages, until another SubOnly message is received with active set to false

type SubTier

type SubTier struct {
	Tier  int64
	Label string
}

SubTier represents a dgg subscription tier

type Subscription

type Subscription struct {
	Sender    User
	Recipient User
	Timestamp time.Time
	Message   string
	Tier      SubTier
	Quantity  int64
	UUID      string
}

Subscription represents a dgg subscription message

func (*Subscription) IsGift

func (s *Subscription) IsGift() bool

IsGift returns true if the subscription was a gift

func (*Subscription) IsMassGift

func (s *Subscription) IsMassGift() bool

IsMassGift returns true if the subscription was a mass gift

type User

type User struct {
	ID          int64     `json:"id"`
	Nick        string    `json:"nick"`
	Features    []string  `json:"features"`
	CreatedDate time.Time `json:"createdDate"`
	Watching    watching  `json:"watching"`
}

User represents a user with a list of features

func (*User) HasFeature

func (u *User) HasFeature(s string) bool

HasFeature returns true if user has given feature

Jump to

Keyboard shortcuts

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