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
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue