Fix stuff from chan

master
ALI Hamza 2021-09-07 18:06:42 +07:00
parent 83d17a5ad6
commit 4480b1ed6b
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
3 changed files with 25 additions and 12 deletions

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"os" "os"
"github.com/pkg/errors"
) )
type Configuration struct { type Configuration struct {
@ -11,15 +13,15 @@ type Configuration struct {
Token string `json:"token"` Token string `json:"token"`
} }
func config() Configuration {
var config Configuration var config Configuration
func init() {
fileBytes, err := os.ReadFile("config.json") fileBytes, err := os.ReadFile("config.json")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(errors.Wrap(err, "could not open config"))
} }
err = json.Unmarshal(fileBytes, &config) err = json.Unmarshal(fileBytes, &config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(errors.Wrap(err, "could not parse config"))
} }
return config
} }

@ -6,6 +6,7 @@ import (
"log" "log"
"path" "path"
"strings" "strings"
"sync"
"time" "time"
"github.com/diamondburned/arikawa/v3/api" "github.com/diamondburned/arikawa/v3/api"
@ -35,18 +36,23 @@ type interactionData struct {
full bool full bool
} }
var interactionMap = map[string]*interactionData{} var (
interactionMap = map[string]*interactionData{}
mu sync.Mutex
)
func init() { func init() {
go func() { go func() {
for { for {
time.Sleep(time.Minute * 5) time.Sleep(time.Minute * 5)
now := time.Now() now := time.Now()
mu.Lock()
for _, data := range interactionMap { for _, data := range interactionMap {
if data.created.After(now) { if now.After(data.created.Add(time.Minute * 5)) {
delete(interactionMap, data.id) delete(interactionMap, data.id)
} }
} }
mu.Unlock()
} }
}() }()
} }
@ -64,16 +70,18 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent) {
} }
query := args["query"].String() query := args["query"].String()
embed := b.onDocs(e, args["query"].String(), false) embed := b.onDocs(e, query, false)
var components *[]discord.Component var components *[]discord.Component
if !strings.HasPrefix(embed.Title, "Error") { if !strings.HasPrefix(embed.Title, "Error") {
mu.Lock()
interactionMap[e.ID.String()] = &interactionData{ interactionMap[e.ID.String()] = &interactionData{
id: e.ID.String(), id: e.ID.String(),
created: time.Now(), created: time.Now(),
user: e.User, user: e.User,
query: query, query: query,
} }
mu.Unlock()
components = &[]discord.Component{ components = &[]discord.Component{
discord.ActionRowComponent{ discord.ActionRowComponent{
@ -159,7 +167,9 @@ func (b *botState) onDocsComponent(e *gateway.InteractionCreateEvent, data *inte
embed = b.onDocs(e, data.query, data.full) embed = b.onDocs(e, data.query, data.full)
embed.Description = "" embed.Description = ""
embed.Footer = nil embed.Footer = nil
mu.Lock()
delete(interactionMap, data.id) delete(interactionMap, data.id)
mu.Unlock()
} }
if e.GuildID != discord.NullGuildID { if e.GuildID != discord.NullGuildID {

@ -43,13 +43,14 @@ func main() {
flag.Parse() flag.Parse()
update = *updateVar update = *updateVar
if config.Token == "" { cfg := config()
log.Fatalln("no token provided") if cfg.Token == "" {
log.Fatal("no token provided")
} }
s, err := state.New("Bot " + config.Token) s, err := state.New("Bot " + cfg.Token)
if err != nil { if err != nil {
log.Fatalln("session failed:", err) log.Fatalln(errors.Wrap(err, "could not open session"))
} }
searcher := doc.New(http.DefaultClient, godocs.Parser) searcher := doc.New(http.DefaultClient, godocs.Parser)