mirai

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2020 License: AGPL-3.0 Imports: 12 Imported by: 0

README

mirai-go

基于mirai-api-http的golang sdk

项目目前仍在开发中,所有功能尚未经过测试且可能出现break change,非常不建议在生产环境中使用

只是个玩具啦

获取
go get github.com/wangnengjie/mirai-go

Quick Start

package main

import (
	"github.com/wangnengjie/mirai-go"
	"github.com/wangnengjie/mirai-go/model"
)

func main() {
	bot := mirai.NewBot(mirai.BotConfig{
		Host:      "127.0.0.1:8080",
		AuthKey:   "12345678",
		Id:        123456789,
		Websocket: true,
		RecvMode:  mirai.RecvAll,
		Debug:     true,
	})
	err := bot.Connect()
	if err != nil {
		bot.Log.Error(err)
	}
	bot.On(model.GroupMessage, repeat)
	bot.Loop()
}

func repeat(ctx *mirai.Context) { // 复读群消息
	m, _ := ctx.Message.(*model.GroupMsg)
	// 0 代表不回复消息,msgId是发出的消息的id
	// chain中第一位为source
	msgId, err := ctx.Bot.SendGroupMessage(m.Sender.Group.Id, m.MessageChain[1:], 0)
	// msgId 是刚刚发送的这条消息的id
	if err != nil {
		ctx.Bot.Log.Error(err)
	} else {
		ctx.Bot.Log.Info(msgId)
	}
}

Todos

  • 添加更多example
  • 添加更多调试信息(不清楚需要添加哪些,欢迎提建议)
  • 完善文档
  • 中间件功能
  • command接口
  • 测试
  • 性能优化?

go语言刚入门菜鸡,项目可能会出现各种问题,欢迎提issue

依赖

  • resty: Simple HTTP and REST client library for Go
  • websocket: A fast, well-tested and widely used WebSocket implementation for Go
  • jsoniter: A high-performance 100% compatible drop-in replacement of "encoding/json"
  • logrus: Structured, pluggable logging for Go
  • nested-logrus-formatter: Formatter for logrus

鸣谢

特别感谢mirai项目组mamoe

  • mirai:全开源 高效率 QQ机器人/Android QQ协议支持库 for JVM / Android
  • mirai-console:mirai 的高效率 QQ 机器人控制台
  • mirai-api-http:Mirai HTTP API (console) plugin

许可证

GNU AGPLv3,基于mirai的一系列项目均使用GNU AGPLv3开源许可证,使用时请遵守相关规则

Documentation

Overview

Context take example by repo gin-gonic/gin

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthResp

type AuthResp struct {
	Code    int    `json:"code"`
	Session string `json:"session"`
}

type Bot

type Bot struct {
	Mu   sync.RWMutex
	Log  *logrus.Entry
	Data sync.Map
	// contains filtered or unexported fields
}

func NewBot added in v0.2.0

func NewBot(config BotConfig) *Bot

func (*Bot) Auth added in v0.2.0

func (b *Bot) Auth() (string, error)

func (*Bot) BotInvitedJoinGroupReply

func (b *Bot) BotInvitedJoinGroupReply(operate int, msg string, req *model.BotInvitedJoinGroupRequest) error

响应Bot被邀请入群

0 同意邀请

1 拒绝邀请

func (*Bot) Connect added in v0.2.0

func (b *Bot) Connect() error

func (*Bot) CountMessage

func (b *Bot) CountMessage() (int, error)

获取bot接收并缓存的消息总数,注意不包含被删除的

func (*Bot) FetchLatestMessage

func (b *Bot) FetchLatestMessage(count int) ([]model.MsgRecv, error)

获取bot接收到的最新消息和最新各类事件(会从MiraiApiHttp消息记录中删除)

func (*Bot) FetchMessage

func (b *Bot) FetchMessage(count int) ([]model.MsgRecv, error)

获取bot接收到的最老消息和最老各类事件(会从MiraiApiHttp消息记录中删除)

func (*Bot) FriendList

func (b *Bot) FriendList() ([]model.User, error)

获取bot的好友列表

func (*Bot) GetConfig

func (b *Bot) GetConfig() (*Config, error)

func (*Bot) GetGroupConfig

func (b *Bot) GetGroupConfig(group model.GroupId) (*model.GroupConfig, error)

获取群设置

func (*Bot) GetMemberInfo

func (b *Bot) GetMemberInfo(group model.GroupId, qq model.QQId) (*model.MemberInfo, error)

获取群员资料

func (*Bot) GroupList

func (b *Bot) GroupList() ([]model.Group, error)

获取bot的群列表

func (*Bot) Id

func (b *Bot) Id() model.QQId

func (*Bot) Kick

func (b *Bot) Kick(group model.GroupId, qq model.QQId, msg string) error

移除指定群成员(需要有相关限权)

