Remove interactions from messages no longer being listened to

master
ALI Hamza 2021-09-08 20:36:40 +07:00
parent db2522e982
commit 9f62cba581
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
4 changed files with 38 additions and 60 deletions

@ -13,6 +13,7 @@ import (
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/hhhapz/doc"
"github.com/pkg/errors"
)
var (
@ -31,7 +32,8 @@ var (
type interactionData struct {
id string
created time.Time
user *discord.User
token string
userID discord.UserID
query string
full bool
}
@ -41,24 +43,41 @@ var (
mu sync.Mutex
)
func gcInteractionData() {
func (b *botState) gcInteractionData() {
mapTicker := time.NewTicker(time.Minute * 5)
cacheTicker := time.NewTicker(time.Hour * 24)
for {
time.Sleep(time.Minute * 5)
select {
// gc interaction tokens
case <-mapTicker.C:
now := time.Now()
mu.Lock()
for _, data := range interactionMap {
if now.After(data.created.Add(time.Minute * 5)) {
delete(interactionMap, data.id)
if !now.After(data.created.Add(time.Minute * 5)) {
continue
}
delete(interactionMap, data.id)
b.state.EditInteractionResponse(b.appID, data.token, api.EditInteractionResponseData{
Components: &[]discord.Component{},
})
}
mu.Unlock()
case <-cacheTicker.C:
b.searcher.WithCache(func(cache map[string]*doc.CachedPackage) {
for k := range cache {
delete(cache, k)
}
})
}
}
}
func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
data := api.InteractionResponse{Type: api.DeferredMessageInteractionWithSource}
if err := b.state.RespondInteraction(e.ID, e.Token, data); err != nil {
log.Println("failed to send interaction callback:", err)
log.Println(errors.Wrap(err, "could not send interaction callback"))
return
}
@ -87,7 +106,8 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
interactionMap[e.ID.String()] = &interactionData{
id: e.ID.String(),
created: time.Now(),
user: e.User,
token: e.Token,
userID: e.User.ID,
query: query,
}
mu.Unlock()
@ -106,7 +126,7 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
},
},
}); err != nil {
log.Println("failed to send interaction callback:", err)
log.Println(errors.Wrap(err, "could not send interaction callback"))
return
}
}
@ -197,7 +217,7 @@ func (b *botState) onDocsComponent(e *gateway.InteractionCreateEvent, data *inte
if e.GuildID != discord.NullGuildID {
// Check admin last.
if e.User.ID != data.user.ID && !hasRole && !isAdmin() {
if e.User.ID != data.userID && !hasRole && !isAdmin() {
embed = failEmbed("Error", notOwner)
}
}
@ -220,10 +240,7 @@ func (b *botState) onDocsComponent(e *gateway.InteractionCreateEvent, data *inte
},
}
}
if err := b.state.RespondInteraction(e.ID, e.Token, resp); err != nil {
log.Println("failed to send interaction callback:", err)
}
b.state.RespondInteraction(e.ID, e.Token, resp)
}
func (b *botState) onDocs(e *gateway.InteractionCreateEvent, query string, full bool) discord.Embed {

@ -4,22 +4,16 @@ go 1.17
require (
github.com/diamondburned/arikawa/v3 v3.0.0-rc.1
github.com/hhhapz/doc v0.3.2
github.com/k0kubun/pp v3.0.1+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/hhhapz/doc v0.4.1
github.com/pkg/errors v0.9.1
)
require (
github.com/PuerkitoBio/goquery v1.7.1 // indirect
github.com/andybalholm/cascadia v1.2.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gorilla/schema v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
)

@ -2,8 +2,6 @@ github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9F
github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY=
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
github.com/diamondburned/arikawa/v3 v3.0.0-20210824182349-f334491deed4 h1:Lj1nG3DiPyY4Mu7N4Wk0wtKtQsFlX9/8oT4g1EjENIo=
github.com/diamondburned/arikawa/v3 v3.0.0-20210824182349-f334491deed4/go.mod h1:sNqM/iGXuH87wEH1rpQBEY1PR0AAkRKJuUhJGOdo7To=
github.com/diamondburned/arikawa/v3 v3.0.0-rc.1 h1:1RHtYaVstlEJ5v8b3PNUIpa7D0cfg3qT7n7WbWjb5ZQ=
github.com/diamondburned/arikawa/v3 v3.0.0-rc.1/go.mod h1:sNqM/iGXuH87wEH1rpQBEY1PR0AAkRKJuUhJGOdo7To=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
@ -12,37 +10,11 @@ github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hhhapz/doc v0.1.0 h1:Saowq412pkjVwzbbaj26E4695MsAh2YvRAy7ai2gE9Y=
github.com/hhhapz/doc v0.1.0/go.mod h1:EmQF3Mq8Y4MacCdVLDORczoxh1AD4WS6VpvHILXNbDE=
github.com/hhhapz/doc v0.1.2 h1:WqVVy/1Pa0LvPd5hhi7Z6SDRNBZ1CJ30cb/vEOww0iQ=
github.com/hhhapz/doc v0.1.2/go.mod h1:veI3TCLBx/7jTkNVShg1omZOx+XducAFqRQN3yG/xyM=
github.com/hhhapz/doc v0.1.3 h1:GOwFE+ewsLE+fIqQyBfJixuYfK3hpzVJss/gIVwu/4o=
github.com/hhhapz/doc v0.1.3/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
github.com/hhhapz/doc v0.1.4 h1:xspKqlE36MmkuKhcHAzNUWlz6iyL4EWPh0Ky2Y/eaZQ=
github.com/hhhapz/doc v0.1.4/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
github.com/hhhapz/doc v0.2.0 h1:vUCk8kQ7s+SPrjF/+ID2+qa1Jvo0YQXmco/n4vfLG2k=
github.com/hhhapz/doc v0.2.0/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
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.1 h1:TKlytW6nDcJydOIDffYF7OINF6z3x6AUhEvlnfXk9FU=
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/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/hhhapz/doc v0.4.1 h1:IOcqlJvnePZCA7iJ2gEN8ufDJfrLTGo63g8AEnntQX4=
github.com/hhhapz/doc v0.4.1/go.mod h1:RzUhRa6guPMbXtNfNm4jW6aYCu+JBapioQEgrzoHSMo=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@ -50,17 +22,12 @@ golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GE
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e h1:EHBhcS0mlXEAVwNyO2dLfjToGsyY4j24pTs2ScHnX7s=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

@ -85,7 +85,7 @@ func main() {
return
}
go gcInteractionData()
go b.gcInteractionData()
select {}
}