Browse Source

Inital add of example bot

Bruce Marriner 9 years ago
parent
commit
a5336e53da
3 changed files with 275 additions and 0 deletions
  1. 92 0
      bot/admin.go
  2. 61 0
      bot/dgbot.go
  3. 122 0
      bot/events.go

+ 92 - 0
bot/admin.go

@@ -0,0 +1,92 @@
+package main
+
+import (
+	"fmt"
+	"strings"
+)
+
+//
+func admin(line string) (response string) {
+
+	var err error
+
+	// trim any leading or trailing space off the whole line
+	line = strings.TrimSpace(line)
+
+	// split the command from the rest
+	split := strings.SplitN(line, " ", 2)
+
+	// store the command and payload seperately
+	command := strings.ToLower(split[0])
+	command = strings.TrimPrefix(command, "~")
+
+	var payload string = ""
+	if len(split) > 1 {
+		payload = split[1]
+	}
+
+	if command == "help" {
+		response += fmt.Sprintln("`~help ...............` Display this help text")
+		response += fmt.Sprintln("`~username [string]...` Set login username to [string]")
+		response += fmt.Sprintln("`~password [string]...` Set login password to [string]")
+		response += fmt.Sprintln("`~login ..............` Login to Discord")
+		response += fmt.Sprintln("`~listen .............` Start websocket listener")
+		response += fmt.Sprintln("`~logout .............` Logout from Discord")
+		return
+	}
+
+	if command == "username" {
+		Username = payload
+		response += "Done."
+		return
+	}
+
+	if command == "password" {
+		Password = payload
+		response += "Done."
+		return
+	}
+
+	if command == "login" {
+		Session.Token, err = Session.Login(Username, Password)
+		if err != nil {
+			fmt.Println("Unable to login to Discord.")
+			fmt.Println(err)
+		}
+		response += "Done."
+		return
+	}
+
+	if command == "listen" {
+
+		// open connection
+		err = Session.Open()
+		if err != nil {
+			fmt.Println(err)
+		}
+
+		// Do Handshake? (dumb name)
+		err = Session.Handshake()
+		if err != nil {
+			fmt.Println(err)
+		}
+
+		// Now listen for events / messages
+		go Session.Listen()
+		response += "Done."
+		return
+	}
+
+	if command == "logout" {
+		err = Session.Logout()
+		if err != nil {
+			fmt.Println("Unable to logout from Discord.")
+			fmt.Println(err)
+		}
+		response += "Done."
+		return
+	}
+
+	response += "I'm sorry I don't understand that command.  Try ~help"
+	return
+}

+ 61 - 0
bot/dgbot.go

@@ -0,0 +1,61 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"math/rand"
+	"os"
+	"time"
+
+	Discord "github.com/bwmarrin/discordgo"
+)
+
+// Global Variables
+var (
+	Session  Discord.Session
+	Username string
+	Password string
+)
+
+func main() {
+	fmt.Printf("\nDiscordgo Bot Starting.\n\n")
+
+	// Register all the Event Handlers
+	RegisterHandlers()
+
+	// read in the config file.
+	ParseFile()
+
+	// seed the random number generator
+	rand.Seed(time.Now().UTC().UnixNano())
+
+	// main program loop to keep dgbot running
+	// will add stuff here to track goroutines
+	// and monitor for CTRL-C or other things.
+	for {
+		time.Sleep(1000 * time.Millisecond)
+	}
+
+	fmt.Println("\nDiscordgo Bot shutting down.\n")
+}
+
+// ParseFile will read a file .dgbotrc and run all included
+// commands
+func ParseFile() {
+	file, err := os.Open(".dgbotrc")
+	if err != nil {
+		return
+	}
+	defer file.Close()
+
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		fmt.Println(scanner.Text())
+		fmt.Println(admin(scanner.Text()))
+	}
+
+	if err := scanner.Err(); err != nil {
+		log.Fatal(err)
+	}
+}

+ 122 - 0
bot/events.go

