Skip to content

Commit

Permalink
Merge pull request #543 from cheesegrits/discord-fixes
Browse files Browse the repository at this point in the history
Discord enhancements
  • Loading branch information
cj123 authored Oct 25, 2019
2 parents 276fb3c + e1f54f1 commit be2f43d
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 159 deletions.
44 changes: 19 additions & 25 deletions championship_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,6 @@ func (cm *ChampionshipManager) ScheduleEvent(championshipID string, eventID stri
return err
}

serverOpts, err := cm.store.LoadServerOptions()

if err != nil {
return err
}

event.Scheduled = date

// if there is an existing schedule timer for this event stop it
Expand Down Expand Up @@ -648,12 +642,15 @@ func (cm *ChampionshipManager) ScheduleEvent(championshipID string, eventID stri
}
})

if serverOpts.NotificationReminderTimer > 0 {
duration = time.Until(date.Add(time.Duration(0-serverOpts.NotificationReminderTimer) * time.Minute))
if cm.notificationManager.HasNotificationReminders() {
for _, timer := range cm.notificationManager.GetNotificationReminders() {
duration = time.Until(date.Add(time.Duration(0-timer) * time.Minute))
thisTimer := timer

cm.championshipEventReminderTimers[event.ID.String()] = time.AfterFunc(duration, func() {
cm.notificationManager.SendChampionshipReminderMessage(championship, event)
})
cm.championshipEventReminderTimers[event.ID.String()] = time.AfterFunc(duration, func() {
cm.notificationManager.SendChampionshipReminderMessage(championship, event, thisTimer)
})
}
}
} else {
event.ClearRecurrenceRule()
Expand Down Expand Up @@ -1472,12 +1469,6 @@ func (cm *ChampionshipManager) InitScheduledChampionships() error {
return err
}

serverOpts, err := cm.store.LoadServerOptions()

if err != nil {
return err
}

for _, championship := range championships {
championship := championship

Expand All @@ -1496,14 +1487,17 @@ func (cm *ChampionshipManager) InitScheduledChampionships() error {
}
})

if serverOpts.NotificationReminderTimer > 0 {
if event.Scheduled.Add(time.Duration(0-serverOpts.NotificationReminderTimer) * time.Minute).After(time.Now()) {
// add reminder
duration = time.Until(event.Scheduled.Add(time.Duration(0-serverOpts.NotificationReminderTimer) * time.Minute))

cm.championshipEventReminderTimers[event.ID.String()] = time.AfterFunc(duration, func() {
cm.notificationManager.SendChampionshipReminderMessage(championship, event)
})
if cm.notificationManager.HasNotificationReminders() {
for _, timer := range cm.notificationManager.GetNotificationReminders() {
if event.Scheduled.Add(time.Duration(0-timer) * time.Minute).After(time.Now()) {
// add reminder
duration = time.Until(event.Scheduled.Add(time.Duration(0-timer) * time.Minute))
thisTimer := timer

cm.championshipEventReminderTimers[event.ID.String()] = time.AfterFunc(duration, func() {
cm.notificationManager.SendChampionshipReminderMessage(championship, event, thisTimer)
})
}
}
}

Expand Down
20 changes: 17 additions & 3 deletions championship_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ var championshipManager *ChampionshipManager

type dummyNotificationManager struct{}

func (d dummyNotificationManager) SendRaceWeekendReminderMessage(raceWeekend *RaceWeekend, session *RaceWeekendSession) error {
func (d *dummyNotificationManager) HasNotificationReminders() bool {
return false
}

func (d *dummyNotificationManager) GetNotificationReminders() []int {
var reminders []int

return reminders
}

func (d dummyNotificationManager) SendRaceWeekendReminderMessage(raceWeekend *RaceWeekend, session *RaceWeekendSession, timer int) error {
return nil
}

Expand All @@ -131,11 +141,15 @@ func (d dummyNotificationManager) SendRaceScheduledMessage(event *CustomRace, da
return nil
}

func (d dummyNotificationManager) SendRaceReminderMessage(event *CustomRace) error {
func (d dummyNotificationManager) SendRaceCancelledMessage(event *CustomRace, date time.Time) error {
return nil
}

func (d dummyNotificationManager) SendRaceReminderMessage(event *CustomRace, timer int) error {
return nil
}

func (d dummyNotificationManager) SendChampionshipReminderMessage(championship *Championship, event *ChampionshipEvent) error {
func (d dummyNotificationManager) SendChampionshipReminderMessage(championship *Championship, event *ChampionshipEvent, timer int) error {
return nil
}

Expand Down
15 changes: 10 additions & 5 deletions config_ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,16 @@ type GlobalServerConfig struct {
UseMPH int `ini:"-" input:"checkbox" help:"When on, this option will make Server Manager use MPH instead of Km/h for all speed values."`

// Discord Integration
DiscordIntegration FormHeading `ini:"-" input:"heading"`
DiscordAPIToken string `ini:"-" help:"If set, will enable race start and scheduled reminder messages to the Discord channel ID specified below. Use your bot's user token, not the OAuth token."`
DiscordChannelID string `ini:"-" help:"If Discord is enabled, this is the channel ID it will send messages to"`
NotificationReminderTimer int `ini:"-" min:"0" max:"65535" help:"If Discord is enabled, a reminder will be sent this many minutes prior to race start. If 0, only race start messages will be sent."`
ShowPasswordInNotifications int `ini:"-" show:"open" input:"checkbox" help:"Show the server password in race start notifications"`
DiscordIntegration FormHeading `ini:"-" input:"heading"`
DiscordAPIToken string `ini:"-" help:"If set, will enable race start and scheduled reminder messages to the Discord channel ID specified below. Use your bot's user token, not the OAuth token."`
DiscordChannelID string `ini:"-" help:"If Discord is enabled, this is the channel ID it will send messages to. To find the channel ID, enable Developer mode in Discord (user settings, Appearance), then Server Settings, Roles, and right click on the channel and Copy ID."`
DiscordRoleID string `ini:"-" help:"If set, this role will be mentioned in all Discord notifications. Any users with this role and access to the channel will be pinged. To find the role ID, enable Developer mode (see above)), then Server Settings, Roles, right click on the role and Copy ID."`
DiscordRoleCommand string `ini:"-" help:"If the Discord Role ID is set, you can optionally specify a command string here, like \"notify\" (no ! prefix), which if run as a ! command by a user (on a line by itself) in Discord will cause this server to attempt to add the configured role to the user. If you run multiple servers with Discord enabled, only set this on one of them. In order for this to work your bot must have the \"Manage Roles\" permission."`

NotificationReminderTimer int `ini:"-" show:"-" min:"0" max:"65535" help:"This setting has been deprecated and will be removed in the next release. Use Notification Reminder Timers instead."`
NotificationReminderTimers string `ini:"-" help:"If Discord is enabled, a reminder will be sent this many minutes prior to race start. If 0 or empty, only race start messages will be sent. You may schedule multiple reminders by using a comma separated list like 120,15."`
ShowPasswordInNotifications int `ini:"-" input:"checkbox" help:"Show the server password in race start notifications."`
NotifyWhenScheduled int `ini:"-" input:"checkbox" help:"Send a notification when a race is scheduled (or cancelled)."`

// Messages
ContentManagerWelcomeMessage string `ini:"-" show:"-"`
Expand Down
Loading

0 comments on commit be2f43d

Please sign in to comment.