-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.go
139 lines (122 loc) · 3.73 KB
/
base.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
134
135
136
137
138
139
package main
import (
"fmt"
"github.com/5HT2/taro-bot/bot"
"github.com/5HT2/taro-bot/cmd"
"github.com/5HT2/taro-bot/plugins"
"github.com/diamondburned/arikawa/v3/discord"
"strconv"
"strings"
"time"
)
func InitPlugin(_ *plugins.PluginInit) *plugins.Plugin {
return &plugins.Plugin{
Name: "Taro Base",
Description: "The base commands and responses included as part of the bot",
Version: "1.0.1",
Commands: []bot.CommandInfo{{
Fn: InviteCommand,
FnName: "InviteCommand",
Name: "invite",
Description: "Invite the bot to your own server!",
}, {
Fn: HelpCommand,
FnName: "HelpCommand",
Name: "help",
Description: "Print a list of available commands",
Aliases: []string{"h"},
}, {
Fn: PingCommand,
FnName: "PingCommand",
Name: "ping",
Description: "Returns the current API latency",
}, {
Fn: PrefixCommand,
FnName: "PrefixCommand",
Name: "prefix",
Description: "Set the bot prefix for your guild",
GuildOnly: true,
}},
Responses: []bot.ResponseInfo{{
Fn: PrefixResponse,
Regexes: []string{"<@!?DISCORD_BOT_ID>", "(prefix|help)"},
MatchMin: 2,
}},
}
}
func HelpCommand(c bot.Command) error {
fmtCmds := make([]string, 0)
for _, command := range bot.Commands {
// Filter GuildOnly commands when not in a guild
if !command.GuildOnly || c.E.GuildID.IsValid() {
fmtCmds = append(fmtCmds, command.MarkdownString())
}
}
_, err := cmd.SendEmbed(c.E,
"Taro Help",
strings.Join(fmtCmds, "\n\n"),
bot.DefaultColor)
return err
}
func InviteCommand(c bot.Command) error {
_, err := cmd.SendEmbed(c.E,
bot.User.Username+" invite", fmt.Sprintf("[Click to add me to your own server!](https://discord.com/oauth2/authorize?client_id=%v&permissions=%v&scope=bot)", bot.User.ID, bot.PermissionsHex),
bot.SuccessColor,
)
return err
}
func PingCommand(c bot.Command) error {
if msg, err := cmd.SendEmbed(c.E,
"Ping!",
"Waiting for API response...",
bot.DefaultColor); err != nil {
return err
} else {
curTime := time.Now().UnixMilli()
msgTime := msg.Timestamp.Time().UnixMilli()
embed := cmd.MakeEmbed("Pong!", "Latency is "+strconv.FormatInt(curTime-msgTime, 10)+"ms", bot.SuccessColor)
_, err = bot.Client.EditMessage(msg.ChannelID, msg.ID, "", embed)
return err
}
}
func PrefixCommand(c bot.Command) error {
arg, argErr := cmd.ParseStringArg(c.Args, 1, false)
if argErr != nil {
return argErr
}
// Filter spaces
arg = strings.ReplaceAll(arg, " ", "")
if len(arg) == 0 {
return bot.GenericError(c.FnName, "getting prefix", "prefix is empty")
}
// Prefix is okay, set it in the cache
//
bot.C.Run(func(config *bot.Config) {
config.PrefixCache[int64(c.E.GuildID)] = arg
})
// Also set it in the guild
//
bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
g.Prefix = arg
return g, "PrefixCommand"
})
embed := discord.Embed{
Description: "Set prefix to `" + arg + "`.",
Footer: &discord.EmbedFooter{Text: "At any time you can ping the bot with the word \"prefix\" to get the current prefix"},
Color: bot.SuccessColor,
}
_, err := cmd.SendCustomEmbed(c.E.ChannelID, embed)
return err
}
func PrefixResponse(r bot.Response) {
if !r.E.GuildID.IsValid() {
_, _ = cmd.SendEmbed(r.E, "", "Commands in DMs don't use a prefix!\nUse `help` for a list of commands.", bot.DefaultColor)
return
}
prefix := bot.DefaultPrefix
bot.GuildContext(r.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
prefix = g.Prefix
return g, "PrefixResponse"
})
_, _ = cmd.SendEmbed(r.E, "", fmt.Sprintf("The current prefix is `%s`\nUse `%shelp` for a list of commands.", prefix, prefix), bot.DefaultColor)
}