bot

package module
v0.0.0-...-f91810a Latest Latest
Warning

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

Go to latest
Published: May 27, 2021 License: MIT Imports: 12 Imported by: 1

README

Bot

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. See the following series for a step-by-step tutorial on Building a Twitch.tv Chat Bot with this package.

Installation

Run go get github.com/foresthoffman/bot

Importing

Import this package by including github.com/foresthoffman/bot in your import block.

e.g.

package main

import(
    ...
    "github.com/foresthoffman/bot"
)

Usage

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()
}

That's all, enjoy!

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

View Source
const PSTFormat = "Jan 2 15:04:05 PST"

Variables

View Source
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.

View Source
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

func TimeStamp

func TimeStamp(format string) string

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

func (bb *BasicBot) HandleChat() error

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

func (bb *BasicBot) ReadCredentials() error

Reads from the private credentials file and stores the data in the bot's Credentials field.

func (*BasicBot) Say

func (bb *BasicBot) Say(msg string) error

Makes the bot send a message to the chat channel.

func (*BasicBot) Start

func (bb *BasicBot) Start()

Starts a loop where the bot will attempt to connect to the Twitch IRC server, then connect to the pre-specified channel, and then handle the chat. It will attempt to reconnect until it is told to shut down, or is forcefully shutdown.

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()
}

type OAuthCred

type OAuthCred struct {

	// The bot account's OAuth password.
	Password string `json:"password,omitempty"`

	// The developer application client ID. Used for API calls to Twitch.
	ClientID string `json:"client_id,omitempty"`
}

Jump to

Keyboard shortcuts

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