Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Strip bot nickname from beginning of messages #120

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package irc

import (
"crypto/tls"
"fmt"
"log"
"regexp"
"strings"

"github.com/go-chat-bot/bot"
Expand All @@ -24,9 +26,10 @@ type Config struct {
}

var (
ircConn *ircevent.Connection
config *Config
b *bot.Bot
ircConn *ircevent.Connection
config *Config
b *bot.Bot
nickStartRE *regexp.Regexp
)

const protocol = "irc"
Expand All @@ -52,7 +55,7 @@ func onPRIVMSG(e *ircevent.Event) {
Channel: e.Arguments[0],
IsPrivate: e.Arguments[0] == ircConn.GetNick()},
&bot.Message{
Text: e.Message(),
Text: nickStartRE.ReplaceAllString(e.Message(), ""),
},
&bot.User{
ID: e.Host,
Expand Down Expand Up @@ -116,6 +119,8 @@ func SetUp(c *Config) *bot.Bot {
Server: c.Server,
},
)
// prepare regex to strip from messages - nick followed by colon/comma and spaces
nickStartRE = regexp.MustCompile(fmt.Sprintf("%s[,:] *", c.Nick))

ircConn.AddCallback("001", onWelcome)
ircConn.AddCallback("PRIVMSG", onPRIVMSG)
Expand Down