Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vikpe/twitch-chatbot

Twitch chatbot in Go (Golang)
https://github.com/vikpe/twitch-chatbot

chatbot irc twitch

Last synced: 4 months ago
JSON representation

Twitch chatbot in Go (Golang)

Awesome Lists containing this project

README

        

# twitch-chatbot [![test](https://github.com/vikpe/twitch-chatbot/actions/workflows/test.yml/badge.svg)](https://github.com/vikpe/twitch-chatbot/actions/workflows/test.yml)

> Twitch chatbot in Go (Golang)

Simple chatbot interface based on [github.com/gempir/go-twitch-irc](https://github.com/gempir/go-twitch-irc)

## Install

```shell
go get github.com/vikpe/twitch-chatbot
```

## Generate chatbot oauth token

* [Twitch Chat OAuth Password Generator](https://twitchapps.com/tmi/)

## Usage

```go

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:

```go
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)` |