main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "time"
  9. "github.com/bwmarrin/discordgo"
  10. )
  11. // Flags
  12. var (
  13. GuildID = flag.String("guild", "", "Test guild ID")
  14. VoiceChannelID = flag.String("voice", "", "Test voice channel ID")
  15. BotToken = flag.String("token", "", "Bot token")
  16. )
  17. func init() { flag.Parse() }
  18. func main() {
  19. s, _ := discordgo.New("Bot " + *BotToken)
  20. s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
  21. fmt.Println("Bot is ready")
  22. })
  23. err := s.Open()
  24. if err != nil {
  25. log.Fatalf("Cannot open the session: %v", err)
  26. }
  27. defer s.Close()
  28. event := createAmazingEvent(s)
  29. transformEventToExternalEvent(s, event)
  30. stop := make(chan os.Signal, 1)
  31. signal.Notify(stop, os.Interrupt)
  32. <-stop
  33. log.Println("Graceful shutdown")
  34. }
  35. // Create a new event on guild
  36. func createAmazingEvent(s *discordgo.Session) *discordgo.GuildScheduledEvent {
  37. // Define the starting time (must be in future)
  38. startingTime := time.Now().Add(1 * time.Hour)
  39. // Define the ending time (must be after starting time)
  40. endingTime := startingTime.Add(30 * time.Minute)
  41. // Create the event
  42. scheduledEvent, err := s.GuildScheduledEventCreate(*GuildID, &discordgo.GuildScheduledEventParams{
  43. Name: "Amazing Event",
  44. Description: "This event will start in 1 hour and last 30 minutes",
  45. ScheduledStartTime: &startingTime,
  46. ScheduledEndTime: &endingTime,
  47. EntityType: discordgo.GuildScheduledEventEntityTypeVoice,
  48. ChannelID: *VoiceChannelID,
  49. PrivacyLevel: discordgo.GuildScheduledEventPrivacyLevelGuildOnly,
  50. })
  51. if err != nil {
  52. log.Printf("Error creating scheduled event: %v", err)
  53. return nil
  54. }
  55. fmt.Println("Created scheduled event:", scheduledEvent.Name)
  56. return scheduledEvent
  57. }
  58. func transformEventToExternalEvent(s *discordgo.Session, event *discordgo.GuildScheduledEvent) {
  59. scheduledEvent, err := s.GuildScheduledEventEdit(*GuildID, event.ID, &discordgo.GuildScheduledEventParams{
  60. Name: "Amazing Event @ Discord Website",
  61. EntityType: discordgo.GuildScheduledEventEntityTypeExternal,
  62. EntityMetadata: &discordgo.GuildScheduledEventEntityMetadata{
  63. Location: "https://discord.com",
  64. },
  65. })
  66. if err != nil {
  67. log.Printf("Error during transformation of scheduled voice event into external event: %v", err)
  68. return
  69. }
  70. fmt.Println("Created scheduled event:", scheduledEvent.Name)
  71. }