luxtbot

package module
v0.0.1-pre Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2022 License: GPL-3.0 Imports: 20 Imported by: 0

README

Luxt Bot

待完善

Documentation

Index

Constants

View Source
const (
	MsgTypeArray  = "array"
	MsgTypeString = "string"
)
View Source
const (
	GroupMsgAction   = "send_group_msg"
	PrivateMsgAction = "send_private_msg"
)
View Source
const (
	MsgTypePrivate = "private"
	MsgTypeGroup   = "group"

	MessageEvent = "message"
	NoticeEvent  = "notice"
	RequestEvent = "request"
	MetaEvent    = "meta_event"

	Lifecycle = "lifecycle"
	Heartbeat = "heartbeat"

	SendTimeout = "timeout"
	BotIDMiss   = "miss"
)
View Source
const (
	DefaultLevel       = logrus.WarnLevel
	DefaultFileNameFmt = "./logs/%Y-%m-%d.log"
	DefaultMaxFiles    = uint(15)
	DefaultRotateDura  = time.Hour * 12
)
View Source
const (
	TextMsgSeg      = "text"
	FaceMsgSeg      = "face"
	ImageMsgSeg     = "image"
	RecordMsgSeg    = "record"
	VideoMsgSeg     = "video"
	AtMsgSeg        = "at"
	RPSMsgSeg       = "rps"
	DiceMsgSeg      = "dice"
	ShakeMsgSeg     = "shake"
	PokeMsgSeg      = "poke"
	AnonymousMsgSeg = "anonymous"
	ShareMsgSeg     = "share"
	ContactMsgSeg   = "contact"
	LocationMsgSeg  = "location"
	MusicMsgSeg     = "music"
	ReplyMsgSeg     = "reply"
	ForwardMsgSeg   = "forward"
	NodeMsgSeg      = "node"
	XmlMsgSeg       = "xml"
	JsonMsgSeg      = "json"
)
View Source
const (
	QEmojiBabble      = 13
	QEmojiFrog        = 170
	QEmojiFirecracker = 137
	QEmojiCrab        = 184
)
View Source
const (
	TokenPrefix = "WhYhAvEsPaCe "
	ReconnTimes = 3
)
View Source
const (
	DefaultTimeout = 10
	OffHeartCheck  = 0
)
View Source
const AtAll = int64(-1)
View Source
const ConmandPrefix = "~$#"

命令以 ~$#或者@bot 开头

Variables

View Source
var (
	BackenChain  []BackenUnit
	MsgChain     []MessageUnit
	CmdChain     []CommandUnit
	NoticeChain  []NoticeUnit
	RequestChain []RequestUnit

	PluginList []*Plugin
)
View Source
var (
	// 消息发送者是否是管理员
	IsAdmin = func(e *Event, bInfo BotInfo) bool {
		for _, admin := range bInfo.Admins {
			if e.UserID == admin {
				return true
			}
		}
		return false
	}

	// 发送者是否是超级管理员
	IsSAdmin = func(e *Event, bInfo BotInfo) bool {
		for _, admin := range Conf.SAdmins {
			if e.UserID == admin {
				return true
			}
		}
		return false
	}

	IsGroupMsg = func(e *Event, bInfo BotInfo) bool {
		return e.MessageType == MsgTypeGroup
	}

	IsPrivateMsg = func(e *Event, bInfo BotInfo) bool {
		return e.MessageType == MsgTypePrivate
	}
)
View Source
var (
	LBLogger logrus.Logger
)
View Source
var SegRegex = regexp.MustCompile(`\[CQ:[a-z]{2,9}[^\]]*\]`)

Functions

func AddEchoCallback

func AddEchoCallback(echo string, callback EchoCallback)

func Init

func Init(conf string)

func InitBackenPlugin

func InitBackenPlugin()

func InitBotCtxs

func InitBotCtxs()

func InitDefaultPluginManager

func InitDefaultPluginManager(plgId int)

func InitLogConf

func InitLogConf()

func InitPluginList

func InitPluginList()

func ParseTextMsg

func ParseTextMsg(segs []MsgSeg) string

func RunBackenPlugin

func RunBackenPlugin()

func RunBots

func RunBots()

func RunEventDispatcher

func RunEventDispatcher()

func RunRespDispatcher

func RunRespDispatcher(poolSize int)

func Start

func Start()

Types

type ApiPost

