summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/bwmarrin/discordgo/examples/pingpong
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-09-19 20:53:26 +0200
committerWim <wim@42.be>2016-09-19 20:53:26 +0200
commita0b84beb9bae3514940a0cde40948e885184f555 (patch)
tree0e8d22c5e2292c24e46699e59f442795b6158b3a /vendor/github.com/bwmarrin/discordgo/examples/pingpong
parent0816e968318be5a4b165ac8fd30c032c6ecce61c (diff)
downloadmatterbridge-msglm-a0b84beb9bae3514940a0cde40948e885184f555.tar.gz
matterbridge-msglm-a0b84beb9bae3514940a0cde40948e885184f555.tar.bz2
matterbridge-msglm-a0b84beb9bae3514940a0cde40948e885184f555.zip
Add Discord support
Diffstat (limited to 'vendor/github.com/bwmarrin/discordgo/examples/pingpong')
-rw-r--r--vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go b/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go
new file mode 100644
index 00000000..e6893ca1
--- /dev/null
+++ b/vendor/github.com/bwmarrin/discordgo/examples/pingpong/main.go
@@ -0,0 +1,78 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+
+ "github.com/bwmarrin/discordgo"
+)
+
+// Variables used for command line parameters
+var (
+ Email string
+ Password string
+ Token string
+ BotID string
+)
+
+func init() {
+
+ flag.StringVar(&Email, "e", "", "Account Email")
+ flag.StringVar(&Password, "p", "", "Account Password")
+ flag.StringVar(&Token, "t", "", "Account Token")
+ flag.Parse()
+}
+
+func main() {
+
+ // Create a new Discord session using the provided login information.
+ dg, err := discordgo.New(Email, Password, Token)
+ if err != nil {
+ fmt.Println("error creating Discord session,", err)
+ 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.
+ 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) {
+
+ // Ignore all messages created by the bot itself
+ if m.Author.ID == BotID {
+ return
+ }
+
+ // If the message is "ping" reply with "Pong!"
+ if m.Content == "ping" {
+ _, _ = s.ChannelMessageSend(m.ChannelID, "Pong!")
+ }
+
+ // If the message is "pong" reply with "Ping!"
+ if m.Content == "pong" {
+ _, _ = s.ChannelMessageSend(m.ChannelID, "Ping!")
+ }
+}