dad

package
v0.0.0-...-1a91e6f Latest Latest
Warning

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

Go to latest
Published: May 8, 2020 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package dad is an extension of hellabot that plays the role of an IRC chat bot, either as a mom or a dad

Index

Constants

View Source
const (
	// ReplyType indicates to send a message to where the message came from
	ReplyType = iota // 0
	// MessageType indicates to send a message to a specified user or channel
	MessageType // 1
	// NoticeType indicates to send a notice to a specified user or channel
	NoticeType // 2
	// ActionType indicates to send an action to a specified user or channel
	ActionType // 3
	// TopicType indicates to set the topic for the current channel
	TopicType // 4
	// SendType indicates to send any command to the server
	SendType // 5
	// ChModeType indicates to change a user's mode in the channel
	ChModeType // 6
	// JoinType indicates to join a channel
	JoinType // 7
	// PartType indicates to leave a specified channel
	PartType // 8
	// QuitType indicates to quit the existing IRC connection
	QuitType // 9
)

Variables

View Source
var AdminTrigger = hbot.Trigger{
	func(bot *hbot.Bot, m *hbot.Message) bool {
		return (m.From == Irc.IRCConfig.Admin)
	},
	func(bot *hbot.Bot, m *hbot.Message) bool {
		responded := Irc.PerformReply(m, true)
		if !responded {
			Irc.PerformReply(m, false)
		}
		return false
	},
}

AdminTrigger is for the admin user. If no admin response is performed, a user reponse is attempted.

View Source
var UserTrigger = hbot.Trigger{
	func(bot *hbot.Bot, m *hbot.Message) bool {
		return (m.From != Irc.IRCConfig.Admin)
	},
	func(bot *hbot.Bot, m *hbot.Message) bool {
		Irc.PerformReply(m, false)
		return false
	},
}

UserTrigger is for all non-admin users.

Functions

func AddArticle

func AddArticle(s string) string

AddArticle prepands the given string with an "a" or "an" based on the first word and returns the result

func GetChannelTargetOrDefault

func GetChannelTargetOrDefault(to, msg string) (string, string)

GetChannelTargetOrDefault looks for a "#channel:" or "user:" in the passed msg. If found, it's split from the message and returned as the new destination, alongside the rest of the message. If not found, the original destination (to) and msg is returned

func GetICanHazDadJoke

func GetICanHazDadJoke() string

GetICanHazDadJoke sends an HTTP request to https://icanhazdadjoke.com and returns a parsed string of the joke

func GetVariableRegex

func GetVariableRegex(s string, regex RegexData) string

GetVariableRegex returns only the part of the string that matches the Variable portion of RegexData

func HandleTextReplacement

func HandleTextReplacement(message *hbot.Message, response *Response, variable string) string

HandleTextReplacement determines what to do with each flag in a response #a indicates an article "a" or "an" #c indicates a response generated from an action handler #u indicates the name of the user who sent the trigger message #v indicates the string captured by the Variable regex Other conditionals can be added here. A string with all flags replaced is returned.

func MustCompileRegexData

func MustCompileRegexData(regex RegexData) (*regexp.Regexp,
	*regexp.Regexp)

MustCompileRegexData compiles and returns all parts of the passed RegexData variable

func RemoveLiteralRegex

func RemoveLiteralRegex(s string, regex string) string

RemoveLiteralRegex removes a matching literal that is passed as regex from the given string, s, and returns the result.

func RemoveTriggerRegex

func RemoveTriggerRegex(s string, regex RegexData) string

RemoveTriggerRegex removes Trigger matching substrings from the given string This doesn't necessarily leave behind the Variable portion, as there could be additional message content that matches neither the Trigger nor Variable.

func StringInSlice

func StringInSlice(a string, s []string) int

StringInSlice checks slice s for string a and returns the first matching index, and -1 otherwise

Types

type BotConfiguration

type BotConfiguration struct {
	AdminSpeak []SpeakData
	Channels   []string
	Debug      bool
	Name       string
	Speak      []SpeakData
}

BotConfiguration gives structure to any bot config file

type ICanHazDadJoke

type ICanHazDadJoke struct {
	ID     string `json:"id"`
	Joke   string `json:"joke"`
	Status int    `json:"status"`
}

ICanHazDadJoke declares the expected json format of jokes from ICanHazDadJoke.com

type IRCBot

type IRCBot struct {
	Bot           *hbot.Bot
	BotConfig     BotConfiguration
	BotConfigFile string
	Muted         MutedList
	MutedFile     string
	IRCConfig     IRCConfiguration
	IRCConfigFile string
	CurrentReply  Reply
	LastReply     Reply
}

IRCBot is an extension of hellabot's Bot that includes an indicator for whether the bot is acting as mom or dad, the config information, and the last reply sent by the bot

var Irc IRCBot

Irc is the global variable that tracks any common, constant values between the two bots. This is only accessed once at startup by each bot

func (IRCBot) AddTrigger

func (ib IRCBot) AddTrigger(t hbot.Trigger)

