mirror of
https://github.com/awfufu/qbot.git
synced 2026-03-01 13:29:43 +08:00
refactor: tidy qbot.Segment structure
This commit is contained in:
44
apis.go
44
apis.go
@@ -60,49 +60,49 @@ func (b *Bot) SendPrivateReplyMsg(userID uint64, msgID uint64, message ...any) (
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateText(userID uint64, message string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Text(message))}, true)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{textSegment(message)}, true)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateJson(userID uint64, data string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Json(data))}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{jsonSegment(data)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateVoice(userID uint64, file string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Record(file))}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{recordSegment(file)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateVideo(userID uint64, file string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Video(file))}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{videoSegment(file)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateMusic(userID uint64, typeStr, id string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Music(typeStr, id))}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{musicSegment(typeStr, id)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateCustomMusic(userID uint64, url, audio, title, content, image string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(CustomMusic(url, audio, title, content, image))}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{customMusicSegment(url, audio, title, content, image)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateDice(userID uint64) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Dice())}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{diceSegment()}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateRps(userID uint64) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(Rps())}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{rpsSegment()}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateFile(userID uint64, file string) (uint64, error) {
|
||||
return api.SendPrivateMsg(b, userID, []api.Segment{api.Segment(File(file))}, false)
|
||||
return api.SendPrivateMsg(b, userID, []Segment{fileSegment(file)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendPrivateForward(userID uint64, block ForwardBlock) (int32, string, error) {
|
||||
var messages []api.Segment
|
||||
var messages []Segment
|
||||
for _, item := range block.Content {
|
||||
content := make([]any, len(item.Content))
|
||||
for i, s := range item.Content {
|
||||
content[i] = s
|
||||
}
|
||||
messages = append(messages, api.Segment(CustomNode(item.Name, item.UserID, content...)))
|
||||
messages = append(messages, Segment(customNodeSegment(item.Name, item.UserID, content...)))
|
||||
}
|
||||
|
||||
var news []api.News
|
||||
@@ -133,49 +133,49 @@ func (b *Bot) SendGroupReplyMsg(groupID uint64, msgID uint64, message ...any) (u
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupText(groupID uint64, message string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Text(message))}, true)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{textSegment(message)}, true)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupJson(groupID uint64, data string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Json(data))}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{jsonSegment(data)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupVoice(groupID uint64, file string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Record(file))}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{recordSegment(file)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupVideo(groupID uint64, file string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Video(file))}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{videoSegment(file)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupMusic(groupID uint64, typeStr, id string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Music(typeStr, id))}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{musicSegment(typeStr, id)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupCustomMusic(groupID uint64, url, audio, title, content, image string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(CustomMusic(url, audio, title, content, image))}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{customMusicSegment(url, audio, title, content, image)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupDice(groupID uint64) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Dice())}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{diceSegment()}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupRps(groupID uint64) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(Rps())}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{rpsSegment()}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupFile(groupID uint64, file string) (uint64, error) {
|
||||
return api.SendGroupMsg(b, groupID, []api.Segment{api.Segment(File(file))}, false)
|
||||
return api.SendGroupMsg(b, groupID, []Segment{fileSegment(file)}, false)
|
||||
}
|
||||
|
||||
func (b *Bot) SendGroupForward(groupID uint64, block ForwardBlock) (int32, string, error) {
|
||||
var messages []api.Segment
|
||||
var messages []Segment
|
||||
for _, item := range block.Content {
|
||||
content := make([]any, len(item.Content))
|
||||
for i, s := range item.Content {
|
||||
content[i] = s
|
||||
}
|
||||
messages = append(messages, api.Segment(CustomNode(item.Name, item.UserID, content...)))
|
||||
messages = append(messages, Segment(customNodeSegment(item.Name, item.UserID, content...)))
|
||||
}
|
||||
|
||||
var news []api.News
|
||||
|
||||
93
segment.go
93
segment.go
@@ -6,11 +6,29 @@ import (
|
||||
"github.com/awfufu/qbot/api"
|
||||
)
|
||||
|
||||
// Segment represents a message segment.
|
||||
type Segment api.Segment
|
||||
type Segment = api.Segment
|
||||
|
||||
// creates a text segment
|
||||
// create text segment
|
||||
func Text(text string) Segment {
|
||||
return textSegment(text)
|
||||
}
|
||||
|
||||
// create at segment
|
||||
func At(userID uint64) Segment {
|
||||
return atSegment(userID)
|
||||
}
|
||||
|
||||
// create face segment
|
||||
func Face(id uint64) Segment {
|
||||
return faceSegment(id)
|
||||
}
|
||||
|
||||
// create image segment
|
||||
func Image(file string, summary ...string) Segment {
|
||||
return imageSegment(file, summary...)
|
||||
}
|
||||
|
||||
func textSegment(text string) Segment {
|
||||
return Segment{
|
||||
Type: "text",
|
||||
Data: map[string]any{
|
||||
@@ -19,8 +37,7 @@ func Text(text string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates an at segment
|
||||
func At(userID uint64) Segment {
|
||||
func atSegment(userID uint64) Segment {
|
||||
return Segment{
|
||||
Type: "at",
|
||||
Data: map[string]any{
|
||||
@@ -29,8 +46,7 @@ func At(userID uint64) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a face segment
|
||||
func Face(id uint64) Segment {
|
||||
func faceSegment(id uint64) Segment {
|
||||
return Segment{
|
||||
Type: "face",
|
||||
Data: map[string]any{
|
||||
@@ -39,10 +55,7 @@ func Face(id uint64) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates an image segment
|
||||
// file can be a local path (file:///path/to/file), a URL (https://example.com/image), or base64;
|
||||
// summary is optional.
|
||||
func Image(file string, summary ...string) Segment {
|
||||
func imageSegment(file string, summary ...string) Segment {
|
||||
data := map[string]any{
|
||||
"file": file,
|
||||
}
|
||||
@@ -55,8 +68,7 @@ func Image(file string, summary ...string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// Json creates a json segment
|
||||
func Json(data string) Segment {
|
||||
func jsonSegment(data string) Segment {
|
||||
return Segment{
|
||||
Type: "json",
|
||||
Data: map[string]any{
|
||||
@@ -65,8 +77,7 @@ func Json(data string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a record segment
|
||||
func Record(file string) Segment {
|
||||
func recordSegment(file string) Segment {
|
||||
return Segment{
|
||||
Type: "record",
|
||||
Data: map[string]any{
|
||||
@@ -75,8 +86,7 @@ func Record(file string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a video segment
|
||||
func Video(file string) Segment {
|
||||
func videoSegment(file string) Segment {
|
||||
return Segment{
|
||||
Type: "video",
|
||||
Data: map[string]any{
|
||||
@@ -85,8 +95,7 @@ func Video(file string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a music segment
|
||||
func Music(typeStr, id string) Segment {
|
||||
func musicSegment(typeStr, id string) Segment {
|
||||
return Segment{
|
||||
Type: "music",
|
||||
Data: map[string]any{
|
||||
@@ -96,8 +105,7 @@ func Music(typeStr, id string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a custom music segment
|
||||
func CustomMusic(url, audio, title, content, image string) Segment {
|
||||
func customMusicSegment(url, audio, title, content, image string) Segment {
|
||||
return Segment{
|
||||
Type: "music",
|
||||
Data: map[string]any{
|
||||
@@ -111,24 +119,21 @@ func CustomMusic(url, audio, title, content, image string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a dice segment
|
||||
func Dice() Segment {
|
||||
func diceSegment() Segment {
|
||||
return Segment{
|
||||
Type: "dice",
|
||||
Data: map[string]any{},
|
||||
}
|
||||
}
|
||||
|
||||
// creates a rps segment
|
||||
func Rps() Segment {
|
||||
func rpsSegment() Segment {
|
||||
return Segment{
|
||||
Type: "rps",
|
||||
Data: map[string]any{},
|
||||
}
|
||||
}
|
||||
|
||||
// creates a file segment
|
||||
func File(file string) Segment {
|
||||
func fileSegment(file string) Segment {
|
||||
return Segment{
|
||||
Type: "file",
|
||||
Data: map[string]any{
|
||||
@@ -137,8 +142,7 @@ func File(file string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// Node creates a forward message node using message ID.
|
||||
func Node(id string) Segment {
|
||||
func nodeSegment(id string) Segment {
|
||||
return Segment{
|
||||
Type: "node",
|
||||
Data: map[string]any{
|
||||
@@ -147,8 +151,7 @@ func Node(id string) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// CustomNode creates a custom forward message node.
|
||||
func CustomNode(name string, uin uint64, content ...any) Segment {
|
||||
func customNodeSegment(name string, uin uint64, content ...any) Segment {
|
||||
return Segment{
|
||||
Type: "node",
|
||||
Data: map[string]any{
|
||||
@@ -159,7 +162,6 @@ func CustomNode(name string, uin uint64, content ...any) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// creates a reply segment
|
||||
func replySegment(msgID uint64) Segment {
|
||||
return Segment{
|
||||
Type: "reply",
|
||||
@@ -169,7 +171,6 @@ func replySegment(msgID uint64) Segment {
|
||||
}
|
||||
}
|
||||
|
||||
// converts variadic arguments to a message ([]api.Segment)
|
||||
func ToMessage(args ...any) []api.Segment {
|
||||
if len(args) == 0 {
|
||||
return []api.Segment{}
|
||||
@@ -179,31 +180,22 @@ func ToMessage(args ...any) []api.Segment {
|
||||
for _, arg := range args {
|
||||
switch v := arg.(type) {
|
||||
case Segment:
|
||||
segments = append(segments, api.Segment(v))
|
||||
case api.Segment:
|
||||
segments = append(segments, v)
|
||||
case string:
|
||||
segments = append(segments, api.Segment(Text(v)))
|
||||
segments = append(segments, textSegment(v))
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
|
||||
segments = append(segments, api.Segment(Text(fmt.Sprint(v))))
|
||||
segments = append(segments, textSegment(fmt.Sprint(v)))
|
||||
case fmt.Stringer:
|
||||
segments = append(segments, api.Segment(Text(v.String())))
|
||||
segments = append(segments, textSegment(v.String()))
|
||||
default:
|
||||
// Try to convert unknown types to string representation
|
||||
segments = append(segments, api.Segment(Text(fmt.Sprintf("%v", v))))
|
||||
segments = append(segments, textSegment(fmt.Sprintf("%v", v)))
|
||||
}
|
||||
}
|
||||
return segments
|
||||
}
|
||||
|
||||
// ForwardBlockItem represents a single forward message node.
|
||||
type ForwardBlockItem struct {
|
||||
Name string `json:"nickname"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
Content []Segment `json:"content"`
|
||||
}
|
||||
|
||||
// ForwardBlock represents a block of forward messages with metadata.
|
||||
// represents a block of forward messages with metadata.
|
||||
type ForwardBlock struct {
|
||||
Title string `json:"source"`
|
||||
Preview string `json:"preview"`
|
||||
@@ -211,3 +203,10 @@ type ForwardBlock struct {
|
||||
Prompt string `json:"prompt"`
|
||||
Content []ForwardBlockItem `json:"messages"`
|
||||
}
|
||||
|
||||
// represents a single forward message node.
|
||||
type ForwardBlockItem struct {
|
||||
Name string `json:"nickname"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
Content []Segment `json:"content"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user