Fix crashes in DMs, default to ephemeral on errors

master
ALI Hamza 2021-09-07 20:53:38 +07:00
parent d67c3d9a63
commit 87b08cb2c5
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
4 changed files with 44 additions and 26 deletions

@ -71,9 +71,20 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
query := args["query"].String() query := args["query"].String()
embed := b.onDocs(e, query, false) embed := b.onDocs(e, query, false)
var components *[]discord.Component
if !strings.HasPrefix(embed.Title, "Error") { if strings.HasPrefix(embed.Title, "Error") {
err := b.state.DeleteInteractionResponse(e.AppID, e.Token)
if err != nil {
log.Println("failed to delete message:", err)
return
}
_, _ = b.state.CreateInteractionFollowup(e.AppID, e.Token, api.InteractionResponseData{
Flags: api.EphemeralResponse,
Embeds: &[]discord.Embed{embed},
})
return
}
mu.Lock() mu.Lock()
interactionMap[e.ID.String()] = &interactionData{ interactionMap[e.ID.String()] = &interactionData{
id: e.ID.String(), id: e.ID.String(),
@ -83,7 +94,9 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
} }
mu.Unlock() mu.Unlock()
components = &[]discord.Component{ if _, err := b.state.EditInteractionResponse(e.AppID, e.Token, api.EditInteractionResponseData{
Embeds: &[]discord.Embed{embed},
Components: &[]discord.Component{
discord.ActionRowComponent{ discord.ActionRowComponent{
Components: []discord.Component{ Components: []discord.Component{
discord.SelectComponent{ discord.SelectComponent{
@ -93,15 +106,8 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
}, },
}, },
}, },
} },
} }); err != nil {
edit := api.EditInteractionResponseData{
Embeds: &[]discord.Embed{embed},
Components: components,
}
if _, err := b.state.EditInteractionResponse(e.AppID, e.Token, edit); err != nil {
log.Println("failed to send interaction callback:", err) log.Println("failed to send interaction callback:", err)
return return
} }
@ -111,15 +117,22 @@ func (b *botState) onDocsComponent(e *gateway.InteractionCreateEvent, data *inte
var embed discord.Embed var embed discord.Embed
var components *[]discord.Component var components *[]discord.Component
var hasRole bool // if e.Member is nil, all operations should be allowed
hasRole := e.Member == nil
if !hasRole {
for _, role := range e.Member.RoleIDs { for _, role := range e.Member.RoleIDs {
if _, ok := privilegedRoles[role]; ok { if _, ok := privilegedRoles[role]; ok {
hasRole = true hasRole = true
break break
} }
} }
}
isAdmin := func() bool { isAdmin := func() bool {
if e.Member == nil {
return true
}
perms, err := b.state.Permissions(e.ChannelID, e.User.ID) perms, err := b.state.Permissions(e.ChannelID, e.User.ID)
if err != nil { if err != nil {
return false return false
@ -342,6 +355,9 @@ func parseQuery(module string) (string, []string) {
dir, base := path.Split(strings.ToLower(module)) dir, base := path.Split(strings.ToLower(module))
split := strings.Split(base, ".") split := strings.Split(base, ".")
full := dir + split[0] full := dir + split[0]
if strings.HasPrefix(full, "x/") {
full = "golang.org/" + full
}
if complete, ok := stdlibPackages[full]; ok { if complete, ok := stdlibPackages[full]; ok {
full = complete full = complete

@ -4,7 +4,7 @@ go 1.17
require ( require (
github.com/diamondburned/arikawa/v3 v3.0.0-rc.1 github.com/diamondburned/arikawa/v3 v3.0.0-rc.1
github.com/hhhapz/doc v0.3.1 github.com/hhhapz/doc v0.3.2
github.com/k0kubun/pp v3.0.1+incompatible github.com/k0kubun/pp v3.0.1+incompatible
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
) )

@ -24,6 +24,8 @@ github.com/hhhapz/doc v0.3.0 h1:SuUTYmZu83tTXTQ0wyOWYCcVeSSxfzvnSXEsE6jr4VI=
github.com/hhhapz/doc v0.3.0/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo= github.com/hhhapz/doc v0.3.0/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
github.com/hhhapz/doc v0.3.1 h1:TKlytW6nDcJydOIDffYF7OINF6z3x6AUhEvlnfXk9FU= github.com/hhhapz/doc v0.3.1 h1:TKlytW6nDcJydOIDffYF7OINF6z3x6AUhEvlnfXk9FU=
github.com/hhhapz/doc v0.3.1/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo= github.com/hhhapz/doc v0.3.1/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
github.com/hhhapz/doc v0.3.2 h1:/FLWokA4uJRVuCX5Q+MPpX1gRP/sZiBPlmTrwtJf/Ws=
github.com/hhhapz/doc v0.3.2/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40= github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=

@ -22,7 +22,7 @@ type botState struct {
} }
func (b *botState) OnCommand(e *gateway.InteractionCreateEvent) { func (b *botState) OnCommand(e *gateway.InteractionCreateEvent) {
if e.GuildID != discord.NullGuildID { if e.GuildID != 0 {
e.User = &e.Member.User e.User = &e.Member.User
} }