This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
/
ctx.go
93 lines (77 loc) · 2.58 KB
/
ctx.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
package crouter
import (
"errors"
"strings"
"github.com/bwmarrin/discordgo"
)
const (
// SuccessEmoji is the emoji used to designate a successful action
SuccessEmoji = "✅"
// ErrorEmoji is the emoji used for errors
ErrorEmoji = "❌"
// WarnEmoji is the emoji used to warn that a command failed
WarnEmoji = "⚠️"
)
// Ctx is the context for a command
type Ctx struct {
Command string
Args []string
RawArgs string
Session *discordgo.Session
BotUser *discordgo.User
Message *discordgo.MessageCreate
Channel *discordgo.Channel
Author *discordgo.User
Cmd *Command
Router *Router
AdditionalParams map[string]interface{}
}
// Errors when creating Context
var (
ErrorNoBotUser = errors.New("bot user not found in state cache")
)
// Context creates a new Ctx
func (r *Router) Context(m *discordgo.MessageCreate) (ctx *Ctx, err error) {
if r.Session.State.User == nil {
return nil, ErrorNoBotUser
}
if !r.prefixUsersSet {
r.Prefixes = append(r.Prefixes, "<@"+r.Session.State.User.ID+">", "<@!"+r.Session.State.User.ID+">")
r.prefixUsersSet = true
}
messageContent := TrimPrefixesSpace(m.Content, r.Prefixes...)
message := strings.Split(messageContent, " ")
command := TrimPrefixesSpace(strings.ToLower(message[0]), r.Prefixes...)
args := []string{}
if len(message) > 1 {
args = message[1:]
}
raw := strings.Join(args, " ")
ctx = &Ctx{Command: command, Args: args, Message: m, Author: m.Author, RawArgs: raw, Router: r, Session: r.Session}
channel, err := r.Session.State.Channel(m.ChannelID)
if err != nil && err != discordgo.ErrStateNotFound {
return ctx, err
} else if err == discordgo.ErrStateNotFound {
channel, err = r.Session.Channel(m.ChannelID)
if err != nil {
return ctx, err
}
}
ctx.Channel = channel
ctx.AdditionalParams = make(map[string]interface{})
ctx.BotUser = r.Session.State.User
return ctx, nil
}
func (ctx *Ctx) botHasSendPerms(embeds, files bool) bool {
if ctx.Message.GuildID == "" {
return true
}
if embeds && files {
return ctx.Router.PermCache.HasPermissions(ctx.BotUser.ID, ctx.Channel.ID, discordgo.PermissionSendMessages+discordgo.PermissionEmbedLinks+discordgo.PermissionAttachFiles)
} else if embeds {
return ctx.Router.PermCache.HasPermissions(ctx.BotUser.ID, ctx.Channel.ID, discordgo.PermissionSendMessages+discordgo.PermissionEmbedLinks)
} else if files {
return ctx.Router.PermCache.HasPermissions(ctx.BotUser.ID, ctx.Channel.ID, discordgo.PermissionSendMessages+discordgo.PermissionAttachFiles)
}
return ctx.Router.PermCache.HasPermissions(ctx.BotUser.ID, ctx.Channel.ID, discordgo.PermissionSendMessages)
}