hbot

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 14 Imported by: 4

README

Hellivabot

GoDoc

Hellivabot is forked from the super WhyRUSleeping's HellaBot, an IRC bot in Go that, unfortunately does not play nice with Twitch's chat particularities. Thanks for WhyRUSleeping and everyone who contributed to Hellabot!

Overview

This is an IRC bot written in Go focused on Twitch's chats. That means that the message handling, the expected parameters, values and details are always to favour how Twitch works. As mentioned above, if you need something less specific, check out WhyRUSleeping's HellaBot project.

Bots for Twitch's chat are useful to manage the chats, help users and keep thing running nice while you livestream. This bot is not programmed to do anything out-of-the-box, except the most basic connection and authentication process, but you can easily add triggers for all events that you want it to react to.

If you want some inspiration, I invite you to check out my implementation of the Hellivabot that I use for my livestreams: BOTavio_KR-twitch-bot. If you want to see it in action, come visit my live at otavio_kr.

The Name

The original project is called Hellabot, which sounds to me like "hell-of-a-bot". Initially, I thought calling mine as "helluvabot", but I thought I could squeeze a "live" in there, so it is now something like "hell-live-a-bot".

Changes from the original project

A different log library

I prefer Sirupsen's Logrus over the chosen log15 used. It is basically personal preference, and since I would already perform other changes, I decided to change this as well. Like I said, nothing wrong with the original log package, I'm just more comfortable with Logrus.

Request specific capabilities

Twitch requires to request 3 capabilities to have access to data and commands in the chat. This can be easily implemented (just send the requests during connection/authentication), but since this is something so basic and fundamental, I think it makes sense to embed it to the bot "framework".

So, the following capabilities are automatically requested:

  • membership
  • commands
  • tags

Specific tags from incoming PRIVMSGs

Twitch sends in the PRIVMSG a lot of details about the user who sent the message. This broke the original message parser, so I rewrote it in a way that we can get all the details. This gives more options to monitor to trigger and act on them.

Other changes

This an IRC chabot for Twitch. That means that, when in doubt, checkout the commands and standards defined by Twitch and those should be the final list. If any Twitch-specific command is not working, consider that to be a bug, and please open an issue reporting it.

What remains the same

Trigger functionality

This feature is really, really well done, so it haven't changed anything. All triggers for the Hellabot should work for Hellivabot perfectly.

# If message is coming/related to user otaviokr, write in chat "otaviokr said something"
var MyTrigger = hbot.Trigger{
	func (b *hbot.Bot, m *Message) bool {
		return m.From == "otaviokr"
	},
	func (b *hbot.Bot, m *hbot.Message) bool {
		b.Reply(m, "otaviokr said something")
		return false
	},
}

The trigger above makes the bot announce to everyone in chat that something was said by user otaviokr in the current chat. Use the code snippet below to instantiate the bot, add the trigger and start the bot (connect and join the channel).

mybot, err := hbot.NewBot("irc.freenode.net:6667","hellivabot")
if err != nil {
    panic(err)
}
mybot.AddTrigger(MyTrigger)
mybot.Run() // Blocks until exit

Why not merge it to Hellabot?

This is a very specific, biased implementation focused on Twitch and Twitch alone. I understand I would be shrinking Hellabot instead of contributing, enhancing or expanding it. If you think it should be better to have just one single solution, you can get the changes I did here and try to incorporate to Hellabot and submit the Pull Request - and it is up to WhyRUSleeping to accept them.

Documentation

Index

Constants

View Source
const (
	WelcomeSignature                string = ":tmi\\.twitch\\.tv 001 .+ :Welcome, GLHF!"
	JoinSignature                   string = ":(.+)!.+@.+\\.tmi\\.twitch\\.tv JOIN (#.+)"
	LoggedInSignature               string = ":tmi\\.twitch\\.tv 376 .+ :>"
	CapabilityAcknowledgedSignature string = ":tmi\\.twitch\\.tv CAP \\* ACK :twitch\\.tv/.+"

	UNKNOWNSignature       string = ":.+\\.tmi\\.twitch\\.tv (353|366) .+"
	IgnoreSignature        string = ":tmi\\.twitch\\.tv (002|003|004|375|372|376) .+"
	EndOfNameListSignature string = ":.+\\.tmi\\.twitch\\.tv 366 .+ #.+ :End of /NAMES list"

	UserStateSignature string = "@badge-info=.+ :tmi\\.twitch\\.tv USERSTATE .+"
	RoomStateSignature string = "@emote-only=.+ :tmi\\.twitch\\.tv ROOMSTATE .+"

	PrivateMessageUserSignature string = ":(.+)!.+@.+\\.tmi\\.twitch\\.tv"

	PingSignature   string = "PING :tmi\\.twitch\\.tv"
	NoticeSignature string = "@msg-id=(.+):tmi\\.twitch\\.tv (NOTICE) (#.+) :(.+)"
)

Variables

This section is empty.

Functions

func ReconOpt

func ReconOpt() func(*Bot)

func SaslAuth

func SaslAuth(pass string) func(*Bot)

Types

type Bot

type Bot struct {

	// Channel for user to read incoming messages
	Incoming chan *Message

	// Log15 loggger
	log.Logger

	// Exported fields
	Host          string
	Password      string
	Channels      []string
	SSL           bool
	SASL          bool
	HijackSession bool
	// An optional function that connects to an IRC server over plaintext:
	Dial func(network, addr string) (net.Conn, error)
	// An optional function that connects to an IRC server over a secured connection:
	DialTLS func(network, addr string, tlsConf *tls.Config) (*tls.Conn, error)
	// This bots nick
	Nick string
	// Duration to wait between sending of messages to avoid being
	// kicked by the server for flooding (default 200ms)
	ThrottleDelay time.Duration
	// Maxmimum time between incoming data
	PingTimeout time.Duration

	TLSConfig tls.Config
	// contains filtered or unexported fields
}

