~mendelmaleh/tg_mcstatusbot

tg_mcstatusbot/commands.go -rw-r--r-- 2.4 KiB
9f359caeMendel E Use akita, use go.mod, move commands to their own functions in commands.go 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main

import (
	"fmt"
	"strings"

	"git.sr.ht/~mendelmaleh/akita"
	"git.sr.ht/~mendelmaleh/mcstatus"

	"github.com/bobesa/go-domain-util/domainutil"
	"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

// TODO: user facing errors

func cmdStatus(cl Client, msg *tgbotapi.Message) (tgbotapi.Chattable, error) {
	db, mc := cl.Bitcask, cl.MCServerStatus

	chatid := int64toBytes(msg.Chat.ID)
	q := msg.CommandArguments()
	if q == "" && !db.Has(chatid) {
		return nil, nil // no args and no default
	}

	if q == "" {
		val, err := db.Get(chatid)
		if err != nil {
			return nil, err
		}

		q = string(val)
	} else {
		q = strings.Fields(q)[0]

		if msg.Chat.IsPrivate() {
			if err := db.Put(chatid, []byte(q)); err != nil {
				return nil, err
			}
		}
	}

	status, err := mc.GetStatus(q)
	if err != nil {
		return nil, err
	}

	return akita.NewMessage(msg, mcStatusString(q, status)), nil
}

func cmdDefault(cl Client, msg *tgbotapi.Message) (tgbotapi.Chattable, error) {
	bot, db := cl.BotAPI, cl.Bitcask

	if msg.Chat.IsPrivate() {
		return nil, nil // private chats can't set defaults
	}

	cm, err := bot.GetChatMember(tgbotapi.GetChatMemberConfig{tgbotapi.ChatConfigWithUser{
		ChatID: msg.Chat.ID,
		UserID: msg.From.ID,
	}})
	if err != nil {
		return nil, err
	}

	if !cm.IsAdministrator() && !cm.IsCreator() {
		return nil, err
	}

	args := strings.Fields(msg.CommandArguments())
	if len(args) < 1 || len(args[0]) < 1 {
		return nil, nil // no args
	}

	q, err := args[0], nil
	if q == "clear" {
		err = db.Delete(int64toBytes(msg.Chat.ID))
	} else {
		err = db.Put(int64toBytes(msg.Chat.ID), []byte(q))
	}

	return nil, err
}

func inlineQuery(cl Client, q *tgbotapi.InlineQuery) (tgbotapi.Chattable, error) {
	if dmn := domainutil.Domain(q.Query); dmn == "" {
		return nil, nil // ignore query
	}

	status, err := cl.MCServerStatus.GetStatus(q.Query)
	if err != nil {
		return nil, err
	}

	results := make([]interface{}, 1)
	results[0] = tgbotapi.InlineQueryResultArticle{
		Type:        "article", // must be
		ID:          q.ID,
		Title:       q.Query,
		Description: fmt.Sprintf("%s is %s", q.Query, mcStatusOnlineString(status)),
		ThumbURL:    mcstatus.IconURL(q.Query),
		InputMessageContent: tgbotapi.InputTextMessageContent{
			DisableWebPagePreview: true,
			ParseMode:             "html",
			Text:                  mcStatusString(q.Query, status),
		},
	}

	return tgbotapi.InlineConfig{
		InlineQueryID: q.ID,
		Results:       results,
	}, nil
}