/
modlog.go
139 lines (114 loc) · 3.45 KB
/
modlog.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 slashcommands
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/zekroTJA/shinpuru/internal/services/database"
"github.com/zekroTJA/shinpuru/internal/services/permissions"
"github.com/zekroTJA/shinpuru/internal/util/static"
"github.com/zekroTJA/shinpuru/pkg/acceptmsg/v2"
"github.com/zekrotja/ken"
)
type Modlog struct{}
var (
_ ken.SlashCommand = (*Modlog)(nil)
_ permissions.PermCommand = (*Modlog)(nil)
)
func (c *Modlog) Name() string {
return "modlog"
}
func (c *Modlog) Description() string {
return "Set the mod log channel for a guild."
}
func (c *Modlog) Version() string {
return "1.0.0"
}
func (c *Modlog) Type() discordgo.ApplicationCommandType {
return discordgo.ChatApplicationCommand
}
func (c *Modlog) Options() []*discordgo.ApplicationCommandOption {
return []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "set",
Description: "Set this or a specified channel as mod log channel.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "A channel to be set as mod log (current channel if not specified).",
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
},
},
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "disable",
Description: "Disable modlog.",
},
}
}
func (c *Modlog) Domain() string {
return "sp.guild.config.modlog"
}
func (c *Modlog) SubDomains() []permissions.SubPermission {
return nil
}
func (c *Modlog) Run(ctx ken.Context) (err error) {
if err = ctx.Defer(); err != nil {
return
}
err = ctx.HandleSubCommands(
ken.SubCommandHandler{"set", c.set},
ken.SubCommandHandler{"disable", c.disable},
)
return
}
func (c *Modlog) set(ctx ken.SubCommandContext) (err error) {
db := ctx.Get(static.DiDatabase).(database.Database)
chV, ok := ctx.Options().GetByNameOptional("channel")
if !ok {
acceptMsg := &acceptmsg.AcceptMessage{
Ken: ctx.GetKen(),
Embed: &discordgo.MessageEmbed{
Color: static.ColorEmbedDefault,
Description: "Do you want to set this channel as modlog channel?",
},
UserID: ctx.User().ID,
DeleteMsgAfter: true,
AcceptFunc: func(cctx ken.ComponentContext) (err error) {
if err = cctx.Defer(); err != nil {
return
}
err = db.SetGuildModLog(ctx.GetEvent().GuildID, ctx.GetEvent().ChannelID)
if err != nil {
return
}
err = cctx.FollowUpEmbed(&discordgo.MessageEmbed{
Description: "Set this channel as modlog channel.",
}).Send().Error
return
},
}
if _, err = acceptMsg.AsFollowUp(ctx); err != nil {
return
}
return acceptMsg.Error()
}
ch := chV.ChannelValue(ctx)
if err = db.SetGuildModLog(ctx.GetEvent().GuildID, ch.ID); err != nil {
return
}
err = ctx.FollowUpEmbed(&discordgo.MessageEmbed{
Description: fmt.Sprintf("Set channel <#%s> as modlog channel.", ch.ID),
}).Send().Error
return
}
func (c *Modlog) disable(ctx ken.SubCommandContext) (err error) {
db := ctx.Get(static.DiDatabase).(database.Database)
if err = db.SetGuildModLog(ctx.GetEvent().GuildID, ""); err != nil {
return
}
return ctx.FollowUpEmbed(&discordgo.MessageEmbed{
Description: "Modloging disabled.",
}).Send().Error
}