@@ -0,0 +1,122 @@
+package main
+
+import (
+	Discord "github.com/bwmarrin/discordgo"
+)
+
+// Registers all event handlers
+func RegisterHandlers() {
+
+	Session = Discord.Session{
+		OnEvent:                   OnEvent,
+		OnReady:                   OnReady,
+		OnTypingStart:             OnTypingStart,
+		OnMessageCreate:           OnMessageCreate,
+		OnMessageUpdate:           OnMessageUpdate,
+		OnMessageDelete:           OnMessageDelete,
+		OnMessageAck:              OnMessageAck,
+		OnVoiceStateUpdate:        OnVoiceStateUpdate,
+		OnPresenceUpdate:          OnPresenceUpdate,
+		OnChannelCreate:           OnChannelCreate,
+		OnChannelUpdate:           OnChannelUpdate,
+		OnGuildCreate:             OnGuildCreate,
+		OnGuildUpdate:             OnGuildUpdate,
+		OnGuildDelete:             OnGuildDelete,
+		OnGuildRoleCreate:         OnGuildRoleCreate,
+		OnGuildRoleUpdate:         OnGuildRoleUpdate,
+		OnGuildRoleDelete:         OnGuildRoleDelete,
+		OnGuildMemberAdd:          OnGuildMemberAdd,
+		OnGuildMemberUpdate:       OnGuildMemberUpdate,
+		OnGuildMemberRemove:       OnGuildMemberRemove,
+		OnGuildIntegrationsUpdate: OnGuildIntegrationsUpdate,
+	}
+
+}
+
+// OnEvent is called for unknown events or unhandled events.  It provides
+// a generic interface to handle them.
+func OnEvent(s *Discord.Session, e Discord.Event) {
+	// Add code here to handle this event.
+}
+
+// OnReady is called when Discordgo receives a READY event
+// This event must be handled and must contain the Heartbeat call.
+func OnReady(s *Discord.Session, st Discord.Ready) {
+
+	// start the Heartbeat
+	go s.Heartbeat(st.HeartbeatInterval)
+
+	// Add code here to handle this event.
+}
+
+func OnTypingStart(s *Discord.Session, st Discord.TypingStart) {
+	// Add code here to handle this event.
+}
+
+func OnPresenceUpdate(s *Discord.Session, st Discord.PresenceUpdate) {
+	// Add code here to handle this event.
+}
+
+func OnMessageCreate(s *Discord.Session, m Discord.Message) {
+	// Add code here to handle this event.
+}
+
+func OnMessageUpdate(s *Discord.Session, m Discord.Message) {
+	// Add code here to handle this event.
+}
+
+func OnMessageAck(s *Discord.Session, st Discord.MessageAck) {
+	// Add code here to handle this event.
+}
+
+func OnMessageDelete(s *Discord.Session, st Discord.MessageDelete) {
+	// Add code here to handle this event.
+}
+
+func OnVoiceStateUpdate(s *Discord.Session, st Discord.VoiceState) {
+	// Add code here to handle this event.
+}
+
+func OnChannelCreate(s *Discord.Session, st Discord.Channel) {
+	// Add code here to handle this event.
+}
+
+func OnChannelUpdate(s *Discord.Session, st Discord.Channel) {
+	// Add code here to handle this event.
+}
+
+func OnGuildCreate(s *Discord.Session, st Discord.Guild) {
+	// Add code here to handle this event.
+}
+
+func OnGuildUpdate(s *Discord.Session, st Discord.Guild) {
+	// Add code here to handle this event.
+}
+func OnGuildDelete(s *Discord.Session, st Discord.Guild) {
+	// Add code here to handle this event.
+}
+
+func OnGuildRoleCreate(s *Discord.Session, st Discord.GuildRole) {
+	// Add code here to handle this event.
+}
+func OnGuildRoleUpdate(s *Discord.Session, st Discord.GuildRole) {
+	// Add code here to handle this event.
+}
+func OnGuildRoleDelete(s *Discord.Session, st Discord.GuildRoleDelete) {
+	// Add code here to handle this event.
+}
+func OnGuildMemberAdd(s *Discord.Session, st Discord.Member) {
+	// Add code here to handle this event.
+}
+
+func OnGuildMemberUpdate(s *Discord.Session, st Discord.Member) {
+	// Add code here to handle this event.
+}
+
+func OnGuildMemberRemove(s *Discord.Session, st Discord.Member) {
+	// Add code here to handle this event.
+}
+
+func OnGuildIntegrationsUpdate(s *Discord.Session, st Discord.GuildIntegrationsUpdate) {
+	// Add code here to handle this event.
+}