init branch;

This commit is contained in:
naudachu 2023-10-25 17:24:24 +05:00
parent f13ba8e204
commit f9fb869abe
2 changed files with 92 additions and 0 deletions

View File

@ -6,10 +6,13 @@ import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"ticket-pimp/bot/handler"
discordbot "ticket-pimp/discord-bot"
"ticket-pimp/internal/services"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/mr-linch/go-tg"
"github.com/mr-linch/go-tg/tgb"
@ -18,6 +21,11 @@ import (
func main() {
log.Print("started")
env("prod.env")
if err := runDiscrodBot(); err != nil {
log.Fatal("Discord bot cannot be runned;")
}
ctx := context.Background()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, os.Kill, syscall.SIGTERM)
@ -38,6 +46,46 @@ func env(envFilePath string) {
}
}
func runDiscrodBot() error {
dbot, err := discordbot.NewDiscordBot(os.Getenv("DISCORD_TOKEN"))
if err != nil {
return err
}
dbot.AddHandler(
func(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.Contains(m.Content, "ping") {
// if ch, err := s.State.Channel(m.ChannelID); err != nil || !ch.IsThread() {
// thread, err := s.MessageThreadStartComplex(m.ChannelID, m.ID, &discordgo.ThreadStart{
// Name: "Pong game with " + m.Author.Username,
// AutoArchiveDuration: 60,
// Invitable: false,
// RateLimitPerUser: 10,
// })
// if err != nil {
// panic(err)
// }
// _, _ = s.ChannelMessageSend(thread.ID, "pong")
// m.ChannelID = thread.ID
// } else {
_, _ = s.ChannelMessageSendReply(m.ChannelID, "pong", m.Reference())
}
// }
})
err = dbot.Open()
if err != nil {
return err
}
defer dbot.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Graceful shutdown")
return nil
}
// runBot ...
// ..function creates new Telegram BOT instance
// ..throw env variables through bot's handlers

View File

@ -0,0 +1,44 @@
package discordbot
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
const (
appID = "1143459608569069589"
publicKey = "34d20a151fda7675e2964525643c8baf6e5ca9156f190f4bcba95a36973fbc2c"
token = "MTE0MzQ1OTYwODU2OTA2OTU4OQ.GYjbl1.QVQLJzlNx1jlw4VLim83wywKsKl17vyw4QWoeI"
)
type DiscordBot struct {
Session *discordgo.Session
}
func NewDiscordBot(token string) (*DiscordBot, error) {
discord, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
}
return &DiscordBot{
Session: discord,
}, nil
}
func (dbot *DiscordBot) AddHandler(handlerFunc func(*discordgo.Session, *discordgo.MessageCreate)) *DiscordBot {
dbot.Session.AddHandler(handlerFunc)
return dbot
}
func (dbot *DiscordBot) Open() error {
if err := dbot.Session.Open(); err != nil {
return fmt.Errorf("cannot open the session: %v", err)
}
return nil
}
func (dbot *DiscordBot) Close() {
dbot.Session.Close()
}