Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Implemented optional database #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/url"
"regexp"
"trup/db"

"github.com/bwmarrin/discordgo"
)
Expand Down Expand Up @@ -36,14 +37,14 @@ var Commands = map[string]Command{
Usage: modpingUsage,
Help: modpingHelp,
},
"fetch": {
"fetch": needsDatabase(Command{
Exec: fetch,
Usage: fetchUsage,
},
"setfetch": {
}),
"setfetch": needsDatabase(Command{
Exec: setFetch,
Help: setFetchHelp,
},
}),
"repo": {
Exec: repo,
Help: repoHelp,
Expand All @@ -52,21 +53,21 @@ var Commands = map[string]Command{
Exec: move,
Usage: moveUsage,
},
"git": {
"git": needsDatabase(Command{
Exec: git,
Usage: gitUsage,
Help: gitHelp,
},
"dotfiles": {
}),
"dotfiles": needsDatabase(Command{
Exec: dotfiles,
Usage: dotfilesUsage,
Help: dotfilesHelp,
},
"desc": {
}),
"desc": needsDatabase(Command{
Exec: desc,
Usage: descUsage,
Help: descHelp,
},
}),
"role": {
Exec: role,
Usage: roleUsage,
Expand All @@ -86,14 +87,14 @@ var Commands = map[string]Command{
Usage: purgeUsage,
Help: purgeHelp,
}),
"note": moderatorOnly(Command{
"note": moderatorOnly(needsDatabase(Command{
Exec: note,
Usage: noteUsage,
}),
"warn": moderatorOnly(Command{
})),
"warn": moderatorOnly(needsDatabase(Command{
Exec: warn,
Usage: warnUsage,
}),
})),
}

var parseMentionRegexp = regexp.MustCompile(`<@!?(\d+)>`)
Expand Down Expand Up @@ -169,6 +170,22 @@ func moderatorOnly(cmd Command) Command {
}
}

func needsDatabase(cmd Command) Command {
return Command{
Exec: func(ctx *Context, args []string) {
if !db.DatabaseUsable() {
ctx.Reply("this command is dependant on a database but that feature is disabled.")
return
}

cmd.Exec(ctx, args)
},
Usage: cmd.Usage,
Help: cmd.Help,
ModeratorOnly: cmd.ModeratorOnly,
}
}

func (ctx *Context) isModerator() bool {
for _, r := range ctx.Message.Member.Roles {
if r == ctx.Env.RoleMod {
Expand Down
15 changes: 13 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ package db
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
"log"
"os"
)

var db *pgxpool.Pool

func DatabaseUsable() bool {
return db != nil
}

func init() {
conn, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
databaseUrl := os.Getenv("DATABASE_URL")
if databaseUrl == "" {
log.Println("Database disabled")
return
}
conn, err := pgxpool.Connect(context.Background(), databaseUrl)
if err != nil {
panic(err)
log.Printf("Unable to connect to the database, the database features will be disabled:\n%s\n", err)
return
}
db = conn
}