ircbot

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 13 Imported by: 2

README

ircbot

Simple irc bot package in Go

Example of implementation can be found at ttps://github.com/zaibon/zbibot

Installation

go get github.com/zaibon/ircbot

Usage

import (
	"github.com/zaibon/ircbot"
	"github.com/zaibon/ircbot/actions"
)


func main(){
	//create new bot
	channels := string[]{
		"go-nuts",
	}
	b := ircbot.NewIrcBot("ircbot", "ircbot", "irc.freenode.net", "6667", channels, "irc.db")

	//add custom intern actions
	b.AddInternAction(&actions.Greet{})
	b.AddInternAction(&actions.TitleExtract{})
	b.AddInternAction(actions.NewLogger(b))
	b.AddInternAction(actions.NewURLLog(b))

	//add command fire by users
	b.AddUserAction(&actions.Help{})

	//connectin to server, listen and serve
	b.Connect(os.Getenv("MY_PASSWORD"))

	//block until we send something to b.Exit channel
	<-b.Exit
	b.Disconnect()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionFunc

type ActionFunc func(b *IrcBot, m *IrcMsg)

ActionFunc is the type of function used in Actioner

type Actioner

type Actioner interface {
	// array of IRC or user command on which the action will be triggered (MSG)
	// example for IRC command: JOIN, PRIVMSG
	// example for user command: .myCmd
	Command() []string

	// used to display help about the action
	Usage() string

	// actual action done by the bot
	Do(b *IrcBot, m *IrcMsg)
}

Actioner is the interface that objects need to implement custom action for the bot

type IrcBot

type IrcBot struct {
	// identity
	User string
	Nick string

	//channel to send *IrcMsg to the goroutine that handle input message
	//You usualy don't need to send anything there. It's useful when creating custom actionner
	ChIn chan *IrcMsg
	//channel to send *IrcMsg to the goroutine that handle output message
	//every *IrcMsg send in this channel will be send to the server
	//You usualy don't need to send anything there. It's useful when creating custom actionner
	ChOut chan *IrcMsg
	//channel to send *IrcMsg to the goroutine that handle errors
	ChError chan error

	// exit flag
	Exit chan bool
	// contains filtered or unexported fields
}

IrcBot represents the bot in general

func NewIrcBot

func NewIrcBot(user, nick, server string, port uint, channels []string, DBPath string) *IrcBot

func (*IrcBot) AddInternAction

func (b *IrcBot) AddInternAction(a Actioner)

AddInternAction add an action to excecute on internal command (join,connect,...) command is the internal command to handle, action is an ActionFunc callback

func (*IrcBot) AddUserAction

func (b *IrcBot) AddUserAction(a Actioner)

AddUserAction add an action fired by the user to handle command is the commands send by user, action is an ActionFunc callback

func (*IrcBot) Connect

func (b *IrcBot) Connect(password string) error

Connect connects the bot to the server and joins the channels

func (*IrcBot) DBConnection

func (b *IrcBot) DBConnection() (*db.DB, error)

DBConnection return a new connection do the database. Use it if your custom action need to access the database

func (*IrcBot) Disconnect

func (b *IrcBot) Disconnect()

Disconnect sends QUIT command to server and closes connections

func (*IrcBot) GetActioner

func (b *IrcBot) GetActioner(actionName string) (Actioner, error)

GetActionUsage returns the Actioner from the user actions map or return an error if no action if found with this name Usefull if you want to access actioner information within other actioner see Help actionner for example

func (*IrcBot) GetActionnersCmds

func (b *IrcBot) GetActionnersCmds() []string

GetActionnersCmds returns all registred user actioners commands

func (*IrcBot) Say

func (b *IrcBot) Say(channel string, text string)

Say makes the bot say text to channel

func (*IrcBot) String

func (b *IrcBot) String() string

String implements the Stringer interface

type IrcMsg

type IrcMsg struct {
	Raw    string
	Prefix string

	Command   string
	CmdParams []string

	Trailing []string
}

IrcMsg represent a message receive or send to the irc server

func NewIrcMsg

func NewIrcMsg() *IrcMsg

func ParseLine

func ParseLine(line string) *IrcMsg

ParseLine parse a line receive from server and return a new IrcMsg object

func (*IrcMsg) Channel

func (m *IrcMsg) Channel() string

Channel return the channel that send the IrcMsg

func (*IrcMsg) Nick

func (m *IrcMsg) Nick() string

Channel return the nickname of the user who send IrcMsg

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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