Skip to content

Commit

Permalink
golint
Browse files Browse the repository at this point in the history
  • Loading branch information
c0re100 committed Mar 5, 2021
1 parent 7e4ba34 commit 64474ec
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 191 deletions.
38 changes: 32 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,131 @@
package config

// GetConfig get current config
func GetConfig() Config {
return config
}

func GetApiId() string {
// GetAPIID get API ID
func GetAPIID() string {
return config.ApiId
}

func GetApiHash() string {
// GetAPIHash get API Hash
func GetAPIHash() string {
return config.ApiHash
}

// GetBotToken get bot token
func GetBotToken() string {
return config.BotToken
}

func GetChatId() int64 {
// GetChatID get chat ID
func GetChatID() int64 {
return config.ChatId
}

func SetChatId(value int64) {
// SetChatID update chat ID
func SetChatID(value int64) {
config.ChatId = value
}

// GetChatUsername get chat username
func GetChatUsername() string {
return config.ChatUsername
}

// GetPinnedMessage get pinned message ID
func GetPinnedMessage() int64 {
return config.PinnedMsg << 20
}

// SetPinnedMessage update pinned message ID
func SetPinnedMessage(value int64) {
config.PinnedMsg = value >> 20
}

// GetBeefWebPort get Beefweb port
func GetBeefWebPort() int {
return config.BeefWebPort
return config.BeefwebPort
}

func GetPlaylistId() string {
// GetPlaylistID get playlist ID
func GetPlaylistID() string {
return config.PlaylistId
}

// GetChatSelectLimit get select limit of group chat
func GetChatSelectLimit() int {
return config.LimitSetting.ChatSelectLimit
}

// GetPrivateChatSelectLimit get select limit of private chat
func GetPrivateChatSelectLimit() int {
return config.LimitSetting.PriSelectLimit
}

// GetRowLimit get row limit
func GetRowLimit() int {
return config.LimitSetting.RowLimit
}

// GetQueueLimit get queue song limit
func GetQueueLimit() int {
return config.LimitSetting.QueueLimit
}

// GetRecentLimit get recent song limit
func GetRecentLimit() int {
return config.LimitSetting.RecentLimit
}

// GetReqSongLimit get request song limit
func GetReqSongLimit() int {
return config.LimitSetting.ReqSongPerMin
}

// IsVoteEnabled return true if vote is enabled
func IsVoteEnabled() bool {
return config.VoteSetting.Enable
}

// GetSuccessRate get vote success rate
func GetSuccessRate() float64 {
return config.VoteSetting.PctOfSuccess
}

// IsPtcpsOnly return true if only participants which are in a voice chat can vote
func IsPtcpsOnly() bool {
return config.VoteSetting.PtcpsOnly
}

// GetVoteTime get vote time
func GetVoteTime() int32 {
return config.VoteSetting.VoteTime
}

// GetReleaseTime get lock the vote seconds after vote ended
func GetReleaseTime() int64 {
return config.VoteSetting.ReleaseTime
}

// GetUpdateTime get vote update time
func GetUpdateTime() int32 {
return config.VoteSetting.UpdateTime
}

// IsJoinNeeded return true if only users which are in the group can vote
func IsJoinNeeded() bool {
return config.VoteSetting.UserMustJoin
}

// IsWebEnabled return true if userbot mode is disabled
func IsWebEnabled() bool {
return config.WebSetting.Enable
}

// GetWebPort get web port
func GetWebPort() int {
return config.WebSetting.Port
}
10 changes: 6 additions & 4 deletions config/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ var (
cron = gocron.NewScheduler(time.UTC)
)

// Read read config and status JSON
func Read() {
ReadConfig()
ReadStatus()
readConfig()
readStatus()
}

func ReadConfig() {
func readConfig() {
if err := LoadConfig(); err != nil {
log.Fatal(err)
}
Expand All @@ -33,6 +34,7 @@ func ReadConfig() {
utils.CheckPortIsValid("Beefweb", port)
}

// LoadConfig load config.json to Config
func LoadConfig() error {
b, err := ioutil.ReadFile("config.json")
if err != nil {
Expand All @@ -51,7 +53,7 @@ func initStatus() {
json.Unmarshal(b, &status)
}

func ReadStatus() {
func readStatus() {
if b, err := ioutil.ReadFile("status.json"); err == nil {
e := json.Unmarshal(b, &status)
if e != nil {
Expand Down
3 changes: 3 additions & 0 deletions config/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"os"
)

// Save save current status and config to file
func Save() {
SaveStatus()
SaveConfig()
}

// SaveStatus save and indent Status to status.json
func SaveStatus() {
b, err := json.MarshalIndent(status, "", " ")
if err != nil {
Expand All @@ -20,6 +22,7 @@ func SaveStatus() {
os.WriteFile("status.json", b, 0755)
}

// SaveConfig save and indent Config to config.json
func SaveConfig() {
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions config/status.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package config

// GetStatus get current status
func GetStatus() Status {
return status
}

// GetCurrent get current song
func (s Status) GetCurrent() string {
return s.Current
}

// SetCurrentSong set current song
func SetCurrentSong(value string) {
status.Current = value
}

// GetQueue get queue song list
func (s Status) GetQueue() []int {
return s.Queue
}

// SetQueueSong set queue song list
func SetQueueSong(value []int) {
status.Queue = value
}

// GetRecent get recent song list
func (s Status) GetRecent() []int {
return s.Recent
}

// SetRecentSong set recent song list
func SetRecentSong(value []int) {
status.Recent = value
}
7 changes: 6 additions & 1 deletion config/struct.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package config

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

// Config config JSON strust
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"`
BeefWebPort int `json:"beefweb_port"`
BeefwebPort int `json:"beefweb_port"`
PlaylistId string `json:"playlist_id"`
LimitSetting Limit `json:"limit"`
VoteSetting Vote `json:"vote"`
WebSetting Web `json:"web"`
}

// Limit limit setting JSON struct
type Limit struct {
ChatSelectLimit int `json:"chat_select_limit"`
PriSelectLimit int `json:"private_select_limit"`
Expand All @@ -29,6 +32,7 @@ type Limit struct {
ReqSongPerMin int `json:"request_song_per_minute"`
}

// Vote vote setting JSON struct
type Vote struct {
Enable bool `json:"enable"`
VoteTime int32 `json:"vote_time"`
Expand All @@ -39,6 +43,7 @@ type Vote struct {
UserMustJoin bool `json:"user_must_join"`
}

// Web web setting JSON struct
type Web struct {
Enable bool `json:"enable"`
Port int `json:"port"`
Expand Down
14 changes: 12 additions & 2 deletions fb2k/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (

var (
bot *tdlib.Client
// SongQueue song queue channel
SongQueue = make(chan int, 100)
)

// New create music player event monitor
func New(client *tdlib.Client) {
bot = client
restoreQueue()
Expand All @@ -28,30 +30,36 @@ func restoreQueue() {
}
}

// Play start play a song
func Play() {
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/play", "", nil)
}

// PlayNext play next song
func PlayNext() {
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/next", "", nil)
}

// PlayRandom play random song
func PlayRandom() {
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/play/random", "", nil)
}

// Stop stop music player
func Stop() {
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/stop", "", nil)
}

// Pause pause current song
func Pause() {
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/pause", "", nil)
}

// PlaySelected play selected song
func PlaySelected(selectedIdx int) {
rx.Lock()
defer rx.Unlock()
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/play/"+config.GetPlaylistId()+"/"+strconv.Itoa(selectedIdx), "", nil)
http.Post("http://127.0.0.1:"+strconv.Itoa(config.GetBeefWebPort())+"/api/player/play/"+config.GetPlaylistID()+"/"+strconv.Itoa(selectedIdx), "", nil)

rList := config.GetStatus().GetRecent()
qList := config.GetStatus().GetQueue()
Expand All @@ -69,6 +77,7 @@ func PlaySelected(selectedIdx int) {
config.SaveStatus()
}

// PushQueue push selected song to queue channel
func PushQueue(selectedIdx int) {
rx.Lock()
defer rx.Unlock()
Expand All @@ -94,11 +103,12 @@ func checkNextSong() {
PlaySelected(next)
}

// SetKillSwitch set kill switch to kill goroutine
func SetKillSwitch() {
killSwitch <- true
}

func getGoId() int {
func getGoID() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
Expand Down
Loading

0 comments on commit 64474ec

Please sign in to comment.