~kris/9p

glenda

ref: 36e2a5b1f0ac25c0952ab9a1c73132f50589861d glenda/x/mux/misc.go -rw-r--r-- 2.7 KiB
36e2a5b1 — Henesy add g!gridlink 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package mux

import (
	"github.com/bwmarrin/discordgo"
	"time"
	"strings"
)

// Chan for communicating to Glenda
var GlendaChan chan string

// Chan for communicating to Mux
var MuxChan chan string

// Chan for communicating with Dump
var dumpChan chan string

// Stores the time that the bot started this boot
var StartTime time.Time


// Multiplex internal channels, initialized once
func CommMux() {
	MuxChan = make(chan string, 5)
	GlendaChan = make(chan string, 5)
	dumpChan = make(chan string)

	// Listen for signals till death do us part
	for {
		select {
		default:
		}

		time.Sleep(500 * time.Millisecond)
	}
}

// Dump configs to file
func dump() string {
	resp := ""

	err := Config.Write()
	if err != nil {
		resp += "Dump config failed. Check logs.\n"
	} else {
		resp += "Ok."
	}
	resp += "\n"
	return resp
}

// Return the current uptime
func (m *Mux) Uptime(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""
	
	resp += time.Now().Sub(StartTime).String()

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// Check authorization as owner
func authorized(dm *discordgo.Message) bool {
	user := dm.Author
	id := user.ID
	
	if id == Config.Db["owner"] {
		return true
	}
	
	return false
}

// Dump config to file
func (m *Mux) Dump(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	if !authorized(dm) {
		ds.ChannelMessageSend(dm.ChannelID, "Only the bot owner can do that.")
		return
	}

	resp := dump()

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// Rewrite griddisk paths as links
// Given /n/griddisk/foo, /foo, foo ⇒ …/incoming/foo
func (m *Mux) GridLink(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""
	base := "http://wiki.9gridchan.org/incoming"
	root := "/n/griddisk"
	
	resp += base
	
	path := ctx.Fields[1]
	
	switch {
	case strings.HasPrefix(path, root):
		// Full path
		sub := strings.TrimPrefix(path, root)
		resp += sub
	
	case path[0] == '/':
		// Root only
		resp += path
	
	default:
		// Any other case
		resp += "/" + path
	}
	
	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// Beer, dude.
func (m *Mux) Beer(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""
	resp += ":beer:"
	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// Whiskey, dude.
func (m *Mux) Whiskey(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""
	resp += ":tumbler_glass:"
	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// Wine, dude.
func (m *Mux) Wine(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""
	resp += ":wine_glass:"
	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}