main.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "encoding/base64"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "github.com/bwmarrin/discordgo"
  9. )
  10. // Variables used for command line parameters
  11. var (
  12. Email string
  13. Password string
  14. Token string
  15. URL string
  16. BotID string
  17. BotUsername string
  18. )
  19. func init() {
  20. flag.StringVar(&Email, "e", "", "Account Email")
  21. flag.StringVar(&Password, "p", "", "Account Password")
  22. flag.StringVar(&Token, "t", "", "Account Token")
  23. flag.StringVar(&URL, "l", "http://bwmarrin.github.io/discordgo/img/discordgo.png", "Link to the avatar image")
  24. flag.Parse()
  25. }
  26. func main() {
  27. // Create a new Discord session using the provided login information.
  28. // Use discordgo.New(Token) to just use a token for login.
  29. dg, err := discordgo.New(Email, Password, Token)
  30. if err != nil {
  31. fmt.Println("error creating Discord session,", err)
  32. return
  33. }
  34. bot, err := dg.User("@me")
  35. if err != nil {
  36. fmt.Println("error fetching the bot details,", err)
  37. return
  38. }
  39. BotID = bot.ID
  40. BotUsername = bot.Username
  41. changeAvatar(dg)
  42. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  43. // Simple way to keep program running until CTRL-C is pressed.
  44. <-make(chan struct{})
  45. return
  46. }
  47. // Helper function to change the avatar
  48. func changeAvatar(s *discordgo.Session) {
  49. resp, err := http.Get(URL)
  50. if err != nil {
  51. fmt.Println("Error retrieving the file, ", err)
  52. return
  53. }
  54. defer func() {
  55. _ = resp.Body.Close()
  56. }()
  57. img, err := ioutil.ReadAll(resp.Body)
  58. if err != nil {
  59. fmt.Println("Error reading the response, ", err)
  60. return
  61. }
  62. base64 := base64.StdEncoding.EncodeToString(img)
  63. avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64)
  64. _, err = s.UserUpdate("", "", BotUsername, avatar, "")
  65. if err != nil {
  66. fmt.Println("Error setting the avatar, ", err)
  67. }
  68. }