diff options
author | Wim <wim@42.be> | 2017-07-16 14:38:45 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-07-16 14:38:45 +0200 |
commit | 5db24aa90163839d0deebc4062d84aa4222bd50a (patch) | |
tree | a5eed881ef53a5cc2d3ef6e1eccdd37345ed0880 /vendor/github.com/bwmarrin/discordgo/examples/avatar | |
parent | aec5e3d77b6e480d04dd8773723de62416a94919 (diff) | |
download | matterbridge-msglm-5db24aa90163839d0deebc4062d84aa4222bd50a.tar.gz matterbridge-msglm-5db24aa90163839d0deebc4062d84aa4222bd50a.tar.bz2 matterbridge-msglm-5db24aa90163839d0deebc4062d84aa4222bd50a.zip |
Update vendor (bwmarrin/discordgo)
Diffstat (limited to 'vendor/github.com/bwmarrin/discordgo/examples/avatar')
3 files changed, 89 insertions, 159 deletions
diff --git a/vendor/github.com/bwmarrin/discordgo/examples/avatar/localfile/main.go b/vendor/github.com/bwmarrin/discordgo/examples/avatar/localfile/main.go deleted file mode 100644 index adfe0b1d..00000000 --- a/vendor/github.com/bwmarrin/discordgo/examples/avatar/localfile/main.go +++ /dev/null @@ -1,73 +0,0 @@ -package main - -import ( - "encoding/base64" - "flag" - "fmt" - "io/ioutil" - "net/http" - - "github.com/bwmarrin/discordgo" -) - -// Variables used for command line parameters -var ( - Email string - Password string - Token string - Avatar string - BotID string - BotUsername string -) - -func init() { - - flag.StringVar(&Email, "e", "", "Account Email") - flag.StringVar(&Password, "p", "", "Account Password") - flag.StringVar(&Token, "t", "", "Account Token") - flag.StringVar(&Avatar, "f", "./avatar.jpg", "Avatar File Name") - flag.Parse() -} - -func main() { - - // Create a new Discord session using the provided login information. - // Use discordgo.New(Token) to just use a token for login. - dg, err := discordgo.New(Email, Password, Token) - if err != nil { - fmt.Println("error creating Discord session,", err) - return - } - - bot, err := dg.User("@me") - if err != nil { - fmt.Println("error fetching the bot details,", err) - return - } - - BotID = bot.ID - BotUsername = bot.Username - changeAvatar(dg) - - fmt.Println("Bot is now running. Press CTRL-C to exit.") - // Simple way to keep program running until CTRL-C is pressed. - <-make(chan struct{}) - return -} - -// Helper function to change the avatar -func changeAvatar(s *discordgo.Session) { - img, err := ioutil.ReadFile(Avatar) - if err != nil { - fmt.Println(err) - } - - base64 := base64.StdEncoding.EncodeToString(img) - - avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64) - - _, err = s.UserUpdate("", "", BotUsername, avatar, "") - if err != nil { - fmt.Println(err) - } -} diff --git a/vendor/github.com/bwmarrin/discordgo/examples/avatar/main.go b/vendor/github.com/bwmarrin/discordgo/examples/avatar/main.go new file mode 100644 index 00000000..e0a9c880 --- /dev/null +++ b/vendor/github.com/bwmarrin/discordgo/examples/avatar/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "encoding/base64" + "flag" + "fmt" + "io/ioutil" + "net/http" + "os" + + "github.com/bwmarrin/discordgo" +) + +// Variables used for command line parameters +var ( + Token string + AvatarFile string + AvatarURL string +) + +func init() { + + flag.StringVar(&Token, "t", "", "Bot Token") + flag.StringVar(&AvatarFile, "f", "", "Avatar File Name") + flag.StringVar(&AvatarURL, "u", "", "URL to the avatar image") + flag.Parse() + + if Token == "" || (AvatarFile == "" && AvatarURL == "") { + flag.Usage() + os.Exit(1) + } +} + +func main() { + + // Create a new Discord session using the provided login information. + dg, err := discordgo.New("Bot " + Token) + if err != nil { + fmt.Println("error creating Discord session,", err) + return + } + + // Declare these here so they can be used in the below two if blocks and + // still carry over to the end of this function. + var base64img string + var contentType string + + // If we're using a URL link for the Avatar + if AvatarURL != "" { + + resp, err := http.Get(AvatarURL) + if err != nil { + fmt.Println("Error retrieving the file, ", err) + return + } + + defer func() { + _ = resp.Body.Close() + }() + + img, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading the response, ", err) + return + } + + contentType = http.DetectContentType(img) + base64img = base64.StdEncoding.EncodeToString(img) + } + + // If we're using a local file for the Avatar + if AvatarFile != "" { + img, err := ioutil.ReadFile(AvatarFile) + if err != nil { + fmt.Println(err) + } + + contentType = http.DetectContentType(img) + base64img = base64.StdEncoding.EncodeToString(img) + } + + // Now lets format our base64 image into the proper format Discord wants + // and then call UserUpdate to set it as our user's Avatar. + avatar := fmt.Sprintf("data:%s;base64,%s", contentType, base64img) + _, err = dg.UserUpdate("", "", "", avatar, "") + if err != nil { + fmt.Println(err) + } +} diff --git a/vendor/github.com/bwmarrin/discordgo/examples/avatar/url/main.go b/vendor/github.com/bwmarrin/discordgo/examples/avatar/url/main.go deleted file mode 100644 index 26170df5..00000000 --- a/vendor/github.com/bwmarrin/discordgo/examples/avatar/url/main.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -import ( - "encoding/base64" - "flag" - "fmt" - "io/ioutil" - "net/http" - - "github.com/bwmarrin/discordgo" -) - -// Variables used for command line parameters -var ( - Email string - Password string - Token string - URL string - BotID string - BotUsername string -) - -func init() { - - flag.StringVar(&Email, "e", "", "Account Email") - flag.StringVar(&Password, "p", "", "Account Password") - flag.StringVar(&Token, "t", "", "Account Token") - flag.StringVar(&URL, "l", "http://bwmarrin.github.io/discordgo/img/discordgo.png", "Link to the avatar image") - flag.Parse() -} - -func main() { - - // Create a new Discord session using the provided login information. - // Use discordgo.New(Token) to just use a token for login. - dg, err := discordgo.New(Email, Password, Token) - if err != nil { - fmt.Println("error creating Discord session,", err) - return - } - - bot, err := dg.User("@me") - if err != nil { - fmt.Println("error fetching the bot details,", err) - return - } - - BotID = bot.ID - BotUsername = bot.Username - changeAvatar(dg) - - fmt.Println("Bot is now running. Press CTRL-C to exit.") - // Simple way to keep program running until CTRL-C is pressed. - <-make(chan struct{}) - return -} - -// Helper function to change the avatar -func changeAvatar(s *discordgo.Session) { - - resp, err := http.Get(URL) - if err != nil { - fmt.Println("Error retrieving the file, ", err) - return - } - - defer func() { - _ = resp.Body.Close() - }() - - img, err := ioutil.ReadAll(resp.Body) - if err != nil { - fmt.Println("Error reading the response, ", err) - return - } - - base64 := base64.StdEncoding.EncodeToString(img) - - avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64) - - _, err = s.UserUpdate("", "", BotUsername, avatar, "") - if err != nil { - fmt.Println("Error setting the avatar, ", err) - } - -} |