func (*Bot) Loop added in v0.2.0

func (b *Bot) Loop()

func (*Bot) MemberJoinReply

func (b *Bot) MemberJoinReply(operate int, msg string, req *model.MemberJoinRequest) error

响应用户入群申请(Bot需要有管理员权限)

0 同意入群

1 拒绝入群

2 忽略请求

3 拒绝入群并添加黑名单,不再接收该用户的入群申请

4 忽略入群并添加黑名单,不再接收该用户的入群申请

func (*Bot) MemberList

func (b *Bot) MemberList() ([]model.Member, error)

获取bot指定群种的成员列表

func (*Bot) MessageFromId

func (b *Bot) MessageFromId(msgId model.MsgId) (model.MsgRecv, error)

通过messageId获取一条被缓存的消息。当该messageId没有被缓存或缓存失效时,返回code 5(指定对象不存在)

func (*Bot) Mute

func (b *Bot) Mute(group model.GroupId, qq model.QQId, time int) error

指定群禁言指定群员(需要有相关限权)

func (*Bot) MuteAll

func (b *Bot) MuteAll(group model.GroupId) error

令指定群进行全体禁言(需要有相关限权)

func (*Bot) NewFriendReply

func (b *Bot) NewFriendReply(operate int, msg string, req *model.NewFriendRequest) error

回复好友申请

0 同意添加好友

1 拒绝添加好友

2 拒绝添加好友并添加黑名单,不再接收该用户的好友申请

func (*Bot) On

func (b *Bot) On(t model.MsgRecvType, handlers ...HandlerFunc)

挂载消息和事件监听

func (*Bot) PeekLatestMessage

func (b *Bot) PeekLatestMessage(count int) ([]model.MsgRecv, error)

获取bot接收到的最新消息和最新各类事件(不会从MiraiApiHttp消息记录中删除)

func (*Bot) PeekMessage

func (b *Bot) PeekMessage(count int) ([]model.MsgRecv, error)

获取bot接收到的最老消息和最老各类事件(不会从MiraiApiHttp消息记录中删除)

func (*Bot) Quit

func (b *Bot) Quit(group model.GroupId) error

使Bot退出群聊

func (*Bot) Recall

func (b *Bot) Recall(msgId model.MsgId) error

撤回指定消息。对于bot发送的消息,有2分钟时间限制。对于撤回群聊中群员的消息,需要有相应权限

func (*Bot) Release added in v0.2.0

func (b *Bot) Release() error

使用此方式释放session及其相关资源(Bot不会被释放),不使用的Session应当被释放

长时间(30分钟)未使用的Session将自动释放,否则Session持续保存Bot收到的消息,将会导致内存泄露

开启websocket后将不会自动释放,请务必定期释放

func (*Bot) ReleaseAndReauth added in v0.2.0

func (b *Bot) ReleaseAndReauth() error

func (*Bot) SendFriendMessage

func (b *Bot) SendFriendMessage(qq model.QQId, mc model.MsgChain, quoteId model.MsgId) (model.MsgId, error)

向指定好友发送消息,quoteId不为0时使用回复

func (*Bot) SendGroupMessage

func (b *Bot) SendGroupMessage(group model.GroupId, mc model.MsgChain, quoteId model.MsgId) (model.MsgId, error)

向指定群发送消息,quoteId不为0时使用回复

func (*Bot) SendImageMessage

func (b *Bot) SendImageMessage(qq model.QQId, group model.GroupId, urls []string) ([]string, error)

向指定对象(群或好友)发送图片消息(通过url)

除非需要通过此手段获取imageId,否则不推荐使用该接口

当qq和group同时存在时,表示发送临时会话图片(默认为0)

func (*Bot) SendTempMessage

func (b *Bot) SendTempMessage(qq model.QQId, group model.GroupId, mc model.MsgChain, quoteId model.MsgId) (model.MsgId, error)

向临时会话对象发送消息,quoteId不为0时使用回复

func (*Bot) SessionKey added in v0.2.0

func (b *Bot) SessionKey() string

func (*Bot) SetConfig

func (b *Bot) SetConfig(cacheSize int, enableWebsocket bool) error

func (*Bot) SetGroupConfig

func (b *Bot) SetGroupConfig(group model.GroupId, config model.GroupConfig) error

修改群设置(需要有相关限权)

func (*Bot) SetMemberInfo

func (b *Bot) SetMemberInfo(group model.GroupId, qq model.QQId, info model.MemberInfo) error

修改群员资料(需要有相关限权)

func (*Bot) SetSessionKey added in v0.2.0

func (b *Bot) SetSessionKey(sessionKey string)

func (*Bot) Unmute

func (b *Bot) Unmute(group model.GroupId, qq model.QQId) error

指定群解除群成员禁言(需要有相关限权)

func (*Bot) UnmuteAll

func (b *Bot) UnmuteAll(group model.GroupId) error

