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 | |
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')
8 files changed, 173 insertions, 266 deletions
diff --git a/vendor/github.com/bwmarrin/discordgo/examples/airhorn/main.go b/vendor/github.com/bwmarrin/discordgo/examples/airhorn/main.go index ff5e5214..21ceb76b 100644 --- a/vendor/github.com/bwmarrin/discordgo/examples/airhorn/main.go +++ b/vendor/github.com/bwmarrin/discordgo/examples/airhorn/main.go @@ -6,7 +6,9 @@ import ( "fmt" "io" "os" + "os/signal" "strings" + "syscall" "time" "github.com/bwmarrin/discordgo" @@ -21,6 +23,7 @@ var token string var buffer = make([][]byte, 0) func main() { + if token == "" { fmt.Println("No token provided. Please run: airhorn -t <bot token>") return @@ -56,21 +59,37 @@ func main() { fmt.Println("Error opening Discord session: ", err) } + // Wait here until CTRL-C or other term signal is received. fmt.Println("Airhorn is now running. Press CTRL-C to exit.") - // Simple way to keep program running until CTRL-C is pressed. - <-make(chan struct{}) - return + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + // Cleanly close down the Discord session. + dg.Close() } +// This function will be called (due to AddHandler above) when the bot receives +// the "ready" event from Discord. func ready(s *discordgo.Session, event *discordgo.Ready) { + // Set the playing status. - _ = s.UpdateStatus(0, "!airhorn") + s.UpdateStatus(0, "!airhorn") } // This function will be called (due to AddHandler above) every time a new // message is created on any channel that the autenticated bot has access to. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + + // Ignore all messages created by the bot itself + // This isn't required in this specific example but it's a good practice. + if m.Author.ID == s.State.User.ID { + return + } + + // check if the message is "!airhorn" if strings.HasPrefix(m.Content, "!airhorn") { + // Find the channel that the message came from. c, err := s.State.Channel(m.ChannelID) if err != nil { @@ -85,7 +104,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - // Look for the message sender in that guilds current voice states. + // Look for the message sender in that guild's current voice states. for _, vs := range g.VoiceStates { if vs.UserID == m.Author.ID { err = playSound(s, g.ID, vs.ChannelID) @@ -102,6 +121,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // This function will be called (due to AddHandler above) every time a new // guild is joined. func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { + if event.Guild.Unavailable { return } @@ -116,8 +136,8 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { // loadSound attempts to load an encoded sound file from disk. func loadSound() error { - file, err := os.Open("airhorn.dca") + file, err := os.Open("airhorn.dca") if err != nil { fmt.Println("Error opening dca file :", err) return err @@ -131,7 +151,7 @@ func loadSound() error { // If this is the end of the file, just return. if err == io.EOF || err == io.ErrUnexpectedEOF { - file.Close() + err := file.Close() if err != nil { return err } @@ -160,6 +180,7 @@ func loadSound() error { // playSound plays the current buffer to the provided channel. func playSound(s *discordgo.Session, guildID, channelID string) (err error) { + // Join the provided voice channel. vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true) if err != nil { @@ -170,7 +191,7 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) { time.Sleep(250 * time.Millisecond) // Start speaking. - _ = vc.Speaking(true) + vc.Speaking(true) // Send the buffer data. for _, buff := range buffer { @@ -178,13 +199,13 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) { } // Stop speaking - _ = vc.Speaking(false) + vc.Speaking(false) // Sleep for a specificed amount of time before ending. time.Sleep(250 * time.Millisecond) // Disconnect from the provided voice channel. - _ = vc.Disconnect() + vc.Disconnect() return nil } diff --git a/vendor/github.com/bwmarrin/discordgo/examples/appmaker/main.go b/vendor/github.com/bwmarrin/discordgo/examples/appmaker/main.go index bd0e3b88..286fe169 100644 --- a/vendor/github.com/bwmarrin/discordgo/examples/appmaker/main.go +++ b/vendor/github.com/bwmarrin/discordgo/examples/appmaker/main.go @@ -1,38 +1,42 @@ package main import ( + "encoding/json" "flag" "fmt" + "os" "github.com/bwmarrin/discordgo" ) // Variables used for command line options var ( - Email string - Password string Token string - AppName string + Name string DeleteID string ListOnly bool ) func init() { - flag.StringVar(&Email, "e", "", "Account Email") - flag.StringVar(&Password, "p", "", "Account Password") - flag.StringVar(&Token, "t", "", "Account Token") + flag.StringVar(&Token, "t", "", "Owner Account Token") + flag.StringVar(&Name, "n", "", "Name to give App/Bot") flag.StringVar(&DeleteID, "d", "", "Application ID to delete") flag.BoolVar(&ListOnly, "l", false, "List Applications Only") - flag.StringVar(&AppName, "a", "", "App/Bot Name") flag.Parse() + + if Token == "" { + flag.Usage() + os.Exit(1) + } } func main() { var err error + // Create a new Discord session using the provided login information. - dg, err := discordgo.New(Email, Password, Token) + dg, err := discordgo.New(Token) if err != nil { fmt.Println("error creating Discord session,", err) return @@ -41,18 +45,17 @@ func main() { // If -l set, only display a list of existing applications // for the given account. if ListOnly { - aps, err2 := dg.Applications() - if err2 != nil { + + aps, err := dg.Applications() + if err != nil { fmt.Println("error fetching applications,", err) return } - for k, v := range aps { - fmt.Printf("%d : --------------------------------------\n", k) - fmt.Printf("ID: %s\n", v.ID) - fmt.Printf("Name: %s\n", v.Name) - fmt.Printf("Secret: %s\n", v.Secret) - fmt.Printf("Description: %s\n", v.Description) + for _, v := range aps { + fmt.Println("-----------------------------------------------------") + b, _ := json.MarshalIndent(v, "", " ") + fmt.Println(string(b)) } return } @@ -66,9 +69,14 @@ func main() { return } + if Name == "" { + flag.Usage() + os.Exit(1) + } + // Create a new application. ap := &discordgo.Application{} - ap.Name = AppName + ap.Name = Name ap, err = dg.ApplicationCreate(ap) if err != nil { fmt.Println("error creating new applicaiton,", err) @@ -76,9 +84,8 @@ func main() { } fmt.Printf("Application created successfully:\n") - fmt.Printf("ID: %s\n", ap.ID) - fmt.Printf("Name: %s\n", ap.Name) - fmt.Printf("Secret: %s\n\n", ap.Secret) + b, _ := json.MarshalIndent(ap, "", " ") + fmt.Println(string(b)) // Create the bot account under the application we just created bot, err := dg.ApplicationBotCreate(ap.ID) @@ -88,11 +95,9 @@ func main() { } fmt.Printf("Bot account created successfully.\n") - fmt.Printf("ID: %s\n", bot.ID) - fmt.Printf("Username: %s\n", bot.Username) - fmt.Printf("Token: %s\n\n", bot.Token) + b, _ = json.MarshalIndent(bot, "", " ") + fmt.Println(string(b)) + fmt.Println("Please save the above posted info in a secure place.") fmt.Println("You will need that information to login with your bot account.") - - return } 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) - } - -} diff --git a/vendor/github.com/bwmarrin/discordgo/examples/mytoken/main.go b/vendor/github.com/bwmarrin/discordgo/examples/mytoken/main.go index 5914fc8a..9375eadc 100644 --- a/vendor/github.com/bwmarrin/discordgo/examples/mytoken/main.go +++ b/vendor/github.com/bwmarrin/discordgo/examples/mytoken/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "os" "github.com/bwmarrin/discordgo" ) @@ -18,6 +19,11 @@ func init() { flag.StringVar(&Email, "e", "", "Account Email") flag.StringVar(&Password, "p", "", "Account Password") flag.Parse() + + if Email == "" || Password == "" { + flag.Usage() + os.Exit(1) + } } func main() { @@ -29,5 +35,6 @@ func main() { return } + // Print out your token. fmt.Printf("Your Authentication Token is:\n\n%s\n", dg.Token) } diff --git a/vendor/github.com/bwmarrin/discordgo/examples/new_basic/main.go b/vendor/github.com/bwmarrin/discordgo/examples/new_basic/main.go deleted file mode 100644 index 0191bb06..00000000 --- a/vendor/github.com/bwmarrin/discordgo/examples/new_basic/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "time" - - "github.com/bwmarrin/discordgo" -) - -// Variables used for command line parameters -var ( - Token string -) - -func init() { - - flag.StringVar(&Token, "t", "", "Bot Token") - flag.Parse() -} - -func main() { - - // Create a new Discord session using the provided bot token. - dg, err := discordgo.New("Bot " + Token) - if err != nil { - fmt.Println("error creating Discord session,", err) - return - } - - // Register messageCreate as a callback for the messageCreate events. - dg.AddHandler(messageCreate) - - // Open the websocket and begin listening. - err = dg.Open() - if err != nil { - fmt.Println("error opening connection,", err) - return - } - - 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 -} - -// This function will be called (due to AddHandler above) every time a new -// message is created on any channel that the autenticated bot has access to. -func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - - // Print message to stdout. - fmt.Printf("%20s %20s %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content) -} diff --git a/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go b/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go index 2edd957e..155e782f 100644 --- a/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go +++ b/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go @@ -3,6 +3,9 @@ package main import ( "flag" "fmt" + "os" + "os/signal" + "syscall" "github.com/bwmarrin/discordgo" ) @@ -10,7 +13,6 @@ import ( // Variables used for command line parameters var ( Token string - BotID string ) func init() { @@ -28,29 +30,24 @@ func main() { return } - // Get the account information. - u, err := dg.User("@me") - if err != nil { - fmt.Println("error obtaining account details,", err) - } - - // Store the account ID for later use. - BotID = u.ID - - // Register messageCreate as a callback for the messageCreate events. + // Register the messageCreate func as a callback for MessageCreate events. dg.AddHandler(messageCreate) - // Open the websocket and begin listening. + // Open a websocket connection to Discord and begin listening. err = dg.Open() if err != nil { fmt.Println("error opening connection,", err) return } + // Wait here until CTRL-C or other term signal is received. 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 + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + // Cleanly close down the Discord session. + dg.Close() } // This function will be called (due to AddHandler above) every time a new @@ -58,17 +55,17 @@ func main() { func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Ignore all messages created by the bot itself - if m.Author.ID == BotID { + // This isn't required in this specific example but it's a good practice. + if m.Author.ID == s.State.User.ID { return } - // If the message is "ping" reply with "Pong!" if m.Content == "ping" { - _, _ = s.ChannelMessageSend(m.ChannelID, "Pong!") + s.ChannelMessageSend(m.ChannelID, "Pong!") } // If the message is "pong" reply with "Ping!" if m.Content == "pong" { - _, _ = s.ChannelMessageSend(m.ChannelID, "Ping!") + s.ChannelMessageSend(m.ChannelID, "Ping!") } } |