1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

Chatbox can now relay messages to subscribers

This commit is contained in:
Ash Keel 2021-09-15 17:46:57 +02:00
parent 599e6a5540
commit 6c8d8f6da2
No known key found for this signature in database
GPG key ID: CF2CC050478BD7E5
6 changed files with 67 additions and 6 deletions

View file

@ -50,6 +50,8 @@ interface TwitchBotConfig {
username: string;
oauth: string;
channel: string;
chat_keys: boolean;
chat_history: number;
}
interface StulbeConfig {

View file

@ -108,6 +108,44 @@ export default function TwitchBotSettingsPage(
<a href="https://twitchapps.com/tmi/">https://twitchapps.com/tmi/</a>
</p>
</div>
<div className="field">
<label className="checkbox">
<input
type="checkbox"
checked={twitchBotConfig?.chat_keys ?? false}
disabled={busy}
onChange={(ev) =>
dispatch(
apiReducer.actions.twitchBotConfigChanged({
...twitchBotConfig,
chat_keys: ev.target.checked,
}),
)
}
/>{' '}
Enable chat keys (for 3rd party chat integration)
</label>
</div>
<div className="field">
<label className="label">Chat history</label>
<p className="control">
<input
className="input"
type="number"
disabled={!twitchBotConfig?.chat_keys ?? true}
placeholder="#"
value={twitchBotConfig?.chat_history ?? '5'}
onChange={(ev) =>
dispatch(
apiReducer.actions.twitchBotConfigChanged({
...twitchBotConfig,
chat_history: parseInt(ev.target.value, 10) ?? 0,
}),
)
}
/>
</p>
</div>
<button
className="button"
onClick={() => {

View file

@ -166,7 +166,7 @@ func main() {
failOnError(db.GetJSON(twitch.ConfigKey, &twitchConfig), "Could not retrieve twitch config")
// Create Twitch client
twitchClient, err := twitch.NewClient(twitchConfig, twitchLogger)
twitchClient, err := twitch.NewClient(db, twitchConfig, twitchLogger)
if err == nil {
// Get Twitchbot config

View file

@ -21,6 +21,7 @@ type Bot struct {
lastMessage time.Time
activeUsers map[string]bool
banlist map[string]bool
chatHistory []irc.PrivateMessage
mu sync.Mutex
@ -45,7 +46,6 @@ func NewBot(api *Client, config BotConfig) *Bot {
}
client.OnPrivateMessage(func(message irc.PrivateMessage) {
bot.logger.Debugf("MSG: <%s> %s", message.User.Name, message.Message)
// Ignore messages for a while or twitch will get mad!
if message.Time.Before(bot.lastMessage.Add(time.Second * 2)) {
bot.logger.Debug("message received too soon, ignoring")
@ -65,6 +65,17 @@ func NewBot(api *Client, config BotConfig) *Bot {
}
}
}
if bot.config.EnableChatKeys {
bot.api.db.PutJSON(BotChatEventKey, message)
if bot.config.ChatHistory > 0 {
if len(bot.chatHistory) >= bot.config.ChatHistory {
bot.chatHistory = bot.chatHistory[len(bot.chatHistory)-bot.config.ChatHistory+1:]
}
bot.chatHistory = append(bot.chatHistory, message)
bot.api.db.PutJSON(BotChatHistoryKey, bot.chatHistory)
}
}
})
client.OnUserJoinMessage(func(message irc.UserJoinMessage) {

View file

@ -3,14 +3,16 @@ package twitch
import (
"github.com/nicklaw5/helix"
"github.com/sirupsen/logrus"
"github.com/strimertul/strimertul/database"
)
type Client struct {
db *database.DB
API *helix.Client
logger logrus.FieldLogger
}
func NewClient(config Config, log logrus.FieldLogger) (*Client, error) {
func NewClient(db *database.DB, config Config, log logrus.FieldLogger) (*Client, error) {
if log == nil {
log = logrus.New()
}
@ -34,6 +36,7 @@ func NewClient(config Config, log logrus.FieldLogger) (*Client, error) {
log.Info("obtained API access token")
return &Client{
db: db,
API: api,
logger: log,
}, nil

View file

@ -11,7 +11,14 @@ type Config struct {
const BotConfigKey = "twitch/bot-config"
type BotConfig struct {
Username string `json:"username"`
Token string `json:"oauth"`
Channel string `json:"channel"`
Username string `json:"username"`
Token string `json:"oauth"`
Channel string `json:"channel"`
EnableChatKeys bool `json:"chat_keys"`
ChatHistory int `json:"chat_history"`
}
const BotChatEventKey = "twitch/ev/chat-message"
const BotChatHistoryKey = "twitch/chat-history"
const BotCustomCommands = "twitch/bot-custom-commands"