令指定群解除全体禁言(需要有相关限权)

func (*Bot) UploadImage

func (b *Bot) UploadImage(t string, path string) (*ImgUploadResp, error)

上传图片文件至服务器并返回ImageId

func (*Bot) Use added in v0.2.0

func (b *Bot) Use(handlers ...HandlerFunc)

func (*Bot) Verify added in v0.2.0

func (b *Bot) Verify(session string) error

type BotConfig added in v0.2.0

type BotConfig struct {
	Host    string // http://Host[:Port?]
	AuthKey string // authkey for remote mirai client

	Id         model.QQId
	CacheSize  int
	Websocket  bool // 是否使用ws(推荐)
	RecvMode   Mode // M_EVENT, M_MESSAGE, M_EVENT|M_MESSAGE (仅对ws有效)
	FetchMount int  // 使用轮询时每次fetch的信息条数
	Debug      bool // 开启debug模式
	Strict     bool // 严格模式下保证按收到的消息的顺序处理消息 (可能造成性能下降)
}

type BotInvitedJoinGroupReply

type BotInvitedJoinGroupReply DefaultEventReply

type Config

type Config struct {
	CacheSize       int  `json:"cacheSize"`
	EnableWebsocket bool `json:"enableWebsocket"`
}

type Context added in v0.2.0

type Context struct {
	Bot     *Bot
	Message model.MsgRecv
	Data    sync.Map
	// contains filtered or unexported fields
}

func (*Context) Abort added in v0.2.0

func (ctx *Context) Abort()

func (*Context) IsAbort added in v0.2.0

func (ctx *Context) IsAbort() bool

func (*Context) Next added in v0.2.0

func (ctx *Context) Next()

type DefaultEventReply

type DefaultEventReply struct {
	SessionKey string        `json:"sessionKey"`
	EventId    model.EventId `json:"eventId"`
	FromId     model.QQId    `json:"fromId"`
	GroupId    model.GroupId `json:"groupId"`
	Operate    int           `json:"operate"`
	Message    string        `json:"message"`
}

type DefaultResp

type DefaultResp struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

type HandlerFunc added in v0.2.0

type HandlerFunc func(ctx *Context)

type HandlersChan added in v0.2.0

type HandlersChan []HandlerFunc

type ImgUploadResp

type ImgUploadResp struct {
	ImageId string `json:"imageId"`
	Url     string `json:"url"`
	Path    string `json:"path"`
}

type KickReq

type KickReq struct {
	SessionKey string        `json:"sessionKey"`
	Target     model.GroupId `json:"target"`
	MemberId   model.QQId    `json:"memberId"`
	Msg        string        `json:"msg,omitempty"`
}

type MemberJoinReply

type MemberJoinReply DefaultEventReply

type Mode added in v0.2.0

type Mode string
const (
	RecvEventOnly   Mode = "event"
	RecvMessageOnly Mode = "message"
	RecvAll         Mode = "all"
)

type MuteOrUnmuteReq

type MuteOrUnmuteReq struct {
	SessionKey string        `json:"sessionKey"`
	Target     model.GroupId `json:"target"`
	MemberId   model.QQId    `json:"memberId,omitempty"`
	Time       int           `json:"time,omitempty"`
}

type NewFriendReply

type NewFriendReply DefaultEventReply

type QuitReq

type QuitReq struct {
	SessionKey string        `json:"sessionKey"`
	Target     model.GroupId `json:"target"`
}

type RecallReq

type RecallReq struct {
	SessionKey string      `json:"sessionKey"`
	Target     model.MsgId `json:"target"`
}

type SendImgMsgReq

type SendImgMsgReq struct {
	SessionKey string        `json:"sessionKey"`
	Urls       []string      `json:"urls"`
	QQ         model.QQId    `json:"qq,omitempty"`
	Group      model.GroupId `json:"group,omitempty"`
}

type SendMsgReq

type SendMsgReq struct {
	SessionKey   string         `json:"sessionKey"`
	MessageChain model.MsgChain `json:"messageChain"`
	QQ           model.QQId     `json:"qq,omitempty"`
	Group        model.GroupId  `json:"group,omitempty"`
	Quote        model.MsgId    `json:"quote,omitempty"`
}

type SendMsgResp

type SendMsgResp struct {
	DefaultResp
	MessageId model.MsgId `json:"messageId"`
}

type SetGroupConfigReq

type SetGroupConfigReq struct {
	SessionKey string            `json:"sessionKey"`
	Target     model.GroupId     `json:"target"`
	Config     model.GroupConfig `json:"config"`
}

type SetMemberInfoReq

type SetMemberInfoReq struct {
	SessionKey string           `json:"sessionKey"`
	Target     model.GroupId    `json:"target"`
	MemberId   model.QQId       `json:"memberId"`
	Info       model.MemberInfo `json:"info"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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