initial
This commit is contained in:
commit
7c610d0477
11 changed files with 399 additions and 0 deletions
69
discordBot/handler.go
Normal file
69
discordBot/handler.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package discordBot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/disgoorg/disgo"
|
||||
"github.com/disgoorg/disgo/bot"
|
||||
"github.com/disgoorg/disgo/discord"
|
||||
"github.com/disgoorg/disgo/gateway"
|
||||
"github.com/disgoorg/snowflake/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"tg-disc-bot/config"
|
||||
"tg-disc-bot/dto"
|
||||
)
|
||||
|
||||
type DiscordBot struct {
|
||||
ctx context.Context
|
||||
client bot.Client
|
||||
guildID snowflake.ID
|
||||
channelID snowflake.ID
|
||||
}
|
||||
|
||||
func NewDiscordBot(ctx context.Context, config config.DiscordConfig) (*DiscordBot, error) {
|
||||
var err error
|
||||
dsBot := &DiscordBot{ctx: ctx, channelID: config.ChannelID}
|
||||
|
||||
dsBot.client, err = disgo.New(config.Token,
|
||||
bot.WithGatewayConfigOpts(
|
||||
gateway.WithIntents(
|
||||
gateway.IntentGuilds,
|
||||
gateway.IntentGuildMessages,
|
||||
gateway.IntentDirectMessages,
|
||||
gateway.IntentMessageContent,
|
||||
),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dsBot, nil
|
||||
}
|
||||
|
||||
func (d *DiscordBot) Start(fromTelegram chan dto.TelegramDTO) chan dto.DiscordDTO {
|
||||
log.Info("Starting discord bot...")
|
||||
|
||||
msgChan := make(chan dto.DiscordDTO, 100)
|
||||
d.client.AddEventListeners(&messageHandler{msgChan: msgChan})
|
||||
|
||||
go func() {
|
||||
for msg := range fromTelegram {
|
||||
log.WithField("content", msg).Debug("DS | Message from Telegram")
|
||||
|
||||
m := discord.MessageCreate{Content: fmt.Sprintf("[%s]\n%s", msg.AuthorName, msg.Content)}
|
||||
|
||||
_, err := d.client.Rest().CreateMessage(d.channelID, m)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to send message to Discord: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if err := d.client.OpenGateway(d.ctx); err != nil {
|
||||
log.Fatalf("Failed to open discord gateway %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return msgChan
|
||||
}
|
||||
28
discordBot/messages.go
Normal file
28
discordBot/messages.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package discordBot
|
||||
|
||||
import (
|
||||
"github.com/disgoorg/disgo/bot"
|
||||
"github.com/disgoorg/disgo/events"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"tg-disc-bot/dto"
|
||||
)
|
||||
|
||||
type messageHandler struct {
|
||||
msgChan chan dto.DiscordDTO
|
||||
}
|
||||
|
||||
func (m *messageHandler) OnEvent(event bot.Event) {
|
||||
if e, ok := event.(*events.MessageCreate); ok {
|
||||
if !e.Message.Author.Bot {
|
||||
message := dto.DiscordDTO{
|
||||
AuthorName: e.Message.Author.Username,
|
||||
Content: e.Message.Content,
|
||||
}
|
||||
|
||||
m.msgChan <- message
|
||||
} else {
|
||||
log.Debug("DS | Bot message, skipping")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue