botsbyuberswe

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

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

Go to latest
Published: Apr 6, 2020 License: MIT Imports: 24 Imported by: 0

README

Twitch Bot

By UberSwe

This a project I started for fun to see how things work and if I could do it well. I probably spent way too much time on it and I should have gone for something simpler but here we are.

Run the project with go run cmd/twitch/main.go

Make sure to copy the .env.example and save it as .env adding your twitch client id and secret. You can find more info about this in the Twitch API Documentation.

By default the project will listen at http://localhost:8010 but you can configure this using parameters (to be documented).

If you go to this url you will be presented with a connect button which you can click to connect your channel account. Once connected you will get to the dashboard.

On the dashboard you will have a link to connect a bot, I recommend opening a private window or a window in a separate browser and logging into twitch with your bot account there and then following the link. Once connected you will now have a bot associated with your channel. This means you can have your own bot which is kind of the whole point of this project :).

Now, if you specify the UNIVERSAL_BOT and you set it equal to the name of your bot, for example botbyuber then this will be the default bot when someone connects their channel bot have not connected a bot. So you could allow others to user your bot, wohoo!

By connecting you will currently always be in your channels chat, this is temporary. Connecting your bot will also always keep it in chat, this is expected and will always be like this.

Check the Issues page to see what kind of known bugs exist. If you try this and find a bug consider opening an issue.

Contributions are welcome!

After I started this I noticed that StreamLabs Chatbot is a program you can run if you want to have a chatbot with a custom name. This project is similar except you could more easily run this project on a server 24/7 and offer the custom bot name to other people.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init()

Init is called before run and currently makes a seed for random number generators

func RandString

func RandString(n int) string

RandString generates a random string of n length

func Run

func Run()

Run runs the application, it loads the config, starts everything and keeps it running

Types

type Bot

type Bot struct {
	Name                  string         `json:"name,omitempty"`
	UserChannelName       string         `json:"user_channel_name,omitempty"`
	TwitchIRCClient       *twitch.Client `json:"-"`
	TwitchOAuthClient     *helix.Client  `json:"-"`
	TwitchConnectFailures int            `json:"twitch_connect_failures,omitempty"`
	AccessCode            string         `json:"access_code,omitempty"`
	AccessToken           string         `json:"access_token,omitempty"`
	RefreshToken          string         `json:"refresh_token,omitempty"`
	Connected             bool           `json:"connected,omitempty"`
	ConnectAttempts       int            `json:"connect_attempts,omitempty"`
	UserTwitchID          string         `json:"user_twitch_id,omitempty"`
	TokenExpiry           time.Time      `json:"token_expiry,omitempty"`
}

Bot represents twitch users connected via the botCallback route. These should post in users channels, Users should never post in channels

type BotToken

type BotToken struct {
	Token    string
	TwitchID string
}

BotToken is used to identify which user the bot should link to when using the botCallback route

type Channel

type Channel struct {
	Name     string `json:"name,omitempty"`
	IsMod    bool   `json:"is_mod,omitempty"`
	LastHost string `json:"last_host,omitempty"`
	LastRaid string `json:"last_raid,omitempty"`
}

Channel holds channel information about a Twitch channel

type Command

type Command struct {
	Input  string `json:"input,omitempty"`
	Output string `json:"output,omitempty"`
}

Command has in input such as `!so` which if it is detected in a Twitch channel message will trigger the Output to be sent by a Bot to the Twitch channel

type ConnectChannel

type ConnectChannel struct {
	Name    string
	Connect bool
}

ConnectChannel is used to tell Bots to connect to a specific channel

type Cookie struct {
	TwitchID string    `json:"twitch_id,omitempty"`
	Expiry   time.Time `json:"expiry,omitempty"`
}

Cookie is stored in the database and identified by a key stored in a http.cookie. This key can then be used to fetch a User via the TwitchID as an identifier

type State

type State struct {
	Commands  []Command  `json:"commands,omitempty"`
	Variables []Variable `json:"variables,omitempty"`
}

State represents variables we show in the frontend, part of the User struct

type Template

type Template struct {
	ModifiedHash string
	BotURL       string
	BotName      string
	BotConnected bool
}

Template holds template variables which we pass to html files that render the frontend

type User

type User struct {
	TwitchID              string         `json:"twitch_id,omitempty"`
	Email                 string         `json:"email,omitempty"`
	AccessCode            string         `json:"access_code,omitempty"`
	AccessToken           string         `json:"access_token,omitempty"`
	RefreshToken          string         `json:"refresh_token,omitempty"`
	TokenExpiry           time.Time      `json:"token_expiry,omitempty"`
	Scopes                []string       `json:"scopes,omitempty"`
	TokenType             string         `json:"token_type,omitempty"`
	Channel               Channel        `json:"channel,omitempty"`
	State                 State          `json:"state,omitempty"`
	Connected             bool           `json:"connected,omitempty"`
	ConnectAttempts       int            `json:"connect_attempts,omitempty"`
	BotToken              string         `json:"bot_token,omitempty"`
	TwitchIRCClient       *twitch.Client `json:"-"`
	TwitchOAuthClient     *helix.Client  `json:"-"`
	TwitchConnectFailures int            `json:"twitch_connect_failures,omitempty"`
}

User represents a Twitch user connected to our application

type Variable

type Variable struct {
	Name        string    `json:"name,omitempty"`
	Value       string    `json:"value,omitempty"`
	Description string    `json:"description,omitempty"`
	Expiry      time.Time `json:"expiry,omitempty"`
}

Variable can be used in commands, this might be things like the last user that raided or hosted a channel or the number of subscribers a channel has

type Wconn

type Wconn struct {
	Connection *websocket.Conn
	TwitchID   string
}

Wconn is Short for WebsocketConnection

type WebsocketMessage

type WebsocketMessage struct {
	Key            string                `json:"key,omitempty"`
	Channel        string                `json:"channel,omitempty"`
	Command        string                `json:"command,omitempty"`
	Text           string                `json:"text,omitempty"`
	MsgParams      map[string]string     `json:"msg_params,omitempty"`
	PrivateMessage twitch.PrivateMessage `json:"private_message,omitempty"`
	State          State                 `json:"state,omitempty"`
	AlertType      string                `json:"alert_type,omitempty"`
	BotName        string                `json:"bot_name,omitempty"`
	TwitchID       string                `json:"-"`
}

WebsocketMessage is the struct for the data sent over websocket connections

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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