mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
init
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
config.json
|
||||||
53
bot/bot.go
Normal file
53
bot/bot.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Variables used for command line parameters
|
||||||
|
var (
|
||||||
|
BotID string
|
||||||
|
Session *discordgo.Session
|
||||||
|
)
|
||||||
|
|
||||||
|
func Connect(token string) {
|
||||||
|
// Create a new Discord session using the provided bot token.
|
||||||
|
var err error
|
||||||
|
Session, err = discordgo.New("Bot " + token)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error creating Discord session,", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the account information.
|
||||||
|
u, err := Session.User("@me")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error obtaining account details,", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the account ID for later use.
|
||||||
|
BotID = u.ID
|
||||||
|
|
||||||
|
fmt.Println("Bot connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Start() {
|
||||||
|
// Open the websocket and begin listening.
|
||||||
|
err := Session.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
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddHandler(handler interface{}) {
|
||||||
|
Session.AddHandler(handler)
|
||||||
|
}
|
||||||
37
config/config.go
Normal file
37
config/config.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Variables used for command line parameters
|
||||||
|
var Config configStruct
|
||||||
|
|
||||||
|
type configStruct struct {
|
||||||
|
Token string `json:"Token"`
|
||||||
|
AlertRoomID string `json:"AlertRoomID"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Configure() {
|
||||||
|
|
||||||
|
log.Println("Reading config file...")
|
||||||
|
|
||||||
|
file, e := ioutil.ReadFile("./config.json")
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
log.Printf("File error: %v\n", e)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%s\n", string(file))
|
||||||
|
|
||||||
|
err := json.Unmarshal(file, &Config)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
20
main.go
Normal file
20
main.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"./bot"
|
||||||
|
"./config"
|
||||||
|
"./serverstatus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Variables used for command line parameters
|
||||||
|
var (
|
||||||
|
BotID string
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
config.Configure()
|
||||||
|
bot.Connect(config.Config.Token)
|
||||||
|
serverstatus.Start()
|
||||||
|
bot.Start()
|
||||||
|
|
||||||
|
}
|
||||||
7
readme.md
Normal file
7
readme.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# GoBot
|
||||||
|
|
||||||
|
My experimental Discord bot
|
||||||
|
|
||||||
|
## Server Status
|
||||||
|
|
||||||
|
This scans the Elysium PvP server checking to see if it is up.
|
||||||
34
serverstatus/serverstatus.go
Normal file
34
serverstatus/serverstatus.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package serverstatus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"../bot"
|
||||||
|
"../config"
|
||||||
|
"github.com/anvie/port-scanner"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Start() {
|
||||||
|
go loop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func loop() {
|
||||||
|
prevServerUp := true
|
||||||
|
elysiumPvP := portscanner.NewPortScanner("149.202.207.235", time.Second*2)
|
||||||
|
|
||||||
|
for {
|
||||||
|
serverUp := elysiumPvP.IsOpen(8099)
|
||||||
|
|
||||||
|
if serverUp && serverUp != prevServerUp {
|
||||||
|
sendMessage("@everyone Elysium PVP is now online!")
|
||||||
|
} else if !serverUp && serverUp != prevServerUp {
|
||||||
|
sendMessage("@everyone Elysium PVP is offline.")
|
||||||
|
}
|
||||||
|
|
||||||
|
prevServerUp = serverUp
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendMessage(message string) {
|
||||||
|
bot.Session.ChannelMessageSend(config.Config.AlertRoomID, message)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user