Save and load Now Playing status from a file

pull/21/head
pocketjawa 2020-10-10 15:34:17 -04:00
parent 0986fe83e3
commit 0b21bf0bb3
2 changed files with 29 additions and 1 deletions

View File

@ -147,6 +147,7 @@ func goToSleep(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
//Set now playing status
func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
if !checkRole(s, m, ctx, bot_admin_role) {
resp := "OwO you aren't my daddy..."
@ -158,6 +159,23 @@ func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context)
arg := strings.Join(ctx.Fields[1:], " ")
s.UpdateStatus(0, arg)
resp = "Now playing: " + arg
f, err := os.Create("userdata/nowplaying.txt")
if err != nil {
log.Println(err)
return
}
_, err = f.WriteString(arg)
if err != nil {
log.Println(err)
f.Close()
return
}
log.Println("Updated Now Playing to", arg, "at the request of", m.Author.String())
err = f.Close()
if err != nil {
log.Println(err)
return
}
} else {
resp = "Please tell me what to play!"
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/disgord/x/mux"
"io/ioutil"
"log"
"math/rand"
"os"
@ -89,7 +90,16 @@ func main() {
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
s.UpdateStatus(0, "with droids!")
playingstatus := "with droids!"
_, err := os.Stat("userdata/nowplaying.txt")
if !os.IsNotExist(err) {
content, err := ioutil.ReadFile("userdata/nowplaying.txt")
if err != nil {
log.Fatal(err)
}
playingstatus = string(content)
}
s.UpdateStatus(0, playingstatus)
if os.Getenv("POCKETBOT_STARTUP_MESSAGE") == "TRUE" {
s.ChannelMessageSend(bot_channel, "This isn't Tatooine...")
}