discord_notify_job.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package job
  2. import (
  3. "context"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  6. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/service/discord"
  8. )
  9. // DiscordNotifyJob sends periodic status reports and database backups via Discord bot.
  10. type DiscordNotifyJob struct {
  11. xrayService service.XrayService
  12. serverService service.ServerService
  13. inboundService service.InboundService
  14. settingService service.SettingService
  15. discordService *discord.DiscordService
  16. }
  17. // NewDiscordNotifyJob creates a new Discord notification job instance.
  18. func NewDiscordNotifyJob(discordService *discord.DiscordService) *DiscordNotifyJob {
  19. return &DiscordNotifyJob{
  20. discordService: discordService,
  21. }
  22. }
  23. // Run executes the periodic status report if Discord bot is enabled and Xray is running.
  24. func (j *DiscordNotifyJob) Run() {
  25. if j.discordService == nil {
  26. return
  27. }
  28. enabled, err := j.settingService.GetDiscordBotEnable()
  29. if err != nil || !enabled {
  30. return
  31. }
  32. if !j.xrayService.IsXrayRunning() {
  33. return
  34. }
  35. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  36. defer cancel()
  37. if err := j.discordService.SendReport(ctx, &j.serverService, &j.inboundService); err != nil {
  38. logger.Warning("DiscordNotifyJob: failed to send status report: ", err)
  39. }
  40. }