type ApiPost struct {
	Action string      `json:"action"`
	Params interface{} `json:"params"`
	Echo   string      `json:"echo"`
}

Params: ptr

func MakeGroupMsg

func MakeGroupMsg(msgBuilder MsgBuilder, groupID int64) ApiPost

func MakePrivateMsg

func MakePrivateMsg(msgBuilder MsgBuilder, userID int64) ApiPost

func (ApiPost) Do

func (api ApiPost) Do(botID int64, needEcho bool) (string, error)

type ApiResp

type ApiResp struct {
	Data struct {
		MessageID int `json:"message_id"`
	} `json:"data"`
	Echo    string `jons:"echo"`
	Retcode int    `json:"retcode"`
	Status  string `json:"status"`
}

type ArrayMsg

type ArrayMsg struct {
	Segs []MsgSeg
	Len  int
}

func MakeArrayMsg

func MakeArrayMsg(size int) *ArrayMsg

func (*ArrayMsg) AddAt

func (am *ArrayMsg) AddAt(uid int64) *ArrayMsg

func (*ArrayMsg) AddDice

func (am *ArrayMsg) AddDice() *ArrayMsg

func (*ArrayMsg) AddFace

func (am *ArrayMsg) AddFace(faceID int) *ArrayMsg

func (*ArrayMsg) AddImg

func (am *ArrayMsg) AddImg(file, url string) *ArrayMsg

func (*ArrayMsg) AddRPS

func (am *ArrayMsg) AddRPS() *ArrayMsg

func (*ArrayMsg) AddRecord

func (am *ArrayMsg) AddRecord(file, url string) *ArrayMsg

func (*ArrayMsg) AddShake

func (am *ArrayMsg) AddShake() *ArrayMsg

func (*ArrayMsg) AddText

func (am *ArrayMsg) AddText(text string) *ArrayMsg

func (*ArrayMsg) GetMsg

func (am *ArrayMsg) GetMsg() (interface{}, error)

type BackenUnit

type BackenUnit struct {
	Plg   *Plugin
	Init  func()
	Start func(bInfos []BotInfo)
}

func (*BackenUnit) AddToBackenChain

func (bp *BackenUnit) AddToBackenChain()

func (*BackenUnit) SetInitFunc

func (bp *BackenUnit) SetInitFunc(f func()) *BackenUnit

func (*BackenUnit) SetStartFunc

func (bp *BackenUnit) SetStartFunc(f func(bInfos []BotInfo)) *BackenUnit

type BeforeApiOutHook

type BeforeApiOutHook func(apiPost *ApiPost, bInfo BotInfo) error

Before send an ApiPost to the CQ server, all of the BeforeApiOutHook will be called. if the hook return a not nil error, this ApiPost will be aborted

func MakeBeforeApiOutHook

func MakeBeforeApiOutHook(task func(apiPost *ApiPost, bInfo BotInfo) error) BeforeApiOutHook

func (BeforeApiOutHook) AddToHookChain

func (hook BeforeApiOutHook) AddToHookChain()

type BotContext

type BotContext struct {
	Conn      *ws.Conn
	CloseLock *sync.Mutex
	OutChan   chan ApiPost
	FlagChan  chan byte
	CloseChan chan byte
	IsReady   bool
	BotInfo   *BotInfo
}

type BotCtxs

type BotCtxs = []BotContext

type BotInfo

type BotInfo struct {
	BotID       int64   `yaml:"id"`
	Host        string  `yaml:"host"`
	Port        int     `yaml:"port"`
	Name        string  `yaml:"name"`
	Token       string  `yaml:"access-token"`
	Timeout     int     `yaml:"time-out"`
	Admins      []int64 `yaml:"admins"`
	MessageType string  `yaml:"message-type"`
}

type CommandUnit

type CommandUnit struct {
	Plg     *Plugin
	Rule    *Rule
	Cmd     string
	Aliases []string
	Process func(e *Event, params []string, bInfo BotInfo)
}

func (*CommandUnit) AddAliases

func (cp *CommandUnit) AddAliases(aliases ...string) *CommandUnit

func (*CommandUnit) AddToCmdChain

func (cp *CommandUnit) AddToCmdChain()

func (*CommandUnit) SetCommand

func (cp *CommandUnit) SetCommand(cmd string) *CommandUnit

func (*CommandUnit) SetProcessor

