-
Notifications
You must be signed in to change notification settings - Fork 2
/
base-fun.go
133 lines (116 loc) · 3.37 KB
/
base-fun.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"encoding/json"
"github.com/5HT2/taro-bot/bot"
"github.com/5HT2/taro-bot/cmd"
"github.com/5HT2/taro-bot/plugins"
"github.com/5HT2/taro-bot/util"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"net/http"
"strconv"
)
func InitPlugin(_ *plugins.PluginInit) *plugins.Plugin {
return &plugins.Plugin{
Name: "Taro Base Fun",
Description: "The fun commands as included as part of the bot",
Version: "1.0.0",
Commands: []bot.CommandInfo{{
Fn: FrogCommand,
FnName: "FrogCommand",
Name: "frog",
Description: "\\*hands you a random frog pic\\*",
}, {
Fn: StealEmojiCommand,
FnName: "StealEmojiCommand",
Name: "stealemoji",
Aliases: []string{"se"},
Description: "Upload an emoji to the current guild",
GuildOnly: true,
}},
Responses: []bot.ResponseInfo{},
}
}
func FrogCommand(c bot.Command) error {
frogData, _, err := util.RequestUrl("https://frog.pics/api/random", http.MethodGet)
if err != nil {
return err
}
type FrogPicture struct {
ImageUrl string `json:"image_url"`
MedianColor string `json:"median_color"`
}
var frogPicture FrogPicture
if err := json.Unmarshal(frogData, &frogPicture); err != nil {
return err
}
color, err := util.ParseHexColorFast("#" + frogPicture.MedianColor)
if err != nil {
return bot.SyntaxError("ParseHexColorFast", err.Error())
}
embed := discord.Embed{
Color: discord.Color(util.ConvertColorToInt32(color)),
Image: &discord.EmbedImage{URL: frogPicture.ImageUrl},
}
_, err = cmd.SendCustomEmbed(c.E.ChannelID, embed)
return err
}
func StealEmojiCommand(c bot.Command) error {
// try to get emoji ID
emojiID, argErr := cmd.ParseInt64Arg(c.Args, 1)
// try to get emoji URL
if argErr != nil {
emojiID, argErr = cmd.ParseEmojiUrlArg(c.Args, 1)
}
// try to get sent emoji
if argErr != nil {
emojiID, argErr = cmd.ParseEmojiIdArg(c.Args, 1)
}
// no emoji found
if argErr != nil {
return argErr
}
//
// we now have the emoji ID, get the name
emojiName, argErr := cmd.ParseStringArg(c.Args, 2, false)
if argErr != nil {
return bot.GenericError(c.FnName, "getting emoji name", "expected emoji name")
}
//
// we now have the emoji ID and name, get the bytes
url := "https://cdn.discordapp.com/emojis/" + strconv.FormatInt(emojiID, 10)
bytes, res, err := util.RequestUrl(url+".gif", http.MethodGet)
if err != nil {
return err
}
if res.StatusCode != 200 {
bytes, res, err = util.RequestUrl(url+".png", http.MethodGet)
if err != nil {
return err
}
if res.StatusCode != 200 {
return bot.GenericError(c.FnName, "getting emoji bytes", "status was "+res.Status)
}
}
// now we try to upload it
image := api.Image{ContentType: res.Header.Get("content-type"), Content: bytes}
createEmojiData := api.CreateEmojiData{
Name: emojiName,
Image: image,
AuditLogReason: api.AuditLogReason(
"emoji created by " + util.GetUserMention(int64(c.E.Author.ID)),
),
}
if emoji, err := bot.Client.CreateEmoji(c.E.GuildID, createEmojiData); err != nil {
// error with uploading
return bot.GenericError(c.FnName, "uploading emoji", err.Error())
} else {
// uploaded successfully, send a nice embed
_, err := bot.Client.SendMessage(
c.E.ChannelID,
emoji.String(),
discord.Embed{Title: "Emoji stolen ;)", Color: bot.SuccessColor},
)
return err
}
}