Consolidated avatar examples to one, cleaned it up.

This commit is contained in:
Bruce 2017-04-14 14:02:37 +00:00
parent 7044ac3441
commit e16cb509c2
6 changed files with 123 additions and 255 deletions

View file

@ -1,20 +1,21 @@
<img align="right" src="http://bwmarrin.github.io/discordgo/img/discordgo.png"> <img align="right" src="http://bwmarrin.github.io/discordgo/img/discordgo.png">
Avatar Examples
====
These example demonstrates how to utilize DiscordGo to change the account avatar using local files and urls. ## DiscordGo Avatar Example
This example demonstrates how to utilize DiscordGo to change the avatar for
a Discord account. This example works both with a local file or the URL of
an image.
**Join [Discord Gophers](https://discord.gg/0f1SbxBZjYoCtNPP)
Discord chat channel for support.**
### Build ### Build
This assumes you already have a working Go environment setup and that DiscordGo is correctly installed on your system. This assumes you already have a working Go environment setup and that
Change directory into the example you wish to build. DiscordGo is correctly installed on your system.
```sh From within the avatar example folder, run the below command to compile the
cd $GOPATH/src/github.com/bwmarrin/discordgo/examples/avatar/localfile example.
```
```sh
cd $GOPATH/src/github.com/bwmarrin/discordgo/examples/avatar/url
```
```sh ```sh
go build go build
@ -22,8 +23,27 @@ go build
### Usage ### Usage
Please refer to the README.md inside the example folder for usage of that particular example. This example uses bot tokens for authentication only. While email/password is
supported by DiscordGo, it is not recommended to use them.
### Note ```
./avatar --help
Usage of ./avatar:
-f string
Avatar File Name
-t string
Bot Token
-u string
URL to the avatar image
```
Please be aware that you will need to login with the account you just changed avatar of to visually see the change. Alternatively you could query the avatar from dicord servers to make sure the change has indeed occured. The below example shows how to set your Avatar from a local file.
```sh
./avatar-t TOKEN -f avatar.png
```
The below example shows how to set your Avatar from a URL.
```sh
./avatar-t TOKEN -u http://bwmarrin.github.io/discordgo/img/discordgo.png
```

View file

@ -1,41 +0,0 @@
<img align="right" src="http://bwmarrin.github.io/discordgo/img/discordgo.png">
Avatar Local File Example
====
This example demonstrates how to utilize DiscordGo to change the account avatar using a local file inside the current working directory.
### Build
This assumes you already have a working Go environment setup and that DiscordGo is correctly installed on your system.
Change directory into the example.
```sh
cd $GOPATH/src/github.com/bwmarrin/discordgo/examples/avatar/localfile
```
```sh
go build
```
### Usage
Please place the file you wish to use as an avatar inside the directory named as ``avatar.jpg``. The filename is not important if you supply it via the commandline flag ``-f`` when starting the application.
```sh
./localfile --help
Usage of ./ocalfile:
-e string
Account Email
-p string
Account Password
-t string
Account Token
-f string
Avatar File Name.
```
For example to start application with a bot token and a non-default avatar:
```sh
./localfile -t "Bot YOUR_BOT_TOKEN" -f "./pathtoavatar.jpg"
```

View file

@ -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)
}
}

89
examples/avatar/main.go Normal file
View file

@ -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)
}
}

View file

@ -1,41 +0,0 @@
<img align="right" src="http://bwmarrin.github.io/discordgo/img/discordgo.png">
Avatar Url Example
====
This example demonstrates how to utilize DiscordGo to change the account avatar using a remote url provided via a commandline flag.
### Build
This assumes you already have a working Go environment setup and that DiscordGo is correctly installed on your system.
Change directory into the example.
```sh
cd $GOPATH/src/github.com/bwmarrin/discordgo/examples/avatar/url
```
```sh
go build
```
### Usage
Please place the file you wish to use as an avatar inside the directory named as ``avatar.jpg``. The filename is not important if you supply it via the commandline flag ``-l`` when starting the application. If the flag is not specified avatar is set to DiscordGo Logo.
```sh
./url --help
Usage of ./url:
-e string
Account Email
-p string
Account Password
-t string
Account Token
-l string
Link to the avatar image.
```
For example to start application with a bot token and a non-default avatar:
```sh
./url -t "Bot YOUR_BOT_TOKEN" -l "http://bwmarrin.github.io/discordgo/img/discordgo.png"
```

View file

@ -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)
}
}