TerrificTable55

Untitled

Dec 31st, 2022
1,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.70 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/BurntSushi/toml"
  6.     "io/ioutil"
  7.     "github.com/bwmarrin/discordgo"
  8.     "os"
  9.     "log"
  10.     "net/http"
  11. )
  12.  
  13.  
  14. type Config struct {
  15.     Bot struct {
  16.         Token   string `toml:"token"`
  17.         GuildID string `toml:"guild_id"`
  18.     } `toml:"Bot"`
  19. }
  20.  
  21.  
  22.  
  23. func main() {
  24.  
  25.     content, err := ioutil.ReadFile("config.toml")     // the file is inside the local directory
  26.     if err != nil { fmt.Println(err); os.Exit(1) }
  27.  
  28.     var conf Config
  29.     _, err = toml.Decode(string(content), &conf)
  30.     if err != nil { fmt.Println(err); os.Exit(1) }
  31.  
  32.  
  33.     dg, err := discordgo.New("Bot " + conf.Bot.Token)
  34.     if err != nil { fmt.Println(err); os.Exit(1) }
  35.  
  36.  
  37.     dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate){
  38.  
  39.         /* err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  40.             Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
  41.         })
  42.         if err != nil {
  43.             fmt.Println("1. InteractionRespond -> ", err)
  44.             s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
  45.                 Content: "Something went wrong",
  46.             })
  47.         } */
  48.  
  49.  
  50.         resp, err := http.Get("https://honbra.com/api/cat/random")
  51.         if err != nil { log.Println(err); os.Exit(1) }
  52.  
  53.         defer resp.Body.Close()
  54.  
  55.  
  56.         embed := discordgo.MessageEmbed{
  57.             Description: "C T A",
  58.             Color: 0x1d5cff,
  59.             Image: &discordgo.MessageEmbedImage{
  60.                 URL: "attachment://image.webp",
  61.             },
  62.         }
  63.  
  64.         err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  65.             Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
  66.             Data: &discordgo.InteractionResponseData{
  67.                 Embeds: []*discordgo.MessageEmbed{
  68.                     &embed,
  69.                 },
  70.                 Files: []*discordgo.File {
  71.                     {
  72.                         Name:   "image.webp",
  73.                         Reader: resp.Body,
  74.                         ContentType: "webp",
  75.                     },
  76.                 },
  77.             },
  78.         })
  79.         if err != nil {
  80.             fmt.Println("InteractionRespond -> ", err)
  81.  
  82.             s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
  83.                 Content: "Something went wrong",
  84.             })
  85.         }
  86.  
  87.     })
  88.  
  89.  
  90.     dg.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
  91.         log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
  92.     })
  93.  
  94.     err = dg.Open()
  95.     if err != nil { fmt.Println(err); os.Exit(1) }
  96.  
  97.  
  98.     registerCommands(dg, conf)
  99.  
  100.  
  101.     defer func(){
  102.         err := dg.Close()
  103.         if err != nil { fmt.Println(err); os.Exit(1) }
  104.     }()
  105.  
  106.     fmt.Println("Bot is now running. Press CTRL-C to exit.")
  107.     <-make(chan struct{})
  108.     return
  109.  
  110. }
  111.  
  112.  
  113.  
  114. func registerCommands(dg *discordgo.Session, conf Config) {
  115.     cmd := &discordgo.ApplicationCommand{
  116.         Name: "cta",
  117.         Description: "CTA",
  118.     }
  119.  
  120.     _, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", cmd)
  121.     if err != nil { fmt.Println(err); os.Exit(1) }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment