diff options
| author | Bobby <[email protected]> | 2025-03-27 11:19:40 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-03-27 11:19:40 +0530 |
| commit | 3a63a1a8be65f3e50141a3970a9d63d91b191755 (patch) | |
| tree | b4ec62d7c3b1741a7c6e832acd61515092690fe6 | |
| parent | b728a3e2b99e7e905485195dbf51cbba8a3b394e (diff) | |
| download | ai-3a63a1a8be65f3e50141a3970a9d63d91b191755.tar.xz ai-3a63a1a8be65f3e50141a3970a9d63d91b191755.zip | |
session handling for discord
| -rw-r--r-- | ai/main.go | 60 | ||||
| -rw-r--r-- | go.mod | 11 | ||||
| -rw-r--r-- | go.sum | 12 |
3 files changed, 80 insertions, 3 deletions
@@ -4,9 +4,65 @@ import ( "ai/config" "ai/types" "ai/utils/logger" - "fmt" + "os" + "os/signal" + "syscall" + + "github.com/bwmarrin/discordgo" +) + +const ( + ProcessPrefix = "Main Process" +) + +var ( + session *discordgo.Session + err error ) +func init() { + session, err = discordgo.New("Bot " + config.Config.DiscordToken) + if err != nil { + logger.Log("error creating Discord session,", types.LogOptions{Fatal: true, Prefix: ProcessPrefix, Level: types.Error}) + } + + session.Identify.Intents |= discordgo.IntentsAllWithoutPrivileged + session.AddHandler(ready) +} + func main() { - logger.Log(fmt.Sprintf("Bot Started. Config: %+v", config.Config), types.LogOptions{}) + err = session.Open() + if err != nil { + logger.Log("error opening connection,", types.LogOptions{Fatal: true, Prefix: ProcessPrefix, Level: types.Error}) + } + + // Wait here until CTRL-C or other term signal is received. + logger.Log("Bot is now running. Press CTRL-C to exit.", types.LogOptions{Prefix: ProcessPrefix}) + defer session.Close() + + session_close := make(chan os.Signal, 1) + signal.Notify(session_close, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + + <-session_close + + logger.Log("Recived SIGINT. Shutting down gracefully.", types.LogOptions{Prefix: ProcessPrefix}) +} + +func ready(s *discordgo.Session, event *discordgo.Ready) { + logger.Log("Bot is ready.", types.LogOptions{Prefix: ProcessPrefix, Level: types.Success}) + + switch config.Config.Activity { + case types.PLAYING: + err = s.UpdateGameStatus(0, config.Config.ActivityMessage) + case types.WATCHING: + err = s.UpdateWatchStatus(0, config.Config.ActivityMessage) + case types.LISTENING: + err = s.UpdateListeningStatus(config.Config.ActivityMessage) + case types.STREAMING: + err = s.UpdateStreamingStatus(0, config.Config.ActivityMessage, config.Config.ActivityURL) + } + + if err != nil { + logger.Log("Error attempting to set activity.", types.LogOptions{Prefix: ProcessPrefix, Level: types.Error}) + } } @@ -2,4 +2,13 @@ module ai go 1.24.1 -require github.com/joho/godotenv v1.5.1 +require ( + github.com/bwmarrin/discordgo v0.28.1 + github.com/joho/godotenv v1.5.1 +) + +require ( + github.com/gorilla/websocket v1.4.2 // indirect + golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect + golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect +) @@ -1,2 +1,14 @@ +github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= +github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
