chatbot

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 10 Imported by: 1

README

twitch-chatbot test

Twitch chatbot in Go (Golang)

Simple chatbot interface based on github.com/gempir/go-twitch-irc

Install

go get github.com/vikpe/twitch-chatbot

Generate chatbot oauth token

Usage


package main

import (
	"fmt"
	"os"

	"github.com/gempir/go-twitch-irc/v4"
	chatbot "github.com/vikpe/twitch-chatbot"
)

func main() {
	// init
	username := "bot_username"
	oauth := "oauth:bbbbbbbbbbbbb"
	channel := "channel_name"
	commandPrefix := '!'

	myBot := chatbot.NewChatbot(username, oauth, channel, commandPrefix)

	// event callbacks
	myBot.OnStarted = func() { fmt.Println("chatbot started") }
	myBot.OnConnected = func() { fmt.Println("chatbot connected") }
	myBot.OnStopped = func(sig os.Signal) {
		fmt.Println(fmt.Sprintf("chatbot stopped (%s)", sig))
	}

	// command handlers
	myBot.AddCommand("hello", func(cmd chatbot.Command, msg twitch.PrivateMessage) {
		myBot.Reply(msg, "world!")
	})

	myBot.AddCommand("test", func(cmd chatbot.Command, msg twitch.PrivateMessage) {
		myBot.Say(fmt.Sprintf("%s called the test command using args %s", msg.User.Name, cmd.ArgsToString()))
	})

	myBot.AddCommand("mod_only", func(cmd chatbot.Command, msg twitch.PrivateMessage) {
		if !chatbot.IsModerator(msg.User) {
			myBot.Reply(msg, "mod_only is only allowed by moderators.")
			return
		}

		myBot.Say(fmt.Sprintf("%s called the mod_only command", msg.User.Name))
	})

	myBot.AddCommand("sub_only", func(cmd chatbot.Command, msg twitch.PrivateMessage) {
		if !chatbot.IsSubscriber(msg.User) {
			myBot.Reply(msg, "sub_only is only allowed by subscribers.")
			return
		}

		myBot.Say(fmt.Sprintf("%s called the sub_only command", msg.User.Name))
	})

	fmt.Println(myBot.GetCommands(", ")) // "hello, test, mod_only, sub_only"

	myBot.Start() // blocking operation
}

Methods

Start()
Stop()
Say(text string)
Reply(msg twitch.PrivateMessage, replyText string)
AddCommand(name string, handler CommandHandler)
GetCommands(sep string)
Callback methods
OnStarted()
OnConnected()
OnStopped(os.Signal)
OnUnknownCommand(cmd Command, msg twitch.PrivateMessage)

OnUnknownCommand defaults to:

func(cmd Command, msg twitch.PrivateMessage) {
replyMessage := fmt.Sprintf(`unknown command "%s". available commands: %s`, cmd.Name, bot.GetCommands(", "))
bot.Reply(msg, replyMessage)
}
User methods
IsBroadcaster(user twitch.User)
IsModerator(user twitch.User)
IsSubscriber(user twitch.User)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsBroadcaster

func IsBroadcaster(user twitch.User) bool

func IsCommand

func IsCommand(prefix rune, text string) bool

func IsModerator added in v0.2.0

func IsModerator(user twitch.User) bool

func IsSubscriber added in v0.2.0

func IsSubscriber(user twitch.User) bool

Types

type Chatbot

type Chatbot struct {
	OnStarted        func()
	OnConnected      func()
	OnStopped        func(os.Signal)
	OnUnknownCommand CommandHandler
	// contains filtered or unexported fields
}

func NewChatbot

func NewChatbot(username string, oauth string, channel string, commandPrefix rune) *Chatbot

func (*Chatbot) AddCommand

func (b *Chatbot) AddCommand(name string, handler CommandHandler)

func (*Chatbot) GetCommands added in v0.1.1

func (b *Chatbot) GetCommands(sep string) string

func (*Chatbot) Reply

func (b *Chatbot) Reply(msg twitch.PrivateMessage, replyText string)

func (*Chatbot) Say

func (b *Chatbot) Say(text string)

func (*Chatbot) Start

func (b *Chatbot) Start()

func (*Chatbot) Stop

func (b *Chatbot) Stop()

type Command

type Command struct {
	Name string
	Args []string
}

func NewCommand

func NewCommand(name string, args ...string) Command

func NewCommandFromMessage

func NewCommandFromMessage(prefix rune, text string) (Command, error)

func (Command) ArgsToString

func (c Command) ArgsToString() string

type CommandHandler

type CommandHandler func(cmd Command, msg twitch.PrivateMessage)

Jump to

Keyboard shortcuts

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