various improvements
This commit is contained in:
parent
d5272262da
commit
1a6953900c
14 changed files with 71 additions and 49 deletions
1
bot.go
1
bot.go
|
@ -6,7 +6,6 @@ import (
|
|||
"watn3y/bloaterbot/commands"
|
||||
"watn3y/bloaterbot/commands/notify"
|
||||
"watn3y/bloaterbot/text"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
|
|
@ -2,25 +2,24 @@ package botIO
|
|||
|
||||
import (
|
||||
"watn3y/bloaterbot/config"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) {
|
||||
|
||||
b, err := tgbotapi.NewBotAPI(config.BotConfig.APIToken)
|
||||
bot, err := tgbotapi.NewBotAPI(config.BotConfig.APIToken)
|
||||
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("Failed to authorize bot")
|
||||
}
|
||||
|
||||
b.Debug = config.BotConfig.DebugMode
|
||||
bot.Debug = config.BotConfig.DebugMode
|
||||
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
updates := tgbotapi.NewUpdate(0)
|
||||
updates.Timeout = 60
|
||||
|
||||
log.Info().Int64("ID", b.Self.ID).Str("username", b.Self.UserName).Msg("Successfully authorized bot")
|
||||
log.Info().Int64("ID", bot.Self.ID).Str("username", bot.Self.UserName).Msg("Successfully authorized bot")
|
||||
|
||||
return b.GetUpdatesChan(u), b
|
||||
return bot.GetUpdatesChan(updates), bot
|
||||
}
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
package botIO
|
||||
|
||||
import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
func GetUserByID(userToGet tgbotapi.ChatConfigWithUser, bot *tgbotapi.BotAPI) (error bool, member tgbotapi.ChatMember) {
|
||||
|
||||
member, err := bot.GetChatMember(tgbotapi.GetChatMemberConfig{ChatConfigWithUser: userToGet})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Unable to get info for user")
|
||||
return true, member
|
||||
}
|
||||
log.Debug().Interface("member",member).Msg("Got Info for user")
|
||||
return false, member
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
//TODO logging
|
||||
|
||||
func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
cmd := strings.ToLower(update.Message.Command())
|
||||
|
|
|
@ -22,6 +22,8 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
//TODO logging
|
||||
|
||||
func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("starting download")
|
||||
|
@ -30,18 +32,17 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
|
||||
msg := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
|
||||
Text: "Downloading your media...",
|
||||
Text: "Downloading your YouTube Video...",
|
||||
}
|
||||
workingMessage := botIO.SendMessage(msg, bot)
|
||||
|
||||
if len(commandArgs) >= 1 && matchURL(commandArgs[0]) != "" {
|
||||
//TODO distinguish
|
||||
//service := matchURL(commandArgs[0])
|
||||
|
||||
} else {
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Str("args", update.Message.CommandArguments()).Msg("failed to download. empty arg")
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Str("args", update.Message.CommandArguments()).Msg("Failed to download YouTube Video. Empty args")
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
Text: "Please specify a valid YouTube URL to download something.",
|
||||
Text: "Please specify a valid YouTube URL.",
|
||||
}
|
||||
botIO.EditMessage(message, bot)
|
||||
return
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
//TODO logging
|
||||
func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("getting gaypoints")
|
||||
|
|
|
@ -11,7 +11,8 @@ var gpSelectUser *sql.Stmt
|
|||
var gpSelectChat *sql.Stmt
|
||||
|
||||
var gpSet *sql.Stmt
|
||||
|
||||
//TODO logging
|
||||
//TODO switch to SQL
|
||||
func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
|
||||
rows, err := gpSelectChat.Query(chatid)
|
||||
if err != nil {
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
//TODO logging
|
||||
|
||||
func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
log.Debug().Str("args", update.Message.CommandArguments()).Msg("parsing new reminder")
|
||||
|
|
|
@ -9,7 +9,8 @@ var notifySetReminder *sql.Stmt
|
|||
var notifyGetReminders *sql.Stmt
|
||||
var notifyGetReminderDetails *sql.Stmt
|
||||
var notifyDeleteReminder *sql.Stmt
|
||||
|
||||
//TODO logging
|
||||
//TODO switch to SQL
|
||||
func InitDB() {
|
||||
const dbPath string = "./bloater.db"
|
||||
log.Info().Str("dbpath", dbPath).Msg("init database")
|
||||
|
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"time"
|
||||
|
@ -11,7 +12,7 @@ type config struct {
|
|||
APIToken string
|
||||
DebugMode bool
|
||||
PrivilegedUsers []int64
|
||||
Shutup map[int64]time.Time //is always filled at startup, not present in config
|
||||
Shutup map[int64]time.Time //! is always filled at startup, not present in config
|
||||
Webserver struct {
|
||||
Port string
|
||||
}
|
||||
|
@ -39,7 +40,7 @@ func LoadConfig() {
|
|||
|
||||
configFile, err := os.ReadFile("config.toml")
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("rror opening config file")
|
||||
log.Panic().Err(err).Msg("error opening config file")
|
||||
}
|
||||
|
||||
_, err = toml.Decode(string(configFile), &BotConfig)
|
||||
|
@ -49,6 +50,10 @@ func LoadConfig() {
|
|||
|
||||
BotConfig.Shutup = make(map[int64]time.Time)
|
||||
|
||||
if !BotConfig.DebugMode {
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
}
|
||||
|
||||
log.Info().Msg("loaded config file")
|
||||
log.Debug().Interface("config", BotConfig).Msg("")
|
||||
|
||||
|
@ -97,8 +102,6 @@ func EncodeToml() {
|
|||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
f, err := os.Create("config.toml.example")
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("error encoding config file. Cannot open config.toml.example")
|
||||
|
@ -113,5 +116,4 @@ if err := f.Close(); err != nil {
|
|||
}
|
||||
|
||||
log.Info().Msg("Wrote example config to config.toml.example. Bye Bye")
|
||||
|
||||
}
|
13
main.go
13
main.go
|
@ -26,16 +26,25 @@ func main() {
|
|||
}
|
||||
|
||||
config.LoadConfig()
|
||||
|
||||
gaypoints.InitDB()
|
||||
|
||||
notify.InitDB()
|
||||
go webserver.RunWeb()
|
||||
|
||||
go webserver.RunServer()
|
||||
|
||||
bot()
|
||||
|
||||
}
|
||||
|
||||
func configureLogger() {
|
||||
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.DateTime}
|
||||
|
||||
log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger()
|
||||
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
||||
|
||||
//! note that we overwrite the loglevel after loading the config in config/config.go:53. This is just the default
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
|
||||
log.Info().Msg("Started zerolog logger")
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
//TODO logging
|
||||
|
||||
func Balonlyl(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
//TODO logging
|
||||
func Matcher(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
log.Debug().Str("text", update.Message.Text).Int64("chat", update.Message.Chat.ID).Msg("Starting text matcher")
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func RunWeb() {
|
||||
func RunServer() {
|
||||
fs := http.FileServer(http.Dir("./videos"))
|
||||
http.Handle("/", fs)
|
||||
|
||||
|
|
Loading…
Reference in a new issue