Documentation
¶
Overview ¶
The bot package provides a set of functions that control a basic Twitch.tv chat bot. The package also exposes an interface which can be used to create a custom chat bot.
Basic usage:
``` package main
import (
"github.com/foresthoffman/bot" "time"
)
func main() {
// Replace the channel name, bot name, and the path to the private directory with your respective
// values.
myBot := bot.BasicBot{
Channel: "twitch",
MsgRate: time.Duration(20/30) * time.Millisecond,
Name: "TwitchBot",
Port: "6667",
PrivatePath: "../private/oauth.json",
Server: "irc.chat.twitch.tv",
}
myBot.Start()
}
```
Index ¶
Constants ¶
const PSTFormat = "Jan 2 15:04:05 PST"
Variables ¶
var CmdRegex *regexp.Regexp = regexp.MustCompile(`^!(\w+)\s?(\w+)?`)
Regex for parsing user commands, from already parsed PRIVMSG strings.
First matched group is the command name and the second matched group is the argument for the command.
var MsgRegex *regexp.Regexp = regexp.MustCompile(`^:(\w+)!\w+@\w+\.tmi\.twitch\.tv (PRIVMSG) #\w+(?: :(.*))?$`)
Regex for parsing PRIVMSG strings.
First matched group is the user's name and the second matched group is the content of the user's message.
Functions ¶
Types ¶
type BasicBot ¶
type BasicBot struct {
// The channel that the bot is supposed to join. Note: The name MUST be lowercase, regardless
// of how the username is displayed on Twitch.tv.
Channel string
// The credentials necessary for authentication.
Credentials *OAuthCred
// A forced delay between bot responses. This prevents the bot from breaking the message limit
// rules. A 20/30 millisecond delay is enough for a non-modded bot. If you decrease the delay
// make sure you're still within the limit!
//
// Message Rate Guidelines: https://dev.twitch.tv/docs/irc#irc-command-and-message-limits
MsgRate time.Duration
// The name that the bot will use in the chat that it's attempting to join.
Name string
// The port of the IRC server.
Port string
// A path to a limited-access directory containing the bot's OAuth credentials.
PrivatePath string
// The domain of the IRC server.
Server string
// contains filtered or unexported fields
}
func (*BasicBot) Connect ¶
func (bb *BasicBot) Connect()
Connects the bot to the Twitch IRC server. The bot will continue to try to connect until it succeeds or is forcefully shutdown.
func (*BasicBot) Disconnect ¶
func (bb *BasicBot) Disconnect()
Officially disconnects the bot from the Twitch IRC server.
func (*BasicBot) HandleChat ¶
Listens for and logs messages from chat. Responds to commands from the channel owner. The bot continues until it gets disconnected, told to shutdown, or forcefully shutdown.
func (*BasicBot) JoinChannel ¶
func (bb *BasicBot) JoinChannel()
Makes the bot join its pre-specified channel.
func (*BasicBot) ReadCredentials ¶
Reads from the private credentials file and stores the data in the bot's Credentials field.
type Bot ¶
type Bot interface {
// Opens a connection to the Twitch.tv IRC chat server.
Connect()
// Closes a connection to the Twitch.tv IRC chat server.
Disconnect()
// Listens to chat messages and PING request from the IRC server.
HandleChat() error
// Joins a specific chat channel.
JoinChannel()
// Parses credentials needed for authentication.
ReadCredentials() error
// Sends a message to the connected channel.
Say(msg string) error
// Attempts to keep the bot connected and handling chat.
Start()
}