Adding Ping Pong example bot.
This commit is contained in:
parent
5549070372
commit
19db45c7ee
2 changed files with 77 additions and 0 deletions
9
examples/pingpong/README.md
Normal file
9
examples/pingpong/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
===
|
||||||
|
DiscordGo Ping Pong Example.
|
||||||
|
|
||||||
|
This is an example bot that will respond to "ping" with "Pong!" and
|
||||||
|
"pong" with "Ping!" on any channel it has access to.
|
||||||
|
|
||||||
|
This example requires the use of an **Authentication Token** for login instead
|
||||||
|
of an email and password. If you don't know your token you can use the
|
||||||
|
**mytoken** example to get it.
|
68
examples/pingpong/pingpong.go
Normal file
68
examples/pingpong/pingpong.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
// This program provides a simple Ping/Pong bot example
|
||||||
|
// using the DiscordGo API package.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Will use this to store the Bot's account ID
|
||||||
|
var BotID string
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
// Check for Token
|
||||||
|
if len(os.Args) != 2 {
|
||||||
|
fmt.Println("You must provide the authentication token for your bot account.")
|
||||||
|
fmt.Println(os.Args[0], " [token]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dg, err := discordgo.New(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error creating Discord session: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the Bot account information.
|
||||||
|
u, err := dg.User("@me")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error obtaining bot 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.
|
||||||
|
dg.Open()
|
||||||
|
|
||||||
|
// Simple way to keep program running until any key press.
|
||||||
|
var input string
|
||||||
|
fmt.Scanln(&input)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function will be called (due to AddHandler above) every time a new
|
||||||
|
// message is created on any channel that the autenticated user 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"
|
||||||
|
if m.Content == "ping" {
|
||||||
|
s.ChannelMessageSend(m.ChannelID, "Pong!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Content == "pong" {
|
||||||
|
s.ChannelMessageSend(m.ChannelID, "Ping!")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue