forked from pothtonswer/discordmuffin
Updates to Ping Pong bot.
This commit is contained in:
parent
d0ea58abb2
commit
e0c0dd8c02
2 changed files with 111 additions and 7 deletions
|
@ -1,9 +1,40 @@
|
||||||
===
|
<img align="right" src="http://bwmarrin.github.io/discordgo/img/discordgo.png">
|
||||||
DiscordGo Ping Pong Example.
|
PingPong Example
|
||||||
|
====
|
||||||
|
|
||||||
This is an example bot that will respond to "ping" with "Pong!" and
|
This example demonstrates how to utilize DiscordGo to create a Ping Pong Bot.
|
||||||
"pong" with "Ping!" on any channel it has access to.
|
|
||||||
|
|
||||||
This example requires the use of an **Authentication Token** for login instead
|
This Bot will respond to "ping" with "Pong!" and "pong" with "Ping!".
|
||||||
of an email and password. If you don't know your token you can use the
|
|
||||||
**mytoken** example to get it.
|
### Build
|
||||||
|
|
||||||
|
This assumes you already have a working Go environment setup and that
|
||||||
|
DiscordGo is correctly installed on your system.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go build
|
||||||
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
You must authenticate using either an Authentication Token or both Email and
|
||||||
|
Password for an account. Keep in mind official Bot accounts only support
|
||||||
|
authenticating via Token.
|
||||||
|
|
||||||
|
```
|
||||||
|
./pingpong --help
|
||||||
|
Usage of ./pingpong:
|
||||||
|
-e string
|
||||||
|
Account Email
|
||||||
|
-p string
|
||||||
|
Account Password
|
||||||
|
-t string
|
||||||
|
Account Token
|
||||||
|
```
|
||||||
|
|
||||||
|
The below example shows how to start the bot using an Email and Password for
|
||||||
|
authentication.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./pingpong -e EmailHere -p PasswordHere
|
||||||
|
```
|
||||||
|
|
73
examples/pingpong/main.go
Normal file
73
examples/pingpong/main.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
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.
|
||||||
|
dg.Open()
|
||||||
|
|
||||||
|
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!")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue