various improvements

This commit is contained in:
watn3y 2023-12-29 02:35:22 +01:00
parent d5272262da
commit 1a6953900c
14 changed files with 71 additions and 49 deletions

1
bot.go
View file

@ -6,7 +6,6 @@ import (
"watn3y/bloaterbot/commands" "watn3y/bloaterbot/commands"
"watn3y/bloaterbot/commands/notify" "watn3y/bloaterbot/commands/notify"
"watn3y/bloaterbot/text" "watn3y/bloaterbot/text"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )

View file

@ -2,25 +2,24 @@ package botIO
import ( import (
"watn3y/bloaterbot/config" "watn3y/bloaterbot/config"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) { func Authenticate() (tgbotapi.UpdatesChannel, *tgbotapi.BotAPI) {
b, err := tgbotapi.NewBotAPI(config.BotConfig.APIToken) bot, err := tgbotapi.NewBotAPI(config.BotConfig.APIToken)
if err != nil { if err != nil {
log.Panic().Err(err).Msg("Failed to authorize bot") log.Panic().Err(err).Msg("Failed to authorize bot")
} }
b.Debug = config.BotConfig.DebugMode bot.Debug = config.BotConfig.DebugMode
u := tgbotapi.NewUpdate(0) updates := tgbotapi.NewUpdate(0)
u.Timeout = 60 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
} }

View file

@ -1,12 +1,18 @@
package botIO 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) { func GetUserByID(userToGet tgbotapi.ChatConfigWithUser, bot *tgbotapi.BotAPI) (error bool, member tgbotapi.ChatMember) {
member, err := bot.GetChatMember(tgbotapi.GetChatMemberConfig{ChatConfigWithUser: userToGet}) member, err := bot.GetChatMember(tgbotapi.GetChatMemberConfig{ChatConfigWithUser: userToGet})
if err != nil { if err != nil {
log.Error().Err(err).Msg("Unable to get info for user")
return true, member return true, member
} }
log.Debug().Interface("member",member).Msg("Got Info for user")
return false, member return false, member
} }

View file

@ -15,6 +15,8 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
//TODO logging
func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
cmd := strings.ToLower(update.Message.Command()) cmd := strings.ToLower(update.Message.Command())

View file