func (cp *CommandUnit) SetProcessor(f func(e *Event, params []string, bInfo BotInfo)) *CommandUnit

func (*CommandUnit) SetRule

func (cp *CommandUnit) SetRule(rule *Rule) *CommandUnit

type Config

type Config struct {
	BotInfos         []BotInfo `yaml:"bots"`
	LogConf          LogConf   `yaml:"log"`
	SAdmins          []int64   `yaml:"s-admin"`
	CallbackPoolSize int       `yaml:"callback-pool-size"`
}
var (
	Conf Config
)

type DisconnectHook

type DisconnectHook func(bInfo BotInfo)

When a connect is closed, this hook will be called.

func MakeDisconnectHook

func MakeDisconnectHook(task func(bInfo BotInfo)) DisconnectHook

func (DisconnectHook) AddToHookChain

func (hook DisconnectHook) AddToHookChain()

type EchoCallback

type EchoCallback func(apiResp *ApiResp, bInfo BotInfo)

type Event

type Event struct {
	Font          int         `json:"font"`
	GroupID       int64       `json:"group_id"`
	Message       interface{} `json:"message"`
	MessageID     int         `json:"message_id"`
	MessageSeq    int         `json:"message_seq"`
	MessageType   string      `json:"message_type"`
	PostType      string      `json:"post_type"`
	RawMessage    string      `json:"raw_message"`
	TempSource    int         `json:"temp_source"`
	SelfID        int64       `json:"self_id"`
	Sender        Sender      `json:"sender"`
	SubType       string      `json:"sub_type"`
	Time          int         `json:"time"`
	UserID        int64       `json:"user_id"`
	MetaEventType string      `json:"meta_event_type"`
}

func (*Event) GetArrayMsg

func (e *Event) GetArrayMsg() []MsgSeg

func (*Event) GetTextMsg

func (e *Event) GetTextMsg() string

type EventInHook

type EventInHook func(e *Event, bInfo BotInfo) error

When an event is received from CQ server, all of the EventInHook will be called. If the hook return a not nil error, the event will be aborted.

func MakeEventInHook

func MakeEventInHook(task func(e *Event, bInfo BotInfo) error) EventInHook

func (EventInHook) AddToHookChain

func (hook EventInHook) AddToHookChain()

type Formatter

type Formatter struct {
	TimestampFormat string
	LineFormat      string
}

*

  • @description: logrus formatter
  • %msg% - messsage, %lvl% - level, %time% - time, %fn% - filename, %fln% - file line number

func (*Formatter) Format

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)

type GroupMsg

type GroupMsg struct {
	Message    interface{} `json:"message"`
	AutoEscape bool        `json:"auto_escape"`
	GroupID    int64       `json:"group_id"`
}

type Judge

type Judge = func(e *Event, bInfo BotInfo) bool

type LogConf

type LogConf struct {
	Level    string `yaml:"level"`
	MaxFiles uint   `yaml:"max-files"`
}

type MessageUnit

type MessageUnit struct {
	Rule    *Rule
	Plg     *Plugin
	Process func(e *Event, bInfo BotInfo)
}

func (*MessageUnit) AddToMsgChain

func (mp *MessageUnit) AddToMsgChain()

func (*MessageUnit) SetProcessor

func (mp *MessageUnit) SetProcessor(f func(e *Event, bInfo BotInfo)) *MessageUnit

func (*MessageUnit) SetRule

func (mp *MessageUnit) SetRule(rule *Rule) *MessageUnit

type MsgBuilder

type MsgBuilder interface {
	GetMsg() (interface{}, error)
}

type MsgSeg

type MsgSeg struct {
	Type string            `json:"type"`
	Data map[string]string `json:"data"`
}

func ParseMsgSegs

func ParseMsgSegs(msg string) []MsgSeg

type NoticeUnit

type NoticeUnit struct {
	Rule    *Rule
	Plg     *Plugin
	Process func(e *Event, bInfo BotInfo)
}

func (*NoticeUnit) AddToNoticeChain

func (np *NoticeUnit) AddToNoticeChain()

func (*NoticeUnit) SetProcessor

func (np *NoticeUnit) SetProcessor(f func(e *Event, bInfo BotInfo)) *NoticeUnit

func (*NoticeUnit) SetRule

func (np *NoticeUnit) SetRule(rule *Rule) *NoticeUnit

type OnconnectHook

type OnconnectHook func(bInfo BotInfo) error

