Add setup method to execute actions after connect

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-07 15:24:11 +02:00
parent 4f07b52d62
commit da426e7d68
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
8 changed files with 32 additions and 6 deletions

20
main.go
View file

@ -47,9 +47,10 @@ func init() {
func main() { func main() {
var ( var (
crontab = cron.New() crontab = cron.New()
discord *discordgo.Session discord *discordgo.Session
err error err error
activeModules []module
) )
if config, err = newConfigFromFile(cfg.Config); err != nil { if config, err = newConfigFromFile(cfg.Config); err != nil {
@ -63,7 +64,6 @@ func main() {
discord.Identify.Intents = discordgo.IntentsAll discord.Identify.Intents = discordgo.IntentsAll
var enabledModules int
for i, mc := range config.ModuleConfigs { for i, mc := range config.ModuleConfigs {
logger := log.WithFields(log.Fields{ logger := log.WithFields(log.Fields{
"idx": i, "idx": i,
@ -78,11 +78,12 @@ func main() {
logger.WithError(err).Fatal("Unable to initialize module") logger.WithError(err).Fatal("Unable to initialize module")
} }
enabledModules++ activeModules = append(activeModules, mod)
logger.Debug("Enabled module") logger.Debug("Enabled module")
} }
if enabledModules == 0 { if len(activeModules) == 0 {
log.Warn("No modules were enabled, quitting now") log.Warn("No modules were enabled, quitting now")
return return
} }
@ -98,6 +99,13 @@ func main() {
defer crontab.Stop() defer crontab.Stop()
log.Debug("Crontab started") log.Debug("Crontab started")
// Execute Setup methods now after we're connected
for i, mod := range activeModules {
if err = mod.Setup(); err != nil {
log.WithError(err).WithField("idx", i).Fatal("Unable to run setup for module")
}
}
// Run HTTP server // Run HTTP server
var h http.Handler = http.DefaultServeMux var h http.Handler = http.DefaultServeMux
h = httpHelpers.GzipHandler(h) h = httpHelpers.GzipHandler(h)

View file

@ -49,6 +49,8 @@ func (m *modClearChannel) Initialize(crontab *cron.Cron, discord *discordgo.Sess
return nil return nil
} }
func (m modClearChannel) Setup() error { return nil }
func (m modClearChannel) cronClearChannel() { func (m modClearChannel) cronClearChannel() {
var ( var (
after = "0" after = "0"

View file

@ -66,6 +66,8 @@ func (m *modLivePosting) Initialize(crontab *cron.Cron, discord *discordgo.Sessi
return nil return nil
} }
func (m modLivePosting) Setup() error { return nil }
func (m modLivePosting) cronFetchChannelStatus() { func (m modLivePosting) cronFetchChannelStatus() {
// @attr poll_usernames optional []string "[]" Check these usernames for active streams when executing the `cron` (at most 100 users can be checked) // @attr poll_usernames optional []string "[]" Check these usernames for active streams when executing the `cron` (at most 100 users can be checked)
usernames, err := m.attrs.StringSlice("poll_usernames") usernames, err := m.attrs.StringSlice("poll_usernames")

View file

@ -43,6 +43,8 @@ func (m *modLiveRole) Initialize(crontab *cron.Cron, discord *discordgo.Session,
return nil return nil
} }
func (m modLiveRole) Setup() error { return nil }
func (m modLiveRole) addLiveStreamerRole(guildID, userID string, presentRoles []string) error { func (m modLiveRole) addLiveStreamerRole(guildID, userID string, presentRoles []string) error {
// @attr role_streamers_live required string "" Role ID to assign to live streamers // @attr role_streamers_live required string "" Role ID to assign to live streamers
roleID := m.attrs.MustString("role_streamers_live", nil) roleID := m.attrs.MustString("role_streamers_live", nil)

View file

@ -51,6 +51,8 @@ func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session,
return nil return nil
} }
func (m modPresence) Setup() error { return nil }
func (m modPresence) cronUpdatePresence() { func (m modPresence) cronUpdatePresence() {
var nextStream *time.Time var nextStream *time.Time

View file

@ -58,6 +58,8 @@ func (m *modStreamSchedule) Initialize(crontab *cron.Cron, discord *discordgo.Se
return nil return nil
} }
func (m modStreamSchedule) Setup() error { return nil }
func (m modStreamSchedule) cronUpdateSchedule() { func (m modStreamSchedule) cronUpdateSchedule() {
twitch := newTwitchAdapter( twitch := newTwitchAdapter(
// @attr twitch_client_id required string "" Twitch client ID the token was issued for // @attr twitch_client_id required string "" Twitch client ID the token was issued for

View file

@ -16,6 +16,7 @@ var (
type ( type (
module interface { module interface {
Initialize(crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error Initialize(crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error
Setup() error
} }
moduleInitFn func() module moduleInitFn func() module

View file

@ -105,6 +105,13 @@ Updates the presence status of the bot to display the next stream
| `cron` | | string | `* * * * *` | When to execute the module | | `cron` | | string | `* * * * *` | When to execute the module |
| `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry | | `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry |
## Type: `reactionrole`
Creates a post with pre-set reactions and assigns roles on reaction
| Attribute | Req. | Type | Default Value | Description |
| --------- | :--: | ---- | ------------- | ----------- |
## Type: `schedule` ## Type: `schedule`
Posts stream schedule derived from Twitch schedule as embed in Discord channel Posts stream schedule derived from Twitch schedule as embed in Discord channel