Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
V4NSH4J
GitHub Repository: V4NSH4J/discord-mass-DM-GO
Path: blob/main/discord/embed.go
310 views
1
// Copyright (C) 2021 github.com/V4NSH4J
2
//
3
// This source code has been released under the GNU Affero General Public
4
// License v3.0. A copy of this license is available at
5
// https://www.gnu.org/licenses/agpl-3.0.en.html
6
7
package discord
8
9
import (
10
"bytes"
11
"encoding/json"
12
"fmt"
13
"io"
14
"net/http"
15
16
"github.com/V4NSH4J/discord-mass-dm-GO/utilities"
17
)
18
19
type ImageEmbed struct {
20
Url string `json:"url"`
21
Thumbnail bool `json:"thumbnail"`
22
}
23
type ProviderEmbed struct {
24
Url string `json:"url"`
25
Name string `json:"name"`
26
}
27
type AuthorEmbed struct {
28
Url string `json:"url"`
29
Name string `json:"name"`
30
}
31
type Embed struct {
32
Title string `json:"title"`
33
Description string `json:"description"`
34
Color string `json:"color"`
35
Redirect string `json:"redirect"`
36
Author AuthorEmbed `json:"author"`
37
Image ImageEmbed `json:"image"`
38
Provider ProviderEmbed `json:"provider"`
39
}
40
41
type EmbedJSONResponse struct {
42
EmbedData Embed `json:"embed"`
43
Timestamp string `json:"timestamp"`
44
Id string `json:"id"`
45
Link string `json:"link"`
46
}
47
48
func LanuchEmbed() {
49
utilities.LogWarn("This feature is provided and hosted by a 3rd party Entity. Use at your own discretion. Contact https://github.com/itschasa for more information.")
50
var embeddata []byte
51
var err string
52
embeddata, err = utilities.GetEmbed()
53
if err == "" {
54
responseBody := bytes.NewBuffer(embeddata)
55
resp, err := http.Post("https://e.chasa.wtf/api/v1/embed", "application/json", responseBody)
56
if err != nil {
57
utilities.LogErr("Error while getting response from 3rd Party Embed API", err)
58
} else {
59
bodyBytes, err := io.ReadAll(resp.Body)
60
if err != nil {
61
utilities.LogErr("Error while reading response body", err)
62
} else {
63
var respdata EmbedJSONResponse
64
err := json.Unmarshal(bodyBytes, &respdata)
65
if err != nil {
66
utilities.LogErr("Error while unmarshalling response body", err)
67
} else {
68
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated {
69
utilities.LogInfo("Created Embed Link, use the link below and add it to your message in message.json.")
70
utilities.LogInfo(respdata.Link)
71
utilities.LogInfo("Make sure to restart DMDGO after editing message.json")
72
utilities.LogInfo("Service provided with <3 by chasa (https://github.com/itschasa)")
73
} else {
74
utilities.LogErr("Unexpected response from server: %v [Try again or the API might be down] Response :%v", fmt.Sprint(resp.StatusCode), string(bodyBytes))
75
}
76
}
77
}
78
}
79
} else {
80
utilities.LogErr("Error while getting embed data from embed.json %v", err)
81
}
82
}
83
84