-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.go
49 lines (38 loc) · 986 Bytes
/
discord.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"fmt"
"github.com/bwmarrin/discordgo"
)
type FetchFileRequest struct {
GuildID string `json:"guild_id"`
ChannelID string `json:"channel_id"`
MessageID string `json:"message_id"`
}
type discord interface {
FetchFile(ctx context.Context, req *FetchFileRequest) (string, error)
}
type Discord struct {
*discordgo.Session
Token string
}
func NewDiscord(token string) discord {
session, err := discordgo.New("Bot " + token)
if err != nil {
panic(fmt.Sprintf("failed to create Discord session: %v", err))
}
return &Discord{
Token: token,
Session: session,
}
}
func (d *Discord) FetchFile(ctx context.Context, req *FetchFileRequest) (string, error) {
msg, err := d.Session.ChannelMessage(req.ChannelID, req.MessageID)
if err != nil {
return "", fmt.Errorf("failed to fetch message: %v", err)
}
if len(msg.Attachments) == 0 {
return "", fmt.Errorf("message has no attachments")
}
return msg.Attachments[0].URL, nil
}