Skip to content

Commit

Permalink
Merge pull request #20 from ccil-kbw/discord-iqama-notification
Browse files Browse the repository at this point in the history
feat(iqama): add notification for iqamas v1
  • Loading branch information
serafdev authored Oct 9, 2023
2 parents 1b8a5bc + 2be5794 commit 4e5fb14
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 38 deletions.
34 changes: 3 additions & 31 deletions cmd/darsrec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,13 @@ package main

import (
"fmt"
"github.com/ccil-kbw/robot/iqama"
"os"
"sync"
"time"

"github.com/ccil-kbw/robot/rec"
)

// AsyncConf (sync.Mutex) is used to handle the RecordConfig asynchronously as it's used in the main and some sub routines
type AsyncConf struct {
mu sync.Mutex
confs []*rec.RecordConfig
}

// Refresh updates the scheduling configurations
func (ac *AsyncConf) Refresh() {
ac.mu.Lock()
ac.confs = rec.GetIqamaRecordingConfigs()
ac.mu.Unlock()
}

func (ac *AsyncConf) Confs() []*rec.RecordConfig {
ac.mu.Lock()
defer ac.mu.Unlock()
return ac.confs
}

func main() {

var host, password string
Expand All @@ -52,16 +33,7 @@ func main() {

defer client.Disconnect()

ac := AsyncConf{}
ac.Refresh()

ticker := time.NewTicker(1 * time.Hour)
go func() {
for range ticker.C {
fmt.Printf("%s: Updating Iqama time...\n", time.Now().Format(time.Kitchen))
ac.Refresh()
}
}()
data := iqama.StartRecordingScheduleServer()

for {

Expand All @@ -70,7 +42,7 @@ func main() {
panic(err)
}

shouldRecord := rec.SupposedToBeRecording(ac.Confs())
shouldRecord := rec.SupposedToBeRecording(data.Confs())

// Start recording if supposed to be recording but currently not recording
if shouldRecord && !isRecording {
Expand Down
2 changes: 1 addition & 1 deletion cmd/discord_bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ var (
func init() { flag.Parse() }

func main() {
go discord.Run(GuildID, BotToken, RemoveCommands, nil)
go discord.Run(GuildID, BotToken, RemoveCommands, nil, make(chan string))
}
54 changes: 51 additions & 3 deletions cmd/monolith/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/ccil-kbw/robot/iqama"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -37,13 +38,60 @@ type Config struct {
Features Features
}

var (
stdHour = "15"
stdMinutes = "4"
)

func main() {
var err error
msgs := make(chan string)
stop := make(chan os.Signal, 1)
notifyChan := make(chan string)

signal.Notify(stop, os.Interrupt)

var prayersData *iqama.PrayersData
{
prayersData = iqama.StartIqamaServer()
}

go func() {
go iqama.StartRecordingScheduleServer()

for {
now := time.Now()
if prayersData.Confs().Fajr.Iqama == now.Format(fmt.Sprintf("%s:%s", stdHour, stdMinutes)) {
fmt.Println("Fajr: Iqama starts Now")
notifyChan <- "Fajr: Iqama starts Now"
}

if prayersData.Confs().Dhuhr.Iqama == now.Format(fmt.Sprintf("%s:%s", stdHour, stdMinutes)) {
fmt.Println("Dhuhur: Iqama Starts Now")
notifyChan <- "Dhuhur: Iqama starts Now"

}

if prayersData.Confs().Asr.Iqama == now.Format(fmt.Sprintf("%s:%s", stdHour, stdMinutes)) {
fmt.Println("Asr: Iqama Starts Now")
notifyChan <- "Asr: Iqama Starts Now"
}

if prayersData.Confs().Maghrib.Iqama == now.Format(fmt.Sprintf("%s:%s", stdHour, stdMinutes)) {
fmt.Println("Maghrib: Iqama Starts Now")
notifyChan <- "Maghrib: Iqama Starts Now"
}

if prayersData.Confs().Isha.Iqama == now.Format(fmt.Sprintf("%s:%s", stdHour, stdMinutes)) {
fmt.Println("Isha: Iqama Starts Now")
notifyChan <- "Isha: Iqama Starts Now"
}

time.Sleep(1 * time.Minute)
}

}()

if config.Features.Proxy {
go proxy()
}
Expand All @@ -60,7 +108,7 @@ func main() {
}

if config.Features.DiscordBot {
go bot(obs)
go bot(obs, notifyChan)
}

out:
Expand Down Expand Up @@ -99,9 +147,9 @@ func proxy() {
_ = http.ListenAndServe(":3333", nil)
}

func bot(obs *rec.Recorder) {
func bot(obs *rec.Recorder, notifyChan chan string) {
guildID := os.Getenv("MDROID_BOT_GUILD_ID")
botToken := os.Getenv("MDROID_BOT_TOKEN")
removeCommands := true
discord.Run(&guildID, &botToken, &removeCommands, obs)
discord.Run(&guildID, &botToken, &removeCommands, obs, notifyChan)
}
14 changes: 13 additions & 1 deletion discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ var (
)

// Run the Discord bot. NOTE: Function can be split
func Run(guildID, botToken *string, removeCommands *bool, obs *rec.Recorder) {
func Run(guildID, botToken *string, removeCommands *bool, obs *rec.Recorder, notifyChan chan string) {
var err error
var s *discordgo.Session

Expand Down Expand Up @@ -183,6 +183,18 @@ func Run(guildID, botToken *string, removeCommands *bool, obs *rec.Recorder) {

defer s.Close()

var channelID string
{
channelID = os.Getenv("MDROID_DISCORD_CHANNEL_ID")
if channelID == "" {
channelID = "1044684995127676958"
}
}
go func() {
for {
s.ChannelMessageSend(channelID, <-notifyChan)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
fmt.Println("Press Ctrl+C to exit")
Expand Down
73 changes: 73 additions & 0 deletions iqama/iqama.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package iqama

import (
"fmt"
v1 "github.com/ccil-kbw/robot/iqama/v1"
"github.com/ccil-kbw/robot/rec"
"sync"
"time"
)

// RecordingsData (sync.Mutex) is used to handle the RecordConfig asynchronously as it's used in the main and some sub routines
type RecordingsData struct {
mu sync.Mutex
confs []*rec.RecordConfig
}

// Refresh updates the scheduling configurations
func (ac *RecordingsData) Refresh() {
ac.mu.Lock()
ac.confs = rec.GetIqamaRecordingConfigs()
ac.mu.Unlock()
}

func (ac *RecordingsData) Confs() []*rec.RecordConfig {
ac.mu.Lock()
defer ac.mu.Unlock()
return ac.confs
}

// PrayersData (sync.Mutex) is used to handle the Iqama times
type PrayersData struct {
mu sync.Mutex
confs *v1.Resp
}

func (i *PrayersData) Refresh() {
i.mu.Lock()
defer i.mu.Unlock()
resp := v1.Get()
i.confs = &resp
}

func (i *PrayersData) Confs() *v1.Resp {
i.mu.Lock()
defer i.mu.Unlock()
return i.confs
}

func StartRecordingScheduleServer() *RecordingsData {
data := &RecordingsData{}
data.Refresh()
ticker := time.NewTicker(1 * time.Hour)
go func() {
for range ticker.C {
fmt.Printf("%s: Updating Iqama time...\n", time.Now().Format(time.Kitchen))
data.Refresh()
}
}()
return data
}

func StartIqamaServer() *PrayersData {
data := &PrayersData{}
data.Refresh()
ticker := time.NewTicker(1 * time.Hour)
go func() {
for range ticker.C {
fmt.Printf("%s: Updating Iqama time...\n", time.Now().Format(time.Kitchen))
data.Refresh()
}
}()
return data
}
2 changes: 1 addition & 1 deletion rec/rec.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (o *Recorder) Disconnect() error {
return o.client.Disconnect()
}

// Rec records if didn't start recording, else continue recording for the specified duration
// StartRecording records if didn't start recording, else continue recording for the specified duration
func (o *Recorder) StartRecording() error {

// Get Record Status
Expand Down
2 changes: 1 addition & 1 deletion rec/rec_iqama.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type RecordConfig struct {
RecordingDays []time.Weekday
}

// SupposedToBeRecording, just what the func name is saying.
// SupposedToBeRecording just what the func name is saying.
// Please add doc wherever you think it was unreadable, else refactor the portion
func SupposedToBeRecording(confs []*RecordConfig) bool {

Expand Down

0 comments on commit 4e5fb14

Please sign in to comment.