@ -22,6 +22,8 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
//TODO logging
func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) { 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") 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{ msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, 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) workingMessage := botIO.SendMessage(msg, bot)
if len(commandArgs) >= 1 && matchURL(commandArgs[0]) != "" { if len(commandArgs) >= 1 && matchURL(commandArgs[0]) != "" {
//TODO distinguish
//service := matchURL(commandArgs[0])
} else { } 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{ message := tgbotapi.EditMessageTextConfig{
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, 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) botIO.EditMessage(message, bot)
return return

View file

@ -10,7 +10,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
//TODO logging
func GetGP(update tgbotapi.Update, bot *tgbotapi.BotAPI) { 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") log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("getting gaypoints")

View file

@ -11,7 +11,8 @@ var gpSelectUser *sql.Stmt
var gpSelectChat *sql.Stmt var gpSelectChat *sql.Stmt
var gpSet *sql.Stmt var gpSet *sql.Stmt
//TODO logging
//TODO switch to SQL
func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) { func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
rows, err := gpSelectChat.Query(chatid) rows, err := gpSelectChat.Query(chatid)
if err != nil { if err != nil {

View file

@ -10,6 +10,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
//TODO logging
func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) { func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
log.Debug().Str("args", update.Message.CommandArguments()).Msg("parsing new reminder") log.Debug().Str("args", update.Message.CommandArguments()).Msg("parsing new reminder")

View file

@ -9,7 +9,8 @@ var notifySetReminder *sql.Stmt
var notifyGetReminders *sql.Stmt var notifyGetReminders *sql.Stmt
var notifyGetReminderDetails *sql.Stmt var notifyGetReminderDetails *sql.Stmt
var notifyDeleteReminder *sql.Stmt var notifyDeleteReminder *sql.Stmt
//TODO logging
//TODO switch to SQL
func InitDB() { func InitDB() {
const dbPath string = "./bloater.db" const dbPath string = "./bloater.db"
log.Info().Str("dbpath", dbPath).Msg("init database") log.Info().Str("dbpath", dbPath).Msg("init database")

View file

@ -2,6 +2,7 @@ package config
import ( import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"os" "os"
"time" "time"
@ -11,11 +12,11 @@ type config struct {
APIToken string APIToken string
DebugMode bool DebugMode bool
PrivilegedUsers []int64 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 { Webserver struct {
Port string Port string
} }
Balonlyl struct { Balonlyl struct {
EnabledChats []int64 EnabledChats []int64
BalonlylAT string BalonlylAT string
TriggerWords []string TriggerWords []string
@ -39,7 +40,7 @@ func LoadConfig() {
configFile, err := os.ReadFile("config.toml") configFile, err := os.ReadFile("config.toml")
if err != nil { 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) _, err = toml.Decode(string(configFile), &BotConfig)
@ -49,6 +50,10 @@ func LoadConfig() {
BotConfig.Shutup = make(map[int64]time.Time) BotConfig.Shutup = make(map[int64]time.Time)
if !BotConfig.DebugMode {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Info().Msg("loaded config file") log.Info().Msg("loaded config file")
log.Debug().Interface("config", BotConfig).Msg("") log.Debug().Interface("config", BotConfig).Msg("")
@ -59,27 +64,27 @@ func EncodeToml() {
exampleConfig := config{ exampleConfig := config{
APIToken: "1234567:h8Y7ma7hMG93kxDx5o", APIToken: "1234567:h8Y7ma7hMG93kxDx5o",
DebugMode: false, DebugMode: false,
PrivilegedUsers: []int64{1234567,9764382423}, PrivilegedUsers: []int64{1234567, 9764382423},
Webserver: struct{ Port string }{ Webserver: struct{ Port string }{
Port: "3000", Port: "3000",
}, },
Balonlyl: struct { Balonlyl: struct {
EnabledChats []int64 EnabledChats []int64
BalonlylAT string BalonlylAT string
TriggerWords []string TriggerWords []string
}{ }{
EnabledChats: []int64{353454534,8743658}, EnabledChats: []int64{353454534, 8743658},
BalonlylAT: "@Balonlyl", BalonlylAT: "@Balonlyl",
TriggerWords: []string{"French, France"}, TriggerWords: []string{"French, France"},
}, },
GayPoints: struct { GayPoints: struct {
EnabledChats []int64 EnabledChats []int64
ModifyUsers []int64 ModifyUsers []int64
}{ }{
EnabledChats: []int64{353454534,8743658}, EnabledChats: []int64{353454534, 8743658},
ModifyUsers: []int64{3827468324,1736576}, ModifyUsers: []int64{3827468324, 1736576},
}, },
Download: struct { Download: struct {
Prefix string Prefix string
Yourls struct { Yourls struct {
APIPath string APIPath string
@ -97,21 +102,18 @@ func EncodeToml() {
}, },
} }
f, err := os.Create("config.toml.example") f, err := os.Create("config.toml.example")
if err != nil { if err != nil {
log.Panic().Err(err).Msg("error encoding config file. Cannot open config.toml.example") log.Panic().Err(err).Msg("error encoding config file. Cannot open config.toml.example")
}
if err := toml.NewEncoder(f).Encode(exampleConfig); err != nil {
log.Panic().Err(err).Msg("error encoding config file")
}
if err := f.Close(); err != nil {
log.Panic().Err(err).Msg("error encoding config file. Cannot close config.toml.example")
}
log.Info().Msg("Wrote example config to config.toml.example. Bye Bye")
} }
if err := toml.NewEncoder(f).Encode(exampleConfig); err != nil {
log.Panic().Err(err).Msg("error encoding config file")
}
if err := f.Close(); err != nil {
log.Panic().Err(err).Msg("error encoding config file. Cannot close config.toml.example")
}
log.Info().Msg("Wrote example config to config.toml.example. Bye Bye")
}

13
main.go
View file

@ -26,16 +26,25 @@ func main() {
} }
config.LoadConfig() config.LoadConfig()
gaypoints.InitDB() gaypoints.InitDB()
notify.InitDB() notify.InitDB()
go webserver.RunWeb()
go webserver.RunServer()
bot() bot()
} }
func configureLogger() { func configureLogger() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.DateTime} output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.DateTime}
log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger() 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") log.Info().Msg("Started zerolog logger")
} }

View file

@ -6,6 +6,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
//TODO logging
func Balonlyl(update tgbotapi.Update, bot *tgbotapi.BotAPI) { func Balonlyl(update tgbotapi.Update, bot *tgbotapi.BotAPI) {

View file

@ -10,7 +10,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
//TODO logging
func Matcher(update tgbotapi.Update, bot *tgbotapi.BotAPI) { 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") log.Debug().Str("text", update.Message.Text).Int64("chat", update.Message.Chat.ID).Msg("Starting text matcher")

View file

@ -7,7 +7,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func RunWeb() { func RunServer() {
fs := http.FileServer(http.Dir("./videos")) fs := http.FileServer(http.Dir("./videos"))
http.Handle("/", fs) http.Handle("/", fs)