Bot implements an irc bot to be connected to a given server

func NewBot

func NewBot(host, nick string, options ...func(*Bot)) (*Bot, error)

NewBot creates a new instance of Bot

func (*Bot) Action

func (bot *Bot) Action(who, text string)

Action sends an action to 'who' (user or channel)

func (*Bot) AddTrigger

func (bot *Bot) AddTrigger(h Handler)

AddTrigger adds a trigger to the bot's handlers

func (*Bot) ChMode

func (bot *Bot) ChMode(user, channel, mode string)

ChMode is used to change users modes in a channel operator = "+o" deop = "-o" ban = "+b"

func (*Bot) Close

func (bot *Bot) Close() error

Close closes the bot

func (*Bot) Join

func (bot *Bot) Join(ch string)

Join a channel

func (*Bot) Msg

func (bot *Bot) Msg(who, text string)

Msg sends a message to 'who' (user or channel)

func (*Bot) Notice

func (bot *Bot) Notice(who, text string)

Notice sends a NOTICE message to 'who' (user or channel)

func (*Bot) Part

func (bot *Bot) Part(ch string)

Part a channel. Notice that Twitch does not expect a leave message, like default IRC.

func (*Bot) Reply

func (bot *Bot) Reply(m *Message, text string)

Reply sends a message to where the message came from (user or channel)

func (*Bot) Run

func (bot *Bot) Run()

Run starts the bot and connects to the server. Blocks until we disconnect from the server.

func (*Bot) SASLAuthenticate

func (bot *Bot) SASLAuthenticate(user, pass string)

SASLAuthenticate performs SASL authentication ref: https://github.com/atheme/charybdis/blob/master/doc/sasl.txt

func (*Bot) Send

func (bot *Bot) Send(command string)

Send any command to the server

func (*Bot) SetNick

func (bot *Bot) SetNick(nick string)

SetNick sets the bots nick on the irc server

func (*Bot) StandardRegistration

func (bot *Bot) StandardRegistration()

StandardRegistration performsa a basic set of registration commands

func (*Bot) StartUnixListener

func (bot *Bot) StartUnixListener()

StartUnixListener starts up a unix domain socket listener for reconnects to be sent through

func (*Bot) String

func (bot *Bot) String() string

func (*Bot) Topic

func (bot *Bot) Topic(c, topic string)

Topic sets the channel 'c' topic (requires bot has proper permissions)

func (*Bot) Uptime

func (bot *Bot) Uptime() string

Uptime returns the uptime of the bot

func (*Bot) WaitFor

func (bot *Bot) WaitFor(filter func(*Message) bool)

WaitFor will block until a message matching the given filter is received

type Emote

type Emote struct {
	Code        int
	Occurrences []int
	Size        int
	Text        string
}

type Handler

type Handler interface {
	Handle(*Bot, *Message) bool
}

Handler is used to subscribe and react to events on the bot Server

type Message

type Message struct {
	BadgeInfo   map[string]int   // Used only for subscribers. Value "subscriber/8" means user has been a subscriber for 8 months.
	Badges      map[string]int   // Badges displayed next to display name. Value "admin/1" means user has the version 1 of admin badge.
	ClientNonce string           // Random identifier to link a response to a request.
	Color       string           // User's defined color for their display name, if set.
	DisplayName string           // User's defined name to be displayed in the chat.
	Emotes      map[string]Emote // If the message contains emotes, they are detailed here. Check Emote type for more details.
	Flags       string           //
	Id          string
	Mod         int
	RoomId      int64
	Subscriber  int
	TmiSentTs   int64
	// Turbo int 				// This is deprecated. Use badges instead.
	UserId int64

	Content         string            // This is the original message sent by the user or system.
	ContentNoEmotes string            // This is the message sent by the user, but all emotes have been removed.
	Unparsed        map[string]string // Anything that was not parsed.

	//Time at which this message was recieved
	TimeStamp time.Time

	// Entity that this message was addressed to (channel or user)
	To string

	// Nick of the messages sender (equivalent to Prefix.Name)
	// Outdated, please use .Name
	From string

	Command string

	Params []string
}

func GenerateMessageObject

func GenerateMessageObject(command, channel, username, content string, badgeInfo, badges map[string]int, clientNonce, color, displayName, flags, id string,
	mod, subscriber int, roomId, timestamp, userId int64, emotes map[string]Emote, unparsed map[string]string) *Message

func ParseMessage

func ParseMessage(raw string) (*Message, error)

func ParseTwitchMessage

func ParseTwitchMessage(raw string) (*Message, error)

func (*Message) Param

func (m *Message) Param(i int) string

Param returns the i'th parameter or the empty string if the requested element doesn't exist.

type Trigger

type Trigger struct {
	// Returns true if this trigger applies to the passed in message
	Condition func(*Bot, *Message) bool

	// The action to perform if Condition is true
	// return true if the message was 'consumed'
	Action func(*Bot, *Message) bool
}

Trigger is a Handler which is guarded by a condition

func (Trigger) Handle

func (t Trigger) Handle(b *Bot, m *Message) bool

Handle executes the trigger action if the condition is satisfied

Directories

Path Synopsis
examples
main
This is an example program showing the usage of hellivabot
This is an example program showing the usage of hellivabot
sasl
This is an example program showing the usage of hellivabot
This is an example program showing the usage of hellivabot
commands Module

Jump to

Keyboard shortcuts

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