소스 검색

constants json in cache folder

Drn 4 년 전
부모
커밋
4146b3df20
4개의 변경된 파일74개의 추가작업 그리고 0개의 파일을 삭제
  1. 9 0
      common.go
  2. 4 0
      config.go
  3. 60 0
      main.go
  4. 1 0
      vars.go

+ 9 - 0
common.go

@@ -7,6 +7,7 @@ import (
 	"net/http"
 	"os"
 	"path"
+	"regexp"
 	"strconv"
 	"strings"
 	"time"
@@ -122,6 +123,14 @@ func wrapHyphensW(i string) string {
 	return wrapHyphens(i, 80)
 }
 
+func stripSymbols(i string) string {
+	re, err := regexp.Compile(`[^\w]`)
+	if err != nil {
+		log.Fatal(err)
+	}
+	return re.ReplaceAllString(i, " ")
+}
+
 //#endregion
 
 //#region Requests

+ 4 - 0
config.go

@@ -146,6 +146,10 @@ type configuration struct {
 	 */
 }
 
+type constStruct struct {
+	Constants map[string]string `json:"_constants,omitempty"`
+}
+
 //#endregion
 
 //#region Channels

+ 60 - 0
main.go

@@ -2,12 +2,14 @@ package main
 
 import (
 	"context"
+	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"log"
 	"net/url"
 	"os"
 	"os/signal"
+	"strings"
 	"syscall"
 	"time"
 
@@ -272,6 +274,64 @@ func main() {
 	// Log Status
 	logStatusMessage(logStatusStartup)
 
+	// Cache constants
+	constants := make(map[string]string)
+	//--- Compile constants
+	for _, server := range bot.State.Guilds {
+		serverKey := fmt.Sprintf("SERVER_%s", stripSymbols(server.Name))
+		serverKey = strings.ReplaceAll(serverKey, " ", "_")
+		for strings.Contains(serverKey, "__") {
+			serverKey = strings.ReplaceAll(serverKey, "__", "_")
+		}
+		serverKey = strings.ToUpper(serverKey)
+		if constants[serverKey] == "" {
+			constants[serverKey] = server.ID
+		} else if config.DebugOutput {
+			log.Println(logPrefixDebug, "[Constants]", color.HiYellowString("%s already cached (processing %s, has %s stored)", serverKey, server.ID, constants[serverKey]))
+		}
+		for _, channel := range server.Channels {
+			if channel.Type != discordgo.ChannelTypeGuildCategory {
+				categoryName := ""
+				if channel.ParentID != "" {
+					channelParent, err := bot.State.Channel(channel.ParentID)
+					if err == nil {
+						categoryName = channelParent.Name
+					}
+				}
+				channelKey := fmt.Sprintf("CHANNEL_%s_%s_%s", stripSymbols(server.Name), stripSymbols(categoryName), stripSymbols(channel.Name))
+				channelKey = strings.ReplaceAll(channelKey, " ", "_")
+				for strings.Contains(channelKey, "__") {
+					channelKey = strings.ReplaceAll(channelKey, "__", "_")
+				}
+				channelKey = strings.ToUpper(channelKey)
+				if constants[channelKey] == "" {
+					constants[channelKey] = channel.ID
+				} else if config.DebugOutput {
+					log.Println(logPrefixDebug, "[Constants]", color.HiYellowString("%s already cached (processing %s/%s, has %s stored)", channelKey, server.ID, channel.ID, constants[channelKey]))
+				}
+			}
+		}
+	}
+	//--- Save constants
+	os.MkdirAll(cachePath, 0755)
+	if _, err := os.Stat(constantsPath); err == nil {
+		err = os.Remove(constantsPath)
+		if err != nil {
+			log.Println("[Constants]", color.HiRedString("Encountered error deleting cache file:\t%s", err))
+		}
+	}
+	constantsStruct := constStruct{}
+	constantsStruct.Constants = constants
+	newJson, err := json.MarshalIndent(constantsStruct, "", "\t")
+	if err != nil {
+		log.Println("[Constants]", color.HiRedString("Failed to format constants...\t%s", err))
+	} else {
+		err := ioutil.WriteFile(constantsPath, newJson, 0644)
+		if err != nil {
+			log.Println("[Constants]", color.HiRedString("Failed to save new constants file...\t%s", err))
+		}
+	}
+
 	//#region Background Tasks
 
 	// Tickers

+ 1 - 0
vars.go

@@ -22,6 +22,7 @@ const (
 	cachePath        = "cache"
 	historyCachePath = cachePath + string(os.PathSeparator) + "history"
 	imgStorePath     = cachePath + string(os.PathSeparator) + "imgStore"
+	constantsPath    = cachePath + string(os.PathSeparator) + "constants.json"
 
 	defaultReact = "✅"
 )