Browse Source

Merge pull request #201 from VagantemNumen/examples

Add examples for setting account avatar
Bruce 8 years ago
parent
commit
ee608e6e3e

+ 29 - 0
examples/avatar/README.md

@@ -0,0 +1,29 @@
+<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.
+
+### 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 you wish to build.
+
+```sh
+cd $GOPATH/src/github.com/bwmarrin/discordgo/examples/avatar/localfile
+```
+```sh
+cd $GOPATH/src/github.com/bwmarrin/discordgo/examples/avatar/url
+```
+
+```sh
+go build
+```
+
+### Usage
+
+Please refer to the README.md inside the example folder for usage of that particular example.
+
+### Note
+
+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.

+ 41 - 0
examples/avatar/localfile/README.md

@@ -0,0 +1,41 @@
+<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 Token and a non-default avatar:
+
+```sh
+./localfile -t "YOUR_BOT_TOKEN" -f "./pathtoavatar.jpg"
+```

+ 72 - 0
examples/avatar/localfile/main.go

@@ -0,0 +1,72 @@
+package main
+
+import (
+	"encoding/base64"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+
+	"github.com/bwmarrin/discordgo"
+)
+
+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), string(base64))
+
+	_, err = s.UserUpdate("", "", BotUsername, avatar, "")
+	if err != nil {
+		fmt.Println(err)
+	}
+}

+ 41 - 0
examples/avatar/url/README.md

@@ -0,0 +1,41 @@
+<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 Token and a non-default avatar:
+
+```sh
+./url -t "YOUR_BOT_TOKEN" -l "http://bwmarrin.github.io/discordgo/img/discordgo.png"
+```

+ 83 - 0
examples/avatar/url/main.go

@@ -0,0 +1,83 @@
+package main
+
+import (
+	"encoding/base64"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+
+	"github.com/bwmarrin/discordgo"
+)
+
+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 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), string(base64))
+
+	_, err = s.UserUpdate("", "", BotUsername, avatar, "")
+	if err != nil {
+		fmt.Println("Error setting the avatar, ", err)
+	}
+
+}