packaged up server, added mutex
This commit is contained in:
		
							parent
							
								
									a8128b120a
								
							
						
					
					
						commit
						062e9276f2
					
				@ -13,7 +13,7 @@ import (
 | 
			
		||||
 | 
			
		||||
/* --------		DB Helper functions		-------- */
 | 
			
		||||
 | 
			
		||||
func openDB(dsn string) (*sql.DB, error){
 | 
			
		||||
func openDB(dsn string) (*sql.DB, error) {
 | 
			
		||||
	db, err := sql.Open("mysql", dsn)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@ -25,7 +25,7 @@ func openDB(dsn string) (*sql.DB, error){
 | 
			
		||||
	return db, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *application) updateAllBadWords() (error) {
 | 
			
		||||
func (app *application) updateAllBadWords() error {
 | 
			
		||||
	var err error
 | 
			
		||||
	app.allBadWords, err = app.badwords.AllWords()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
@ -10,6 +10,7 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/bwmarrin/discordgo"
 | 
			
		||||
	"quenten.nl/pepebot/discord/mux"
 | 
			
		||||
	"quenten.nl/pepebot/limiter"
 | 
			
		||||
	"quenten.nl/pepebot/models/mysql"
 | 
			
		||||
)
 | 
			
		||||
@ -25,10 +26,7 @@ type application struct {
 | 
			
		||||
	trigger     string
 | 
			
		||||
	allBadWords map[string][]string
 | 
			
		||||
	limiter     *limiter.Limiter
 | 
			
		||||
	commandMux  *CommandMux
 | 
			
		||||
 | 
			
		||||
	active bool
 | 
			
		||||
	stop   bool
 | 
			
		||||
	commandMux  *mux.CommandMux
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
@ -62,8 +60,8 @@ func main() {
 | 
			
		||||
		Logs:      make(map[string][]*limiter.Action),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mux := NewCommandMux()
 | 
			
		||||
	mux.prefix = "!pepe"
 | 
			
		||||
	server := mux.NewCommandMux()
 | 
			
		||||
	server.Prefix = "!pepe"
 | 
			
		||||
 | 
			
		||||
	app := &application{
 | 
			
		||||
		infoLog:    infoLog,
 | 
			
		||||
@ -72,7 +70,7 @@ func main() {
 | 
			
		||||
		adminroles: &mysql.AdminRolesModel{DB: db},
 | 
			
		||||
		trigger:    "!pepe",
 | 
			
		||||
		limiter:    limiter,
 | 
			
		||||
		commandMux: mux,
 | 
			
		||||
		commandMux: server,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	app.allBadWords, err = app.badwords.AllWords()
 | 
			
		||||
@ -80,12 +78,12 @@ func main() {
 | 
			
		||||
		app.errorLog.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mux.HandleFunc("cringe", app.sendCringe)
 | 
			
		||||
	mux.HandleFunc("gif", app.sendNigelGif)
 | 
			
		||||
	mux.HandleFunc("tuesday", app.sendTuesday)
 | 
			
		||||
	mux.HandleFunc("wednesday", app.sendWednesday)
 | 
			
		||||
	mux.HandleFunc("github", app.sendGithub)
 | 
			
		||||
	mux.HandleFunc("source", app.sendGithub)
 | 
			
		||||
	server.HandleFunc("cringe", app.sendCringe)
 | 
			
		||||
	server.HandleFunc("gif", app.sendNigelGif)
 | 
			
		||||
	server.HandleFunc("tuesday", app.sendTuesday)
 | 
			
		||||
	server.HandleFunc("wednesday", app.sendWednesday)
 | 
			
		||||
	server.HandleFunc("github", app.sendGithub)
 | 
			
		||||
	server.HandleFunc("source", app.sendGithub)
 | 
			
		||||
	/* The admin commands are left out for now.
 | 
			
		||||
	They have specialised functions and don't work yet.
 | 
			
		||||
	Their code is left unworking and nonfunctional to be fixed
 | 
			
		||||
@ -95,7 +93,7 @@ func main() {
 | 
			
		||||
	It goes underused and has had it's joke.
 | 
			
		||||
 | 
			
		||||
	Oh and no one must be sad to see the death of the spam command...*/
 | 
			
		||||
	mux.HandleFunc(mux.prefix, app.sendPepe)
 | 
			
		||||
	server.HandleFunc(server.Prefix, app.sendPepe)
 | 
			
		||||
 | 
			
		||||
	/* token, err := app.readAuthToken()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,14 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "github.com/bwmarrin/discordgo"
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/bwmarrin/discordgo"
 | 
			
		||||
	"quenten.nl/pepebot/discord/mux"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (app *application) LogToConsole(next Command) Command {
 | 
			
		||||
func (app *application) LogToConsole(next mux.Command) mux.Command {
 | 
			
		||||
	fn := func(s *discordgo.Session, m *discordgo.MessageCreate) {
 | 
			
		||||
		app.infoLog.Printf("%s \tsaid: %s\n", m.Author.Username, m.Content)
 | 
			
		||||
		next.Execute(s, m)
 | 
			
		||||
	}
 | 
			
		||||
	return HandlerFunc(fn)
 | 
			
		||||
	return mux.HandlerFunc(fn)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,14 +1,12 @@
 | 
			
		||||
package main
 | 
			
		||||
package mux
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/bwmarrin/discordgo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/* The Command interface is a template for the implementation of a command of the discord bot.
 | 
			
		||||
The actual command will be executed in the Execute function.
 | 
			
		||||
All the actual Command objects will be (similarly to Handlers in the net/http package) put into a CommandMux */
 | 
			
		||||
type Command interface {
 | 
			
		||||
	Execute(s *discordgo.Session, m *discordgo.MessageCreate)
 | 
			
		||||
}
 | 
			
		||||
@ -21,8 +19,9 @@ func (f HandlerFunc) Execute(s *discordgo.Session, m *discordgo.MessageCreate) {
 | 
			
		||||
 | 
			
		||||
/* The CommandMux struct is a type of mux for Discord commands. It's modelled after the net/http ServeMux */
 | 
			
		||||
type CommandMux struct {
 | 
			
		||||
	mu     sync.RWMutex
 | 
			
		||||
	m      map[string]muxEntry
 | 
			
		||||
	prefix string
 | 
			
		||||
	Prefix string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewCommandMux() *CommandMux { return new(CommandMux) }
 | 
			
		||||
@ -44,12 +43,14 @@ func (c *CommandMux) firstCommand(command string) string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *CommandMux) Handler(m *discordgo.MessageCreate) (cmd Command, pattern string) {
 | 
			
		||||
	if strings.HasPrefix(m.Content, c.prefix) {
 | 
			
		||||
	c.mu.RLock()
 | 
			
		||||
	defer c.mu.RUnlock()
 | 
			
		||||
	if strings.HasPrefix(m.Content, c.Prefix) {
 | 
			
		||||
		/* Special case for this bot alone. It has a command that is only it's prefix
 | 
			
		||||
		So we check if the whole message is only the prefix before proceding.
 | 
			
		||||
		So please don't forget to add the command, since it's totally hardcoded here. */
 | 
			
		||||
		if strings.TrimSpace(m.Content) == c.prefix {
 | 
			
		||||
			return c.m[c.prefix].h, c.m[c.prefix].pattern
 | 
			
		||||
		if strings.TrimSpace(m.Content) == c.Prefix {
 | 
			
		||||
			return c.m[c.Prefix].h, c.m[c.Prefix].pattern
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		m := c.removeFirst(m.Content) /* Here the prefix is removed, so we're left with only the first keyword */
 | 
			
		||||
@ -73,6 +74,8 @@ func (c *CommandMux) Execute(s *discordgo.Session, m *discordgo.MessageCreate) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *CommandMux) Handle(pattern string, handler Command) {
 | 
			
		||||
	c.mu.Lock()
 | 
			
		||||
	defer c.mu.Unlock()
 | 
			
		||||
	if pattern == "" {
 | 
			
		||||
		panic("commandmux: invalid pattern")
 | 
			
		||||
	}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user