Skip to content

Commit c5f8101

Browse files
committed
feat: Add postArtworkCmd to handle artwork posting
1 parent 9d7e864 commit c5f8101

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

bot/bot.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ func RunPolling() {
4444
baseGroup.HandleMessageCtx(getPictureFile, telegohandler.Or(telegohandler.CommandEqual("file"), telegohandler.CommandEqual("files")), mentionIsBot)
4545
baseGroup.HandleMessageCtx(randomPicture, telegohandler.Or(telegohandler.CommandEqual("setu"), telegohandler.CommandEqual("random")), mentionIsBot)
4646
baseGroup.HandleMessageCtx(help, telegohandler.CommandEqual("help"), mentionIsBot)
47-
baseGroup.HandleMessageCtx(getArtworkInfo, sourceUrlMatches)
4847
baseGroup.HandleMessageCtx(searchPicture, telegohandler.CommandEqual("search"), mentionIsBot)
4948
baseGroup.HandleMessageCtx(setAdmin, telegohandler.CommandEqual("set_admin"))
5049
baseGroup.HandleMessageCtx(deletePicture, telegohandler.Or(telegohandler.CommandEqual("del"), telegohandler.CommandEqual("delete")))
5150
baseGroup.HandleMessageCtx(fetchArtwork, telegohandler.CommandEqual("fetch"))
5251
baseGroup.HandleMessageCtx(processPictures, telegohandler.CommandEqual("process_pictures"))
5352
baseGroup.HandleMessageCtx(setArtworkR18, telegohandler.CommandEqual("r18"))
5453
baseGroup.HandleMessageCtx(setArtworkTags, telegohandler.Or(telegohandler.CommandEqual("tags"), telegohandler.CommandEqual("addtags"), telegohandler.CommandEqual("deltags")))
54+
baseGroup.HandleMessageCtx(postArtworkCmd, telegohandler.CommandEqual("post"))
5555
baseGroup.HandleMessageCtx(batchPostArtwork, telegohandler.CommandEqual("batch_post"))
56-
57-
baseGroup.HandleCallbackQueryCtx(postArtwork, telegohandler.CallbackDataContains("post_artwork"))
56+
baseGroup.HandleCallbackQueryCtx(postArtworkCb, telegohandler.CallbackDataContains("post_artwork"))
57+
baseGroup.HandleMessageCtx(getArtworkInfo, sourceUrlMatches)
5858
baseGroup.HandleInlineQueryCtx(inlineQuery)
5959

6060
botHandler.Start()

bot/handlers_admin.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func fetchArtwork(ctx context.Context, bot *telego.Bot, message telego.Message)
204204
telegram.ReplyMessage(bot, message, "开始拉取作品了")
205205
}
206206