When a bot instance onconnect to a CQ server, all of the OnconnectHook will be called. If the hook return an error which is not nil, this connect will be closed, and then, the Disconnecthooks will be called.

func MakeOnconnectHook

func MakeOnconnectHook(task func(bInfo BotInfo) error) OnconnectHook

func (OnconnectHook) AddToHookChain

func (hook OnconnectHook) AddToHookChain()

type Plugin

type Plugin struct {
	ID            int
	Name          string
	Enable        bool
	HelpInfo      string
	IsAdminPlugin bool
}

func NewPlugin

func NewPlugin(id int) *Plugin

func (*Plugin) AddBackenUnit

func (p *Plugin) AddBackenUnit() *BackenUnit

func (*Plugin) AddCommandUnit

func (p *Plugin) AddCommandUnit() *CommandUnit

func (*Plugin) AddMessageUnit

func (p *Plugin) AddMessageUnit() *MessageUnit

func (*Plugin) AddNoticeUnit

func (p *Plugin) AddNoticeUnit() *NoticeUnit

func (*Plugin) AddRequestUnit

func (p *Plugin) AddRequestUnit() *RequestUnit

func (*Plugin) SetAdminPlugin

func (p *Plugin) SetAdminPlugin() *Plugin

func (*Plugin) SetHelpInfo

func (p *Plugin) SetHelpInfo(helpInfo string) *Plugin

func (*Plugin) SetName

func (p *Plugin) SetName(name string) *Plugin

type PrivateMsg

type PrivateMsg struct {
	Message    interface{} `json:"message"`
	AutoEscape bool        `json:"auto_escape"`
	UserID     int64       `json:"user_id"`
	GroupID    int64       `json:"group_id"`
}

type RequestUnit

type RequestUnit struct {
	Rule    *Rule
	Plg     *Plugin
	Process func(e *Event, bInfo BotInfo)
}

func (*RequestUnit) AddToRequestChain

func (rp *RequestUnit) AddToRequestChain()

func (*RequestUnit) SetProcessor

func (rp *RequestUnit) SetProcessor(f func(e *Event, bInfo BotInfo)) *RequestUnit

func (*RequestUnit) SetRule

func (rp *RequestUnit) SetRule(rule *Rule) *RequestUnit

type Rule

type Rule struct {
	Must []Judge
	Or   []Judge
}

func NewRule

func NewRule() *Rule

func (*Rule) AddMustRules

func (r *Rule) AddMustRules(rs ...Judge) *Rule

func (*Rule) AddOrRules

func (r *Rule) AddOrRules(rs ...Judge) *Rule

func (*Rule) CheckRules

func (r *Rule) CheckRules(e *Event, bInfo BotInfo) bool

*

  • @description: All in *Must* is match and (One of *Or* is math or len(Or) == 0)
  • @param *Event e
  • @param *BotContext bInfo
  • @return result bool

type Sender

type Sender struct {
	Age      int    `json:"age"`
	Area     string `json:"area"`
	Card     string `json:"card"`
	Level    string `json:"level"`
	Nickname string `json:"nickname"`
	Role     string `json:"role"`
	Sex      string `json:"sex"`
	Title    string `json:"title"`
	UserID   int64  `json:"user_id"`
}

type TextMsg

type TextMsg struct {
	// contains filtered or unexported fields
}

func MakeTextMsg

func MakeTextMsg() *TextMsg

func (*TextMsg) AddAt

func (tm *TextMsg) AddAt(uid int64) *TextMsg

func (*TextMsg) AddDice

func (tm *TextMsg) AddDice() *TextMsg

func (*TextMsg) AddFace

func (tm *TextMsg) AddFace(faceNo int) *TextMsg

func (*TextMsg) AddImg

func (tm *TextMsg) AddImg(file, url string) *TextMsg

func (*TextMsg) AddRPS

func (tm *TextMsg) AddRPS() *TextMsg

func (*TextMsg) AddRecord

func (tm *TextMsg) AddRecord(file, url string) *TextMsg

func (*TextMsg) AddShake

func (tm *TextMsg) AddShake() *TextMsg

func (*TextMsg) AddText

func (tm *TextMsg) AddText(text string) *TextMsg

func (*TextMsg) GetMsg

func (tm *TextMsg) GetMsg() (interface{}, error)

func (*TextMsg) NewLine

func (tm *TextMsg) NewLine() *TextMsg

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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