Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
c0re100 committed Mar 4, 2021
1 parent 69920cb commit 59e1482
Show file tree
Hide file tree
Showing 30 changed files with 2,294 additions and 0 deletions.
30 changes: 30 additions & 0 deletions config.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"api_id": "api_id_here",
"api_hash": "api_hash_here",
"bot_token": "bot_token_here",
"chat_id": 0,
"chat_username": "",
"pinned_message": 0,
"playlist_id": "p0",
"limit": {
"chat_select_limit": 5,
"private_select_limit": 10,
"row_limit": 10,
"queue_limit": 50,
"recent_limit": 50,
"request_song_per_minute": 1
},
"vote": {
"enable": true,
"vote_time": 45,
"update_time": 15,
"release_time": 600,
"percent_of_success": 40,
"participants_only": true,
"user_must_join": false
},
"web": {
"enable": true,
"web_port": 2468
}
}
97 changes: 97 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package config

func GetConfig() Config {
return config
}

func GetApiId() string {
return config.ApiId
}

func GetApiHash() string {
return config.ApiHash
}

func GetBotToken() string {
return config.BotToken
}

func GetChatId() int64 {
return config.ChatId
}

func SetChatId(value int64) {
config.ChatId = value
}

func GetChatUsername() string {
return config.ChatUsername
}

func GetPinnedMessage() int64 {
return config.PinnedMsg << 20
}

func GetPlaylistId() string {
return config.PlaylistId
}

func GetChatSelectLimit() int {
return config.LimitSetting.ChatSelectLimit
}

func GetPrivateChatSelectLimit() int {
return config.LimitSetting.PriSelectLimit
}

func GetRowLimit() int {
return config.LimitSetting.RowLimit
}

func GetQueueLimit() int {
return config.LimitSetting.QueueLimit
}

func GetRecentLimit() int {
return config.LimitSetting.RecentLimit
}

func GetReqSongLimit() int {
return config.LimitSetting.ReqSongPerMin
}

func IsVoteEnabled() bool {
return config.VoteSetting.Enable
}

func GetSuccessRate() float64 {
return config.VoteSetting.PctOfSuccess
}

func IsPtcpsOnly() bool {
return config.VoteSetting.PtcpsOnly
}

func GetVoteTime() int32 {
return config.VoteSetting.VoteTime
}

func GetReleaseTime() int64 {
return config.VoteSetting.ReleaseTime
}

func GetUpdateTime() int32 {
return config.VoteSetting.UpdateTime
}

func IsJoinNeeded() bool {
return config.VoteSetting.UserMustJoin
}

func IsWebEnabled() bool {
return config.WebSetting.Enable
}

func GetWebPort() int {
return config.WebSetting.Port
}
63 changes: 63 additions & 0 deletions config/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package config

import (
"encoding/json"
"io/ioutil"
"log"
"os"
"time"

"github.com/go-co-op/gocron"
)

var (
status Status
config Config
cron = gocron.NewScheduler(time.UTC)
)

func Read() {
ReadConfig()
ReadStatus()
}

func ReadConfig() {
if err := LoadConfig(); err != nil {
log.Fatal(err)
}
}

func LoadConfig() error {
b, err := ioutil.ReadFile("config.json")
if err != nil {
return err
}
e := json.Unmarshal(b, &config)
if e != nil {
return e
}
return nil
}

func initStatus() {
b := []byte("{}")
os.WriteFile("status.json", b, 0755)
json.Unmarshal(b, &status)
}

func ReadStatus() {
if b, err := ioutil.ReadFile("status.json"); err == nil {
e := json.Unmarshal(b, &status)
if e != nil {
log.Println("status.json is broken...resetting")
initStatus()
}
} else {
log.Println("status.json not found...initializating")
initStatus()
}
cron.Every(1).Minute().Do(func() {
SaveStatus()
})
cron.StartAsync()
}
30 changes: 30 additions & 0 deletions config/save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"encoding/json"
"log"
"os"
)

func Save() {
SaveStatus()
SaveConfig()
}

func SaveStatus() {
b, err := json.MarshalIndent(status, "", " ")
if err != nil {
log.Println("Failed to save status...")
return
}
os.WriteFile("status.json", b, 0755)
}

func SaveConfig() {
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
log.Println("Failed to save config...")
return
}
os.WriteFile("config.json", b, 0755)
}
29 changes: 29 additions & 0 deletions config/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

func GetStatus() Status {
return status
}

func (s Status) GetCurrent() string {
return s.Current
}

func SetCurrentSong(value string) {
status.Current = value
}

func (s Status) GetQueue() []int {
return s.Queue
}

func SetQueueSong(value []int) {
status.Queue = value
}

func (s Status) GetRecent() []int {
return s.Recent
}

func SetRecentSong(value []int) {
status.Recent = value
}
44 changes: 44 additions & 0 deletions config/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

type Status struct {
Current string `json:"current"`
Queue []int `json:"queue"`
Recent []int `json:"recent"`
}

type Config struct {
ApiId string `json:"api_id"`
ApiHash string `json:"api_hash"`
BotToken string `json:"bot_token"`
ChatId int64 `json:"chat_id"`
ChatUsername string `json:"chat_username"`
PinnedMsg int64 `json:"pinned_message"`
PlaylistId string `json:"playlist_id"`
LimitSetting Limit `json:"limit"`
VoteSetting Vote `json:"vote"`
WebSetting Web `json:"web"`
}

type Limit struct {
ChatSelectLimit int `json:"chat_select_limit"`
PriSelectLimit int `json:"private_select_limit"`
RowLimit int `json:"row_limit"`
QueueLimit int `json:"queue_limit"`
RecentLimit int `json:"recent_limit"`
ReqSongPerMin int `json:"request_song_per_minute"`
}

type Vote struct {
Enable bool `json:"enable"`
VoteTime int32 `json:"vote_time"`
UpdateTime int32 `json:"update_time"`
ReleaseTime int64 `json:"release_time"`
PctOfSuccess float64 `json:"percent_of_success"`
PtcpsOnly bool `json:"participants_only"`
UserMustJoin bool `json:"user_must_join"`
}

type Web struct {
Enable bool `json:"enable"`
Port int `json:"port"`
}
Loading

0 comments on commit 59e1482

Please sign in to comment.