main.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "encoding/base64"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "time"
  9. "github.com/bwmarrin/discordgo"
  10. )
  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. // Register messageCreate as a callback for the messageCreate events.
  35. dg.AddHandler(messageCreate)
  36. // Open the websocket and begin listening.
  37. dg.Open()
  38. bot, err := dg.User("@me")
  39. if err != nil {
  40. fmt.Println("error fetching the bot details,", err)
  41. return
  42. }
  43. BotID = bot.ID
  44. BotUsername = bot.Username
  45. changeAvatar(dg)
  46. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  47. // Simple way to keep program running until CTRL-C is pressed.
  48. <-make(chan struct{})
  49. return
  50. }
  51. // Helper function to change the avatar
  52. func changeAvatar(s *discordgo.Session) {
  53. resp, err := http.Get(Url)
  54. if err != nil {
  55. fmt.Println("Error retrieving the file, ", err)
  56. return
  57. }
  58. defer resp.Body.Close()
  59. img, err := ioutil.ReadAll(resp.Body)
  60. if err != nil {
  61. fmt.Println("Error reading the response, ", err)
  62. return
  63. }
  64. base64 := base64.StdEncoding.EncodeToString(img)
  65. avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), string(base64))
  66. _, err = s.UserUpdate("", "", BotUsername, avatar, "")
  67. if err != nil {
  68. fmt.Println("Error setting the avatar, ", err)
  69. }
  70. }
  71. // This function will be called (due to AddHandler above) every time a new
  72. // message is created on any channel that the autenticated bot has access to.
  73. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  74. // Print message to stdout.
  75. fmt.Printf("%-20s %-20s\n %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content)
  76. }