Skip to content

Commit

Permalink
refactor: some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Dec 19, 2023
1 parent c720a25 commit d9d8281
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
25 changes: 17 additions & 8 deletions kreacher/client.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package kreacher

import (
"log"

td "github.com/gotd/td/telegram"
redis "github.com/redis/go-redis/v9"
tele "gopkg.in/telebot.v3"
)

type Kreacher struct {
RedisDB *redis.Client
KreacherBot *tele.Bot
UserBot *td.Client
Logger *log.Logger
Bot *tele.Bot
UserBot *td.Client
RedisDB *redis.Client
}

func NewKreacher(redisDB *redis.Client, kreacherBot *tele.Bot, userBot *td.Client) *Kreacher {
func NewKreacher(
logger *log.Logger,
kreacherBot *tele.Bot,
userBot *td.Client,
redisDB *redis.Client,
) *Kreacher {

return &Kreacher{
RedisDB: redisDB,
KreacherBot: kreacherBot,
UserBot: userBot,
Logger: logger,
Bot: kreacherBot,
UserBot: userBot,
RedisDB: redisDB,
}

}
16 changes: 9 additions & 7 deletions kreacher/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package kreacher

import (
"log"
"log/syslog"
"os"
)

func NewLogger() *log.Logger {
func NewLogger(name string, path string) (*log.Logger, error) {
var logger *log.Logger

syslogWriter, err := syslog.New(syslog.LOG_INFO, "my-app")
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
return nil, err
}
logger.SetOutput(syslogWriter)

defer file.Close()

logger.SetOutput(file)
logger.SetFlags(0)

return logger
return logger, nil
}
25 changes: 22 additions & 3 deletions kreacher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,42 @@ import (
)

func main() {
logger, err := NewLogger("kreacher", "kreacher.log")

if err != nil {
panic(err)
}

redisDB := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", NewConfig().RedisHost, NewConfig().RedisPort),
Password: NewConfig().RedisPassword,
DB: 0, // use default DB
Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3
})

kreacherBot, _ := tele.NewBot(tele.Settings{
defer redisDB.Close()

bot, err := tele.NewBot(tele.Settings{
Token: NewConfig().BotToken,
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
})

if err != nil {
panic(err)
}

defer bot.Close()

userBot := td.NewClient(NewConfig().APIID, NewConfig().APIHash, td.Options{})

kreacher := NewKreacher(redisDB, kreacherBot, userBot)
kreacher := NewKreacher(
logger,
bot,
userBot,
redisDB,
)

err := kreacher.KreacherBot.SetCommands([]tele.Command{
err = kreacher.Bot.SetCommands([]tele.Command{
{Text: "config", Description: "Set the bot's configuration"},
{Text: "help", Description: "How to use this"},
{Text: "leave", Description: "Leave the voice chat"},
Expand Down

0 comments on commit d9d8281

Please sign in to comment.