-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from EsefexBot/dev-api
Permission System & Improved Commands
- Loading branch information
Showing
130 changed files
with
4,717 additions
and
981 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
linters: | ||
# enable-all: true | ||
disable: | ||
- forbidigo | ||
- golint | ||
- depguard | ||
- varnamelen | ||
- ifshort | ||
- nosnakecase | ||
- deadcode | ||
- varcheck | ||
- exhaustivestruct | ||
- maligned | ||
- interfacer | ||
- scopelint | ||
- structcheck | ||
|
||
run: | ||
skip-files: | ||
- ".+_testing.go$" # skip files for quick testing of fuctionality | ||
skip-dirs: | ||
- "cmd/testing" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch file", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "main.go", | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Discord Commands | ||
|
||
```go | ||
/help -> shows general Help | ||
|
||
/sound -> shows sound help | ||
/sound upload -> upload sound | ||
/sound list -> list sounds | ||
/sound delete -> delete sounds | ||
/sound modify -> modify sounds | ||
|
||
/permission -> shows permissions help | ||
/permission set role <roleid> <permissionID> <value> -> sets permissions for user | ||
/permission set user <userID> <permissionID> <value> | ||
/permission set channel <channelID> <permissionID> <value> | ||
|
||
/permission get role/user/channel -> gets permission value for role/user/channel | ||
/permission list role/user/channel -> list all permissions for role/user/channel | ||
/permission clear role/user/channel -> clears permissions for role/user/channel | ||
|
||
/bot -> shows bot help | ||
/bot join -> makes bot join | ||
/bot leave -> makes bot leave | ||
/bot stats -> displays bot stats | ||
|
||
/config -> change bot config | ||
|
||
/user link -> link user | ||
/user unlink -> unlink user | ||
/user stats -> show user stats | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
package middleware | ||
|
||
import "esefexapi/db" | ||
import ( | ||
"esefexapi/db" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
type Middleware struct { | ||
dbs *db.Databases | ||
ds *discordgo.Session | ||
} | ||
|
||
func NewMiddleware(dbs *db.Databases) *Middleware { | ||
func NewMiddleware(dbs *db.Databases, ds *discordgo.Session) *Middleware { | ||
return &Middleware{ | ||
dbs: dbs, | ||
ds: ds, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package middleware | ||
|
||
import ( | ||
"esefexapi/permissions" | ||
"esefexapi/types" | ||
"esefexapi/userdb" | ||
"esefexapi/util/dcgoutil" | ||
"esefexapi/util/refl" | ||
"net/http" | ||
) | ||
|
||
func (m *Middleware) Permission(next http.Handler, perms ...string) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Get the user from the request context | ||
user := r.Context().Value("user").(userdb.User) | ||
userChan, err := dcgoutil.UserVCAny(m.ds, user.ID) | ||
if err != nil { | ||
errorMsg := "Error getting user channel: " + err.Error() | ||
http.Error(w, errorMsg, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if userChan.IsNone() { | ||
errorMsg := "User is not in a voice channel" | ||
http.Error(w, errorMsg, http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
p, err := m.dbs.PermissionDB.Query(types.GuildID(userChan.Unwrap().ChannelID), user.ID) | ||
if err != nil { | ||
errorMsg := "Error querying permissions: " + err.Error() | ||
http.Error(w, errorMsg, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Check if the user has any of the required permissions | ||
for _, perm := range perms { | ||
ps, err := refl.GetNestedFieldValue(p, perm) | ||
if err != nil { | ||
errorMsg := "Error getting nested field value: " + err.Error() | ||
http.Error(w, errorMsg, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if !ps.(permissions.PermissionState).Allowed() { | ||
errorMsg := "User does not have the required permissions" | ||
http.Error(w, errorMsg, http.StatusUnauthorized) | ||
return | ||
} | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package routes | ||
|
||
import ( | ||
"esefexapi/types" | ||
"esefexapi/util/dcgoutil" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// api/guild | ||
// returns the guild a user is connected to | ||
func (h *RouteHandlers) GetGuild() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
userID := r.Context().Value("user").(types.UserID) | ||
Ovs, err := dcgoutil.UserVCAny(h.ds, userID) | ||
if err != nil { | ||
errorMsg := "Error getting user Voice State" | ||
http.Error(w, errorMsg, http.StatusInternalServerError) | ||
return | ||
} | ||
if Ovs.IsNone() { | ||
http.Error(w, "User not connected to guild channel", http.StatusForbidden) | ||
return | ||
} | ||
|
||
guildID := Ovs.Unwrap().GuildID | ||
_, err = w.Write([]byte(guildID)) | ||
if err != nil { | ||
log.Println(err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
} |
Oops, something went wrong.