chat

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2022 License: MPL-2.0 Imports: 12 Imported by: 4

Documentation

Overview

Package chat implements the official classic Battle.net chat API.

Example
package main

import (
	"fmt"

	"github.com/nielsAD/gowarcraft3/network"
	"github.com/nielsAD/gowarcraft3/network/chat"
	"github.com/nielsAD/gowarcraft3/protocol/capi"
)

func main() {
	bot, err := chat.NewBot(&chat.Config{
		Endpoint: capi.Endpoint + ".example",
		APIKey:   "12345678901234567890",
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer bot.Close()

	// Connect to server
	if err = bot.Connect(); err != nil {
		fmt.Println(err)
		return
	}

	// Print incoming chat messages
	bot.On(&capi.MessageEvent{}, func(ev *network.Event) {
		var msg = ev.Arg.(*capi.MessageEvent)
		fmt.Printf("[%d] %s\n", msg.UserID, msg.Message)
	})

	// Run() blocks until the connection is closed
	bot.Run()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedPacket = errors.New("chat: Received unexpected packet")
)

Errors

Functions

This section is empty.

Types

type Bot

type Bot struct {
	network.EventEmitter
	network.CAPIConn

	// Set once before Connect(), read-only after that
	Config
	// contains filtered or unexported fields
}

Bot implements a basic chat bot using the official classic Battle.net chat API Public methods/fields are thread-safe unless explicitly stated otherwise

func NewBot

func NewBot(conf *Config) (*Bot, error)

NewBot initializes a Bot struct

func (*Bot) BanUser

func (b *Bot) BanUser(uid int64) error

BanUser bans a user from the channel

func (*Bot) Channel

func (b *Bot) Channel() string

Channel currently chatting in

func (*Bot) Connect

func (b *Bot) Connect() error

Connect opens a new connection to server and joins chat

func (*Bot) InitDefaultHandlers

func (b *Bot) InitDefaultHandlers()

InitDefaultHandlers adds the default callbacks for relevant packets

func (*Bot) KickUser

func (b *Bot) KickUser(uid int64) error

KickUser kicks a user from the channel

func (*Bot) RPC

func (b *Bot) RPC(command string, arg ...interface{}) (interface{}, error)

RPC executes Remote Procedure Call cmd asynchronously, retries on timeout/rate-limit Needs to called in a goroutine while Run() is running asynchronously to process incoming packets

func (*Bot) Run

func (b *Bot) Run() error

Run reads packets and emits an event for each received packet Not safe for concurrent invocation

func (*Bot) SendEmote

func (b *Bot) SendEmote(s string) error

SendEmote sends an emote on behalf of a bot

func (*Bot) SendMessage

func (b *Bot) SendMessage(s string) error

SendMessage sends a chat message to the channel

func (*Bot) SendWhisper

func (b *Bot) SendWhisper(uid int64, s string) error

SendWhisper sends a chat message to one user in the channel

func (*Bot) SetModerator

func (b *Bot) SetModerator(uid int64) error

SetModerator sets the current chat moderator to a member of the current chat

func (*Bot) UnbanUser

func (b *Bot) UnbanUser(username string) error

UnbanUser un-bans a user from the channel

func (*Bot) User

func (b *Bot) User(uid int64) (*User, bool)

User in channel by id

func (*Bot) Users

func (b *Bot) Users() map[int64]User

Users in channel

type Config

type Config struct {
	Endpoint   string
	APIKey     string
	RPCTimeout time.Duration
}

Config for chat.Bot

type User

type User struct {
	UserID   int64
	Username string
	Flags    UserFlags

	ProgramID string
	Rate      string
	Rank      string
	Wins      string

	Joined   time.Time
	LastSeen time.Time
}

User in chat

func (*User) Operator

func (u *User) Operator() bool

Operator in channel

func (*User) Update added in v1.3.0

func (u *User) Update(ev *capi.UserUpdateEvent)

Update u with UserUpdateEvent information

type UserFlags added in v1.3.0

type UserFlags uint32

UserFlags enum

const (
	UserFlagAdmin UserFlags = 1 << iota
	UserFlagModerator
	UserFlagSpeaker
	UserFlagMuteGlobal
	UserFlagMuteWhisper
)

User flags

func UnmarshalUserFlags added in v1.3.0

func UnmarshalUserFlags(arr []string) UserFlags

UnmarshalUserFlags from its JSON representation

func (UserFlags) Marshal added in v1.3.0

func (f UserFlags) Marshal() []string

Marshal to its JSON representation

func (UserFlags) String added in v1.3.0

func (f UserFlags) String() string

type UserJoined added in v1.3.0

type UserJoined struct {
	User
}

UserJoined event

type UserLeft added in v1.3.0

type UserLeft struct {
	User
}

UserLeft event

type UserUpdate added in v1.3.0

type UserUpdate struct {
	User
}

UserUpdate event

Jump to

Keyboard shortcuts

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