AddTrigger adds the given Trigger to the bot

func (*IRCBot) FormatReply

func (ib *IRCBot) FormatReply(message *hbot.Message, adminSpeak bool, sIndex int) Reply

FormatReply formulates the bot's response given the message, whether or not the sender was an admin (adminSpeak), and the index of the SpeakData to format the reply to (sIndex). It returns the reply with set content and destination (but not the time). Message content is split into multiple messages anywhere a "\n" is found

func (*IRCBot) Ground

func (ib *IRCBot) Ground(name string)

Ground checks the list of currently grounded users and adds the name if it has not yet been added.

func (*IRCBot) MessageRateMet

func (ib *IRCBot) MessageRateMet(message *hbot.Message) bool

MessageRateMet checks whether or not enough time has passed since the Last reply was sent. If the message just sent was from an admin, ignore time passed.

func (*IRCBot) PerformAction

func (ib *IRCBot) PerformAction(reply Reply, speak *SpeakData,
	variable string) (Reply, string)

PerformAction evaluates the action associated with the response (speak) and performs any necessary actions. Any content that needs to be used in the response will be returned.

func (*IRCBot) PerformReply

func (ib *IRCBot) PerformReply(m *hbot.Message, adminSpeak bool) bool

PerformReply determines whether or not a reply should be formulated and then performs it by passing it the bot in use (bot), the message just sent (m), and whether or not the sender was the admin (adminSpeak). If an action was performed, return true.

func (*IRCBot) ReadConfig

func (ib *IRCBot) ReadConfig(ircFileName, botFileName, mutedFileName string)

ReadConfig reads each config file and returns a struct of each

func (*IRCBot) Run

func (ib *IRCBot) Run(ircConfigFileName, botConfigFileName, mutedListFileName string)

Run starts an instance of the bot, with variable dad indicating whether the bot should behave like a dad or a mom

func (*IRCBot) SendMessageType

func (ib *IRCBot) SendMessageType(m *hbot.Message, to string, replyType int, msg string)

SendMessageType accepts the bot and message and sends a message (msg) to the specified user/channel (to) of type replyType.

func (*IRCBot) SendReply

func (ib *IRCBot) SendReply(m *hbot.Message, reply Reply) bool

SendReply accepts the bot, message, and the bot's reply. It returns true if at least one line was sent, and false if nothing was sent.

func (*IRCBot) TestMessage

func (ib *IRCBot) TestMessage(regex RegexData, message *hbot.Message) bool

TestMessage tests the passed message against the passed regex and returns whether or not a match was found

func (*IRCBot) Unground

func (ib *IRCBot) Unground(name string)

Unground checks the list of grounded users for the requested name and removes it if it is found.

func (*IRCBot) UpdateBotConfig

func (ib *IRCBot) UpdateBotConfig()

UpdateBotConfig parses the existing bot configuration data into a writable format and updates the corresponding data file.

type IRCBotInterface

type IRCBotInterface interface {
	AddTrigger(t *hbot.Trigger)
	Run()
	ReadConfig()
	PerformReply()
	SendReply()
	SendMessageType()
	TestMessage()
	UpdateBotConfig()
}

IRCBotInterface is a temporary place-holder

type IRCConfiguration

type IRCConfiguration struct {
	Admin       string
	IP          string
	MessageRate int
	Timeout     int
}

IRCConfiguration contains all content that should be common between the bots

type MutedList

type MutedList struct {
	Bots  []string
	Users []string
}

MutedList keeps track of grounded users and bots as they are added or removed.

type RegexData

type RegexData struct {
	Trigger  string
	Variable string
}

RegexData makes it a little easier to capture text by having regex that seperates the message content into two parts. The Trigger section acts as the command that the bot responds to. The Variable is what the bot will take and use in its response (if needed). If no reuse of message content is needed in a response, leave Variable blank.

type Reply

type Reply struct {
	Content string
	To      string
	Sent    time.Time
	Type    int
}

Reply includes the final formatted response (all text replacement blocks dealt with), the destination, the time the message was sent at, and the type.

type Response

type Response struct {
	Message string
	Count   int
	Type    int
}

ResponseData contains the bot's reply, the number of times the reply has been sent, and the type of response the bot should give. Message may contain action tags (#<char>) for different types of text replacement/manipulation.

type SpeakData

type SpeakData struct {
	Action    string
	Regex     RegexData
	Responses []Response `json:"Response"`
}

SpeakData is the regex-to-response pairing for each possible response. There can be more than one response, and it will be chosen semi-randomly.

func (*SpeakData) GetRandomLeastUsedResponse

func (speak *SpeakData) GetRandomLeastUsedResponse() *Response

GetRandomLeastUsedResponse chooses a random response among all within the given speak data, giving priority to responses that have not yet been used as much. It returns a reference to the response it chose

func (*SpeakData) GetRandomResponse

func (speak *SpeakData) GetRandomResponse() *Response

GetRandomResponse chooses a random response among all within the given speak data. It returns a reference to the response it chose.

Jump to

Keyboard shortcuts

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