Skip to content

Commit

Permalink
PlayingMsg is now rotatable and other tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
apiks committed Sep 21, 2019
1 parent 85cf7ce commit d5d9765
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
84 changes: 79 additions & 5 deletions commands/owner.go
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"math/rand"
"strings"

"github.com/bwmarrin/discordgo"
Expand All @@ -26,7 +27,13 @@ func playingMsgCommand(s *discordgo.Session, m *discordgo.Message) {

// Displays current playing message if it's only that
if len(commandStrings) == 1 {
_, err := s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Current playing message is: `%v` \n\n To change the message please use `%vplayingmsg [new message]`", config.PlayingMsg, guildPrefix))
var playingMsgs string
misc.MapMutex.Lock()
for _, msg := range config.PlayingMsg {
playingMsgs += fmt.Sprintf("\n`%v`,", msg)
}
misc.MapMutex.Unlock()
_, err := s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Current playing messages are: %v \n\nTo add more messages please use `%vplayingmsg [new message]`", playingMsgs, guildPrefix))
if err != nil {
_, err = s.ChannelMessageSend(guildBotLog, err.Error()+"\n"+misc.ErrorLocation(err))
if err != nil {
Expand All @@ -39,7 +46,7 @@ func playingMsgCommand(s *discordgo.Session, m *discordgo.Message) {

// Changes and writes new playing message to storage
misc.MapMutex.Lock()
config.PlayingMsg = commandStrings[1]
config.PlayingMsg = append(config.PlayingMsg, commandStrings[1])
err := config.WriteConfig()
if err != nil {
misc.MapMutex.Unlock()
Expand All @@ -49,13 +56,13 @@ func playingMsgCommand(s *discordgo.Session, m *discordgo.Message) {
misc.MapMutex.Unlock()

// Refreshes playing message
err = s.UpdateStatus(0, config.PlayingMsg)
err = s.UpdateStatus(0, commandStrings[1])
if err != nil {
misc.CommandErrorHandler(s, m, err, guildBotLog)
return
}

_, err = s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Success! New playing message is: `%v`", config.PlayingMsg))
_, err = s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Success! New playing message added is: `%v`", commandStrings[1]))
if err != nil {
_, err = s.ChannelMessageSend(guildBotLog, err.Error()+"\n"+misc.ErrorLocation(err))
if err != nil {
Expand All @@ -65,6 +72,65 @@ func playingMsgCommand(s *discordgo.Session, m *discordgo.Message) {
}
}

// Handles removing a playing message
func removePlayingMsgCommand(s *discordgo.Session, m *discordgo.Message) {

if m.Author.ID != config.OwnerID {
return
}

misc.MapMutex.Lock()
guildPrefix := misc.GuildMap[m.GuildID].GuildConfig.Prefix
guildBotLog := misc.GuildMap[m.GuildID].GuildConfig.BotLog.ID
misc.MapMutex.Unlock()

commandStrings := strings.SplitN(m.Content, " ", 2)

if len(commandStrings) == 1 {
_, err := s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Usage: `%vremoveplayingmsg [msg]`", guildPrefix))
if err != nil {
_, err = s.ChannelMessageSend(guildBotLog, err.Error()+"\n"+misc.ErrorLocation(err))
if err != nil {
return
}
return
}
return
}

// Changes and removes playing msg from storage
misc.MapMutex.Lock()
var index int
for i, msg := range config.PlayingMsg {
if msg == commandStrings[1] {
index = i
break
}
}
config.PlayingMsg = append(config.PlayingMsg[:index], config.PlayingMsg[index+1:]...)
err := config.WriteConfig()
if err != nil {
misc.MapMutex.Unlock()
misc.CommandErrorHandler(s, m, err, guildBotLog)
return
}

// Refreshes playing message
randMsg := rand.Intn(len(config.PlayingMsg)-1)
err = s.UpdateStatus(0, config.PlayingMsg[randMsg])
if err != nil {
misc.MapMutex.Unlock()
misc.CommandErrorHandler(s, m, err, guildBotLog)
return
}
misc.MapMutex.Unlock()

_, err = s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Success! Removed playing message: `%v`", commandStrings[1]))
if err != nil {
_, _ = s.ChannelMessageSend(guildBotLog, err.Error()+"\n"+misc.ErrorLocation(err))
}
}

// Prints in how many servers the BOT is
func serversCommand(s *discordgo.Session, m *discordgo.Message) {

Expand Down Expand Up @@ -107,7 +173,15 @@ func init() {
add(&command{
execute: playingMsgCommand,
trigger: "playingmsg",
desc: "Prints or changes the current BOT playing message.",
desc: "Prints or adds a BOT playing message.",
elevated: true,
admin: true,
})
add(&command{
execute: removePlayingMsgCommand,
trigger: "removeplayingmsg",
aliases: []string{"killplayingmsg"},
desc: "Removes a BOT playing message.",
elevated: true,
admin: true,
})
Expand Down
1 change: 1 addition & 0 deletions commands/schedule.go
Expand Up @@ -297,6 +297,7 @@ func init() {
add(&command{
execute: scheduleCommand,
trigger: "schedule",
aliases: []string{"schedul", "schedu", "schedle", "schdule", "animeschedule", "anischedule"},
desc: "Print anime air times SUBBED. Add a day to specify a day",
category: "normal",
DMAble: true,
Expand Down
2 changes: 1 addition & 1 deletion commands/stats.go
Expand Up @@ -518,7 +518,7 @@ func init() {
add(&command{
execute: showStats,
trigger: "stats",
aliases: []string{"channelstats", "channels"},
aliases: []string{"channelstats", "channels", "stat", "chanstat", "chanstats", "statss"},
desc: "Prints all channel stats.",
elevated: true,
category: "stats",
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Expand Up @@ -16,7 +16,7 @@ var (
BotLogID string
OwnerID string
Website string
PlayingMsg string
PlayingMsg []string
RedditAppName string
RedditAppSecret string
DiscordAppSecret string
Expand All @@ -38,7 +38,7 @@ type configStruct struct {
VoteChannelCategoryID string `json:"VoteChannelCategoryID"`
Kaguya string `json:"Kaguya"`
MsgAttachRemoval string `json:"MsgAttachRemoval"`
PlayingMsg string `json:"PlayingMsg"`
PlayingMsg []string `json:"PlayingMsg"`
}

type configSecrets struct {
Expand Down
13 changes: 12 additions & 1 deletion misc/events.go
Expand Up @@ -43,7 +43,11 @@ func StatusReady(s *discordgo.Session, e *discordgo.Ready) {
}

// Update playing status
_ = s.UpdateStatus(0, config.PlayingMsg)
MapMutex.Lock()
rand.Seed(time.Now().UnixNano())
randInt := rand.Intn(len(config.PlayingMsg))
_ = s.UpdateStatus(0, config.PlayingMsg[randInt])
MapMutex.Unlock()

// Sends server count to bot list sites if it's the public ZeroTsu
sendServers(s)
Expand Down Expand Up @@ -166,6 +170,13 @@ func UnbanEmbed(s *discordgo.Session, user *UserInfo, mod string, botLog string)
func TwentyMinTimer(s *discordgo.Session, e *discordgo.Ready) {
for range time.NewTicker(20 * time.Minute).C {

// Update playing status
MapMutex.Lock()
rand.Seed(time.Now().UnixNano())
randInt := rand.Intn(len(config.PlayingMsg))
_ = s.UpdateStatus(0, config.PlayingMsg[randInt])
MapMutex.Unlock()

MapMutex.Lock()
for _, guild := range e.Guilds {

Expand Down

0 comments on commit d5d9765

Please sign in to comment.