generated from lazybytez/general-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from lazybytez/feature/bot-status
Add bot status management
- Loading branch information
Showing
8 changed files
with
358 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* JOJO Discord Bot - An advanced multi-purpose discord bot | ||
* Copyright (C) 2022 Lazy Bytez (Elias Knodel, Pascal Zarrad) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// botStatusManager is the DiscordGoStatusManager instance used across the bots' lifetime. | ||
var botStatusManager *DiscordGoStatusManager | ||
|
||
// DiscordGoStatusManager holds the available status of the bot | ||
// and manages the cycling of these. | ||
type DiscordGoStatusManager struct { | ||
mu sync.RWMutex | ||
status []SimpleBotStatus | ||
current int | ||
} | ||
|
||
// StatusManager manages the available status | ||
// which are set fopr the bot. | ||
type StatusManager interface { | ||
// AddStatusToRotation adds the given status to the list of | ||
// rotated status. | ||
AddStatusToRotation(status SimpleBotStatus) | ||
// Next works like next on an iterator which self resets automatically. | ||
Next() *SimpleBotStatus | ||
} | ||
|
||
// BotStatusManager returns the current StatusManager which | ||
// allows to add additional status to the bot. | ||
func (c *Component) BotStatusManager() StatusManager { | ||
return botStatusManager | ||
} | ||
|
||
func init() { | ||
botStatusManager = &DiscordGoStatusManager{ | ||
mu: sync.RWMutex{}, | ||
status: make([]SimpleBotStatus, 0), | ||
current: 0, | ||
} | ||
} | ||
|
||
// AddStatusToRotation adds the given status to the list of | ||
// rotated status. | ||
func (dgsm *DiscordGoStatusManager) AddStatusToRotation(status SimpleBotStatus) { | ||
dgsm.mu.Lock() | ||
defer dgsm.mu.Unlock() | ||
|
||
dgsm.status = append(dgsm.status, status) | ||
} | ||
|
||
// Next works like next on an iterator which self resets automatically. | ||
func (dgsm *DiscordGoStatusManager) Next() *SimpleBotStatus { | ||
dgsm.mu.Lock() | ||
defer dgsm.mu.Unlock() | ||
|
||
statusCount := len(dgsm.status) | ||
if 0 == statusCount { | ||
return nil | ||
} | ||
|
||
if dgsm.current >= statusCount { | ||
dgsm.current = 0 | ||
} | ||
|
||
status := &dgsm.status[dgsm.current] | ||
|
||
dgsm.current = dgsm.current + 1 | ||
|
||
return status | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* JOJO Discord Bot - An advanced multi-purpose discord bot | ||
* Copyright (C) 2022 Lazy Bytez (Elias Knodel, Pascal Zarrad) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/stretchr/testify/suite" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
type StatusManagerSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *StatusManagerSuite) SetupTest() { | ||
botStatusManager = &DiscordGoStatusManager{ | ||
mu: sync.RWMutex{}, | ||
status: make([]SimpleBotStatus, 0), | ||
current: 0, | ||
} | ||
} | ||
|
||
func (suite *StatusManagerSuite) TestNextWithNoStatus() { | ||
for i := 0; i < 10; i++ { | ||
suite.Nil(botStatusManager.Next()) | ||
} | ||
} | ||
|
||
func (suite *StatusManagerSuite) TestNextWithOneStatus() { | ||
firstStatus := SimpleBotStatus{ | ||
ActivityType: discordgo.ActivityTypeGame, | ||
Content: "Test", | ||
} | ||
|
||
botStatusManager.AddStatusToRotation(firstStatus) | ||
|
||
// Cycle five times | ||
for i := 0; i < 5; i++ { | ||
suite.Equal(firstStatus, *botStatusManager.Next()) | ||
} | ||
} | ||
|
||
func (suite *StatusManagerSuite) TestNextWithMultipleStatus() { | ||
firstStatus := SimpleBotStatus{ | ||
ActivityType: discordgo.ActivityTypeGame, | ||
Content: "Test", | ||
} | ||
|
||
secondStatus := SimpleBotStatus{ | ||
ActivityType: discordgo.ActivityTypeStreaming, | ||
Url: "https://localhost:8080/", | ||
} | ||
|
||
thirdStatus := SimpleBotStatus{ | ||
ActivityType: discordgo.ActivityTypeListening, | ||
Content: "Roundabout", | ||
} | ||
|
||
botStatusManager.AddStatusToRotation(firstStatus) | ||
botStatusManager.AddStatusToRotation(secondStatus) | ||
botStatusManager.AddStatusToRotation(thirdStatus) | ||
|
||
// First cycle | ||
suite.Equal(firstStatus, *botStatusManager.Next()) | ||
suite.Equal(secondStatus, *botStatusManager.Next()) | ||
suite.Equal(thirdStatus, *botStatusManager.Next()) | ||
// Second cycle | ||
suite.Equal(firstStatus, *botStatusManager.Next()) | ||
suite.Equal(secondStatus, *botStatusManager.Next()) | ||
suite.Equal(thirdStatus, *botStatusManager.Next()) | ||
// Third cycle | ||
suite.Equal(firstStatus, *botStatusManager.Next()) | ||
suite.Equal(secondStatus, *botStatusManager.Next()) | ||
suite.Equal(thirdStatus, *botStatusManager.Next()) | ||
// Fourth cycle | ||
suite.Equal(firstStatus, *botStatusManager.Next()) | ||
suite.Equal(secondStatus, *botStatusManager.Next()) | ||
suite.Equal(thirdStatus, *botStatusManager.Next()) | ||
} | ||
|
||
func TestStatusManager(t *testing.T) { | ||
suite.Run(t, new(StatusManagerSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* JOJO Discord Bot - An advanced multi-purpose discord bot | ||
* Copyright (C) 2022 Lazy Bytez (Elias Knodel, Pascal Zarrad) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bot_status | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/lazybytez/jojo-discord-bot/api" | ||
"time" | ||
) | ||
|
||
// BotStatusRotationTime is the time between the status | ||
// that are registered in the DiscordGoStatusManager. | ||
const BotStatusRotationTime = 5 * time.Minute | ||
|
||
var C = api.Component{ | ||
// Metadata | ||
Code: "bot_status", | ||
Name: "Bot Status", | ||
Description: "This component handles automated rotation and setting of the bot status in Discord.", | ||
LoadPriority: -1000, // Be the last core component, so others can register initial status | ||
|
||
State: &api.State{ | ||
DefaultEnabled: true, | ||
}, | ||
} | ||
|
||
// botStatusRotationTicker id the ticker used to periodically | ||
// change the bots' status. | ||
var botStatusRotationTicker *time.Ticker | ||
|
||
func init() { | ||
api.RegisterComponent(&C, LoadComponent) | ||
} | ||
|
||
// LoadComponent loads the bot core component | ||
// and handles migration of core entities | ||
// and registration of important core event handlers. | ||
func LoadComponent(_ *discordgo.Session) error { | ||
C.HandlerManager().RegisterOnce("start_status_rotation", onBotReady) | ||
|
||
return nil | ||
} | ||
|
||
// onBotReady starts the bot status rotation. | ||
// At this point, discordgo is fully initialized and connected. | ||
func onBotReady(_ *discordgo.Session, _ *discordgo.Ready) { | ||
startBotStatusRotation() | ||
} | ||
|
||
// startBotStatusRotation starts a routine that handles the automated rotation | ||
// of the bots' status. | ||
func startBotStatusRotation() { | ||
botStatusRotationTicker = time.NewTicker(BotStatusRotationTime) | ||
|
||
// Initial status rotation | ||
rotateStatus() | ||
|
||
// Continues status rotation | ||
go func() { | ||
for range botStatusRotationTicker.C { | ||
rotateStatus() | ||
} | ||
}() | ||
} | ||
|
||
// rotateStatus updates the status of the bot by rotating it. | ||
func rotateStatus() { | ||
status := C.BotStatusManager().Next() | ||
|
||
if nil == status { | ||
C.Logger().Info("Not updating status, as no status are registered!") | ||
|
||
return | ||
} | ||
|
||
err := C.DiscordApi().SetBotStatus(*status) | ||
|
||
if nil != err { | ||
C.Logger().Err(err, "Could not update the status of the bot due to an unexpected error!") | ||
|
||
return | ||
} | ||
|
||
C.Logger().Info("Updated bot status to content \"%s\" and url \"%s\" with activity type \"%d\"", | ||
status.Content, | ||
status.Url, | ||
status.ActivityType) | ||
} |
Oops, something went wrong.