207-
func postArtwork(ctx context.Context, bot *telego.Bot, query telego.CallbackQuery) {
207+
func postArtworkCb(ctx context.Context, bot *telego.Bot, query telego.CallbackQuery) {
208208
if !service.CheckAdminPermission(ctx, query.From.ID, types.PermissionPostArtwork) &&
209209
!service.CheckAdminPermission(ctx, query.Message.GetChat().ID, types.PermissionPostArtwork) {
210210
bot.AnswerCallbackQuery(&telego.AnswerCallbackQueryParams{
@@ -316,6 +316,71 @@ func postArtwork(ctx context.Context, bot *telego.Bot, query telego.CallbackQuer
316316
})
317317
}
318318

319+
func postArtworkCmd(ctx context.Context, bot *telego.Bot, message telego.Message) {
320+
if !CheckPermissionInGroup(ctx, message, types.PermissionPostArtwork) {
321+
telegram.ReplyMessage(bot, message, "你没有发布作品的权限")
322+
return
323+
}
324+
_, _, args := telegoutil.ParseCommand(message.Text)
325+
if len(args) == 0 && message.ReplyToMessage == nil {
326+
telegram.ReplyMessage(bot, message, "请提供作品链接, 或回复一条消息")
327+
return
328+
}
329+
var sourceURL string
330+
if message.ReplyToMessage != nil {
331+
sourceURL = FindSourceURLForMessage(message.ReplyToMessage)
332+
if sourceURL == "" {
333+
if len(args) == 0 {
334+
telegram.ReplyMessage(bot, message, "不支持的链接")
335+
return
336+
}
337+
sourceURL = sources.FindSourceURL(args[0])
338+
}
339+
}
340+
if len(args) > 0 {
341+
sourceURL = sources.FindSourceURL(args[0])
342+
}
343+
if sourceURL == "" {
344+
telegram.ReplyMessage(bot, message, "不支持的链接")
345+
return
346+
}
347+
artwork, _ := service.GetArtworkByURL(ctx, sourceURL)
348+
if artwork != nil {
349+
telegram.ReplyMessage(bot, message, "作品已存在")
350+
return
351+
}
352+
msg, err := telegram.ReplyMessage(bot, message, "正在发布...")
353+
if err == nil && msg != nil {
354+
defer bot.DeleteMessage(telegoutil.Delete(msg.Chat.ChatID(), msg.MessageID))
355+
}
356+
cachedArtwork, err := service.GetCachedArtworkByURL(ctx, sourceURL)
357+
if err != nil {
358+
artwork, err = sources.GetArtworkInfo(sourceURL)
359+
if err != nil {
360+
Logger.Errorf("获取作品信息失败: %s", err)
361+
telegram.ReplyMessage(bot, message, "获取作品信息失败: "+err.Error())
362+
return
363+
}
364+
} else {
365+
artwork = cachedArtwork.Artwork
366+
}
367+
if err := fetcher.PostAndCreateArtwork(ctx, artwork, bot, storage.GetStorage(), message.Chat.ID); err != nil {
368+
telegram.ReplyMessage(bot, message, "发布失败: "+err.Error())
369+
return
370+
}
371+
artwork, err = service.GetArtworkByURL(ctx, sourceURL)
372+
if err != nil {
373+
telegram.ReplyMessage(bot, message, "获取发布后的作品信息失败: "+err.Error())
374+
return
375+
}
376+
bot.SendMessage(telegoutil.Message(telegoutil.ID(message.Chat.ID), "发布成功: "+artwork.Title).
377+
WithReplyParameters(&telego.ReplyParameters{
378+
ChatID: message.Chat.ChatID(),
379+
MessageID: message.MessageID,
380+
},
381+
).WithReplyMarkup(telegram.GetPostedPictureReplyMarkup(artwork.Pictures[0])))
382+
}
383+
319384
func processPictures(ctx context.Context, bot *telego.Bot, message telego.Message) {
320385
userAdmin, err := service.GetAdminByUserID(ctx, message.From.ID)
321386
if err != nil {

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version string = "0.8.0"
10+
Version string = "0.8.1"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

common/os.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
. "ManyACG/logger"
1010
)
1111

12+
// 创建文件, 自动创建目录
1213
func MkFile(path string, data []byte) error {
1314
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
1415
if err != nil {
@@ -17,6 +18,11 @@ func MkFile(path string, data []byte) error {
1718
return os.WriteFile(path, data, os.ModePerm)
1819
}
1920

21+
func FileExists(path string) bool {
22+
_, err := os.Stat(path)
23+
return err == nil
24+
}
25+
2026
// 删除文件, 并清理空目录. 如果文件不存在则返回 nil
2127
func PurgeFile(path string) error {
2228
if err := os.Remove(path); err != nil {

telegram/telegram.go

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ var (
8484
Command: "fetch",
8585
Description: "开始一次拉取",
8686
},
87+
{
88+
Command: "post",
89+
Description: "发布作品 <url>",
90+
},
8791
{
8892
Command: "process_pictures",
8993
Description: "处理无哈希的图片",

0 commit comments

Comments
 (0)