modules/config: Move roles and token into config file

master
Luther Wen Xu 2019-12-13 18:07:39 +07:00
parent c73d28cc79
commit 801f9f8d6f
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
6 changed files with 12 additions and 27 deletions

2
.gitignore vendored

@ -1,4 +1,2 @@
token.txt
roles.txt
bot.db bot.db
config.json config.json

@ -2,10 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -13,14 +11,13 @@ import (
"PermissionGacha/db" "PermissionGacha/db"
"PermissionGacha/modules/active" "PermissionGacha/modules/active"
"PermissionGacha/modules/commands" "PermissionGacha/modules/commands"
"PermissionGacha/modules/config"
"PermissionGacha/modules/level" "PermissionGacha/modules/level"
"PermissionGacha/modules/starboard" "PermissionGacha/modules/starboard"
) )
func main() { func main() {
token, _ := ioutil.ReadFile("token.txt") dg, err := discordgo.New(config.BotToken)
dg, err := discordgo.New(strings.TrimSpace(string(token)))
if err != nil { if err != nil {
fmt.Println("Error creating Discord session:", err) fmt.Println("Error creating Discord session:", err)
return return

@ -11,6 +11,8 @@ type config struct {
GuildID string `json:"guildID"` GuildID string `json:"guildID"`
PrestigeRequirement int `json:"prestigeFactor"` PrestigeRequirement int `json:"prestigeFactor"`
StarChannel string `json:"starChannel"` StarChannel string `json:"starChannel"`
BotToken string `json:"token"`
LevelRoles []string `json:"roles"`
} }
type activeNerf struct { type activeNerf struct {
@ -36,6 +38,8 @@ func init() {
GuildID = output.GuildID GuildID = output.GuildID
PrestigeRequirement = output.PrestigeRequirement PrestigeRequirement = output.PrestigeRequirement
StarChannel = output.StarChannel StarChannel = output.StarChannel
BotToken = output.BotToken
LevelRoles = output.LevelRoles
} }
//Exposed Variables //Exposed Variables
@ -47,3 +51,5 @@ var CommandPrefix string
var GuildID string var GuildID string
var PrestigeRequirement int var PrestigeRequirement int
var StarChannel string var StarChannel string
var BotToken string
var LevelRoles []string

@ -9,7 +9,7 @@ import (
func GiveLevelRoles(s *discordgo.Session, discordID string, oldLevel, newLevel int) { func GiveLevelRoles(s *discordgo.Session, discordID string, oldLevel, newLevel int) {
for i := oldLevel + 1; i <= newLevel; i++ { for i := oldLevel + 1; i <= newLevel; i++ {
s.GuildMemberRoleAdd(config.GuildID, discordID, rolesFromLevel[i-1]) s.GuildMemberRoleAdd(config.GuildID, discordID, config.LevelRoles[i-1])
} }
} }
@ -20,7 +20,7 @@ func RemoveAllLevelRoles(s *discordgo.Session, discordID string) {
return return
} }
for _, v := range member.Roles { for _, v := range member.Roles {
for _, w := range rolesFromLevel { for _, w := range config.LevelRoles {
if v == w { if v == w {
s.GuildMemberRoleRemove(config.GuildID, discordID, v) s.GuildMemberRoleRemove(config.GuildID, discordID, v)
break break

@ -15,9 +15,9 @@ func GetOldLevel(s *discordgo.Session, id string) (int, error) {
} }
foundLevel := 0 foundLevel := 0
levelSearchLoop: levelSearchLoop:
for i := len(rolesFromLevel); i > 0; i-- { for i := len(config.LevelRoles); i > 0; i-- {
for _, v := range member.Roles { for _, v := range member.Roles {
if rolesFromLevel[i-1] == v { if config.LevelRoles[i-1] == v {
foundLevel = i foundLevel = i
break levelSearchLoop break levelSearchLoop
} }

@ -1,16 +0,0 @@
package roles
import (
"io/ioutil"
"strings"
)
var rolesFromLevel []string
func init() {
listOfRoles, _ := ioutil.ReadFile("roles.txt")
rolesFromLevel = strings.Split(string(listOfRoles), "\n")
for k, v := range rolesFromLevel {
rolesFromLevel[k] = strings.TrimSpace(v)
}
}