~kris/9p

glenda

2d91f0f60fc5b52d2428c31608de19bdda8f59e3 — Henesy 4 years ago 5dfede3
oracle
4 files changed, 62 insertions(+), 2 deletions(-)

M .gitignore
M mux.go
M x/mux/config.go
A x/mux/oracle.go
M .gitignore => .gitignore +1 -0
@@ 1,3 1,4 @@
glenda
cfg*/
*.log
oracledbs/

M mux.go => mux.go +3 -1
@@ 62,7 62,9 @@ func init() {

	Router.Route("uptime", "Current bot uptime", Router.Uptime)

	Router.Route("gridlink", "Convert griddisk paths to 'incoming' URL's", Router.GridLink)
	Router.Route("gridlink", "Convert griddisk paths to incoming URLs", Router.GridLink)

	Router.Route("roll", "Roll dice in the form XdY", Router.Roll)

	Router.Route("oracle", "Display oracle readings. Files are (papers fortunes fqa faust pooh).", Router.Oracle)
}

M x/mux/config.go => x/mux/config.go +1 -1
@@ 59,7 59,7 @@ func (c *Configuration) Init(s *discordgo.Session) {
		
		"extrafortunes":	"../sys/games/lib/fortunes",			// Unix
		"fortunes":		"/usr/share/mirror/plan9front/lib/",	// Plan 9
		
		"oracledbs": "./oracledbs/",	// Vendored if you want it
		}
		
	// Regularly dump the config

A x/mux/oracle.go => x/mux/oracle.go +57 -0
@@ 0,0 1,57 @@
package mux

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

// Display conjured fortunes from the oracle
// g!oracle papers factotum is
func (m *Mux) Oracle(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := "```\n"

	// Read relevant file
	path := Config.Db["oracledbs"]

	fields := strings.Fields(dm.Content)
	fields = fields[1:]
	f := "papers"
	prompt := []string{}
	if len(fields) > 1 {
		prompt = fields[1:]
	}

	if strings.Contains(dm.Content, "papers") {
		f = "papers"
	} else if strings.Contains(dm.Content, "fqa") {
		f = "fqa"
	} else if strings.Contains(dm.Content, "pooh") {
		f = "pooh"
	} else if strings.Contains(dm.Content, "faust") {
		f = "faust"
	} else if strings.Contains(dm.Content, "fortunes") {
		f = "fortunes"
	}

	// Linux solution
	var args []string
	args = append(args, "-db", path + f, "-len", "30")
	args = append(args, prompt...)
	//fmt.Println(args)
	out, err := exec.Command("oracle", args...).Output()
	if err != nil {
		fmt.Println("Error calling oracle(1), see: x/mux/oracle.go")
		fmt.Println("%s\n", err)
	}
	resp += string(out)

	resp += "```\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}