Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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)
- Host: GitHub
- URL: https://github.com/vikpe/twitch-chatbot
- Owner: vikpe
- License: mit
- Created: 2022-07-09T08:52:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-19T19:42:40.000Z (over 1 year ago)
- Last Synced: 2024-06-21T08:14:28.116Z (7 months ago)
- Topics: chatbot, irc, twitch
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)` |