added: image send
This commit is contained in:
parent
4540519753
commit
9714464d20
3 changed files with 218 additions and 25 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package discordBot
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/disgoorg/disgo"
|
||||
|
|
@ -18,11 +19,16 @@ type DiscordBot struct {
|
|||
client bot.Client
|
||||
guildID snowflake.ID
|
||||
channelID snowflake.ID
|
||||
token string
|
||||
}
|
||||
|
||||
func NewDiscordBot(ctx context.Context, config config.DiscordConfig) (*DiscordBot, error) {
|
||||
var err error
|
||||
dsBot := &DiscordBot{ctx: ctx, channelID: config.ChannelID}
|
||||
dsBot := &DiscordBot{
|
||||
ctx: ctx,
|
||||
channelID: config.ChannelID,
|
||||
token: config.Token,
|
||||
}
|
||||
|
||||
dsBot.client, err = disgo.New(config.Token,
|
||||
bot.WithGatewayConfigOpts(
|
||||
|
|
@ -40,7 +46,7 @@ func NewDiscordBot(ctx context.Context, config config.DiscordConfig) (*DiscordBo
|
|||
return dsBot, nil
|
||||
}
|
||||
|
||||
func (d *DiscordBot) Start(fromTelegram chan dto.TelegramDTO) chan dto.DiscordDTO {
|
||||
func (d *DiscordBot) Start(fromTelegram <-chan dto.TelegramDTO) chan dto.DiscordDTO {
|
||||
log.Info("Starting discord bot...")
|
||||
|
||||
msgChan := make(chan dto.DiscordDTO, 100)
|
||||
|
|
@ -50,11 +56,26 @@ func (d *DiscordBot) Start(fromTelegram chan dto.TelegramDTO) chan dto.DiscordDT
|
|||
for msg := range fromTelegram {
|
||||
log.WithField("content", msg).Debug("DS | Message from Telegram")
|
||||
|
||||
var files []*discord.File
|
||||
for _, media := range *(msg.Images) {
|
||||
file := &discord.File{
|
||||
Name: media.Filename,
|
||||
Reader: bytes.NewReader(media.Data),
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
|
||||
m := discord.MessageCreate{Content: fmt.Sprintf("[%s]\n%s", msg.AuthorName, msg.Content)}
|
||||
|
||||
if len(files) > 0 {
|
||||
m.Files = append(m.Files, files...)
|
||||
|
||||
}
|
||||
|
||||
_, err := d.client.Rest().CreateMessage(d.channelID, m)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to send message to Discord: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -2,8 +2,11 @@ package discordBot
|
|||
|
||||
import (
|
||||
"github.com/disgoorg/disgo/bot"
|
||||
"github.com/disgoorg/disgo/discord"
|
||||
"github.com/disgoorg/disgo/events"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"tg-disc-bot/dto"
|
||||
)
|
||||
|
||||
|
|
@ -14,9 +17,20 @@ type messageHandler struct {
|
|||
func (m *messageHandler) OnEvent(event bot.Event) {
|
||||
if e, ok := event.(*events.MessageCreate); ok {
|
||||
if !e.Message.Author.Bot {
|
||||
attachments := e.Message.Attachments
|
||||
var images *[]dto.Image
|
||||
var err error
|
||||
if len(attachments) > 0 {
|
||||
images, err = getImages(attachments)
|
||||
if err != nil {
|
||||
log.Warnf("could not get images from attachments: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
message := dto.DiscordDTO{
|
||||
AuthorName: e.Message.Author.Username,
|
||||
Content: e.Message.Content,
|
||||
Images: images,
|
||||
}
|
||||
|
||||
m.msgChan <- message
|
||||
|
|
@ -26,3 +40,36 @@ func (m *messageHandler) OnEvent(event bot.Event) {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
func getImages(a []discord.Attachment) (*[]dto.Image, error) {
|
||||
var images []dto.Image
|
||||
for _, item := range a {
|
||||
if (*item.ContentType == "image/png") || (*item.ContentType == "image/jpeg") {
|
||||
imageData, err := downloadImage(item.URL)
|
||||
if err != nil {
|
||||
log.Errorf("Error downloading image: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
images = append(images, dto.Image{
|
||||
Data: imageData,
|
||||
Filename: item.Filename,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &images, nil
|
||||
}
|
||||
|
||||
func downloadImage(url string) ([]byte, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue