~kris/9p

glenda

0f8947618dd176e53ef3d85d96c8973de296771d — Sean Hinchee 8 years ago 6af501f
adding fortunes and some wordage
6 files changed, 59 insertions(+), 12 deletions(-)

M README.md
M main.go
M mux.go
A x/mux/fortunes.go
M x/mux/help.go
M x/mux/mux.go
M README.md => README.md +2 -2
@@ 13,9 13,9 @@ go get github.com/bwmarrin/discordgo

### Usage
```
cd $GOPATH/src/github.com/bwmarrin/disgord
cd $GOPATH/src/bitbucket.org/henesy/glenda
go build
./disgord -t 'Bot BOT_AUTH_TOKEN'
./glenda -t 'Bot BOT_AUTH_TOKEN'
```

## Documentation

M main.go => main.go +3 -6
@@ 1,8 1,5 @@
// Declare this file to be part of the main package so it can be compiled into
// an executable.
package main

// Import all Go packages required for this file.
import (
	"flag"
	"fmt"


@@ 10,12 7,10 @@ import (
	"os"
	"os/signal"
	"syscall"

	"github.com/bwmarrin/discordgo"
)

// Version is a constant that stores the Disgord version information.
const Version = "v0.0.0-alpha"
const Version = "v0.1.0-alpha"

// Session is declared in the global space so it can be easily used
// throughout this program.


@@ 27,6 22,7 @@ var Session, _ = discordgo.New()
func init() {

	// Discord Authentication Token
	// Have to prefix "Bot [Token Here]" or 401 Forbidden
	Session.Token = os.Getenv("DG_TOKEN")
	if Session.Token == "" {
		flag.StringVar(&Session.Token, "t", "", "Discord Authentication Token")


@@ 89,3 85,4 @@ func main() {

	// Exit Normally.
}


M mux.go => mux.go +3 -0
@@ 17,4 17,7 @@ func init() {

	// Register the build-in help command.
	Router.Route("help", "Display this message.", Router.Help)

	Router.Route("fortune", "Display fortunes.", Router.Fortunes)
}


A x/mux/fortunes.go => x/mux/fortunes.go +48 -0
@@ 0,0 1,48 @@
package mux

import (
	"github.com/bwmarrin/discordgo"
	"strings"
	"os/exec"
	"fmt"
)

// Display fortunes from various 9front /lib/ files
func (m *Mux) Fortunes(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := "```\n"

	// Read relevant file
	path := "/usr/share/mirror/plan9front/lib/"

	f := ""
	if strings.Contains(dm.Content, "theo") {
		f = "theo"
	} else if strings.Contains(dm.Content, "troll") {
		f = "troll"
	} else if strings.Contains(dm.Content, "rsc") {
		f = "rsc"
	} else if strings.Contains(dm.Content, "bullshit") {
		f = "bullshit"
	} else if strings.Contains(dm.Content, "terry") {
		f = "terry"
	} else if strings.Contains(dm.Content, "rob") {
		f = "rob"
	} else {
		f = "../sys/games/lib/fortunes"
	}

	// Linux solution
	out, err := exec.Command("fortune", path + f).Output()
	if err != nil {
		fmt.Println("Error calling fortune(1), see: x/mux/fortunes.go")
		fmt.Println("%s\n", err)
	}
	resp += string(out)

	resp += "```\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}


M x/mux/help.go => x/mux/help.go +1 -2
@@ 4,7 4,6 @@ import (
	"fmt"
	"sort"
	"strconv"

	"github.com/bwmarrin/discordgo"
)



@@ 54,7 53,7 @@ func (m *Mux) Help(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	sort.Strings(keys)

	// TODO: Learn more link needs to be configurable
	resp := "\n*Commands can be abbreviated and mixed with other text.  Learn more at <https://github.com/bwmarrin/disgord>*\n"
	resp := "\n*Commands can be abbreviated and mixed with other text.*\n"
	resp += "```autoit\n"

	v, ok := cmdmap["help"]

M x/mux/mux.go => x/mux/mux.go +2 -2
@@ 46,7 46,7 @@ type Mux struct {
// New returns a new Discord message route mux
func New() *Mux {
	m := &Mux{}
	m.Prefix = "-dg "
	m.Prefix = "!g "
	return m
}



@@ 62,7 62,7 @@ func (m *Mux) Route(pattern, desc string, cb HandlerFunc) (*Route, error) {
	return &r, nil
}

// FuzzyMatch attepts to find the best route match for a givin message.
// FuzzyMatch attepts to find the best route match for a given message.
func (m *Mux) FuzzyMatch(msg string) (*Route, []string) {

	// Tokenize the msg string into a slice of words