mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 08:32:48 +00:00
use permissions from database - updated bot-scripts
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,7 +3,8 @@ dist
|
|||||||
node_modules
|
node_modules
|
||||||
yarn-error*
|
yarn-error*
|
||||||
vendor
|
vendor
|
||||||
bot
|
/bot
|
||||||
|
/bot-scripts
|
||||||
debug
|
debug
|
||||||
go-discord-bot
|
go-discord-bot
|
||||||
.wwp-cache
|
.wwp-cache
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ RUN apk add --no-cache git alpine-sdk pkgconfig opus-dev opusfile-dev
|
|||||||
RUN go get -u github.com/gobuffalo/packr/...
|
RUN go get -u github.com/gobuffalo/packr/...
|
||||||
RUN go get -u github.com/golang/dep/cmd/dep
|
RUN go get -u github.com/golang/dep/cmd/dep
|
||||||
RUN dep ensure
|
RUN dep ensure
|
||||||
RUN packr build -o /build/server
|
RUN packr build -o /build/bot
|
||||||
|
RUN go build -o /build/bot-scripts ./scripts
|
||||||
|
|
||||||
|
|
||||||
FROM wernight/youtube-dl
|
FROM wernight/youtube-dl
|
||||||
@@ -24,6 +25,6 @@ RUN apk update
|
|||||||
RUN apk add ca-certificates opus-dev opusfile-dev
|
RUN apk add ca-certificates opus-dev opusfile-dev
|
||||||
|
|
||||||
WORKDIR /bot
|
WORKDIR /bot
|
||||||
COPY --from=1 /build/server /
|
COPY --from=1 /build /server
|
||||||
|
|
||||||
ENTRYPOINT ["/server"]
|
ENTRYPOINT ["/server/bot"]
|
||||||
|
|||||||
@@ -7,11 +7,6 @@
|
|||||||
|
|
||||||
"bot_prefix": "#",
|
"bot_prefix": "#",
|
||||||
|
|
||||||
"admin_emails": ["mail@example.com"],
|
|
||||||
"mod_emails": ["mail@example.com"],
|
|
||||||
|
|
||||||
"jwt_secret": "generate a random secret string here",
|
"jwt_secret": "generate a random secret string here",
|
||||||
"server_addr": "0.0.0.0:80",
|
"server_addr": "0.0.0.0:80"
|
||||||
|
|
||||||
"logger": true
|
|
||||||
}
|
}
|
||||||
|
|||||||
3
makefile
3
makefile
@@ -5,6 +5,9 @@ install:
|
|||||||
build-server:
|
build-server:
|
||||||
cd ./server && packr build -o ../bot ./main.go
|
cd ./server && packr build -o ../bot ./main.go
|
||||||
|
|
||||||
|
build-scripts:
|
||||||
|
go build -o bot-scripts ./server/scripts
|
||||||
|
|
||||||
build-client:
|
build-client:
|
||||||
cd client && npm run build
|
cd client && npm run build
|
||||||
|
|
||||||
|
|||||||
16
readme.md
16
readme.md
@@ -33,6 +33,22 @@ services:
|
|||||||
- <path to your data directory>:/bot
|
- <path to your data directory>:/bot
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Running Bot Scripts
|
||||||
|
|
||||||
|
Use the following scripts
|
||||||
|
|
||||||
|
- restore-messages
|
||||||
|
- used to search message history and store in database
|
||||||
|
- update-db
|
||||||
|
- used to run additional DB change scripts (will likely never have to be run)
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
docker-compose exec go-discord-bot /server/bot-scripts update-db
|
||||||
|
docker-compose exec go-discord-bot /server/bot-scripts restore-message <roomID>
|
||||||
|
```
|
||||||
|
|
||||||
### Commands
|
### Commands
|
||||||
|
|
||||||
- `clip` - clips the past minute of audio (currently bugged if more than one user is speaking)
|
- `clip` - clips the past minute of audio (currently bugged if more than one user is speaking)
|
||||||
|
|||||||
9
server/Gopkg.lock
generated
9
server/Gopkg.lock
generated
@@ -94,6 +94,14 @@
|
|||||||
pruneopts = "UT"
|
pruneopts = "UT"
|
||||||
revision = "0f2e0b4fc6cd5710fddbb74ba2e5e02c1c1bc22b"
|
revision = "0f2e0b4fc6cd5710fddbb74ba2e5e02c1c1bc22b"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
branch = "master"
|
||||||
|
digest = "1:5c3444689562053b027ef3b96372e306adbe0d7d109b6cdd48d01eb80f8bab14"
|
||||||
|
name = "github.com/jinzhu/copier"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "7e38e58719c33e0d44d585c4ab477a30f8cb82dd"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
digest = "1:2a21d36a5ab33e6e4ce82b3c7ba45c2330ca6e9af247382475e056b06395b8a9"
|
digest = "1:2a21d36a5ab33e6e4ce82b3c7ba45c2330ca6e9af247382475e056b06395b8a9"
|
||||||
name = "github.com/jinzhu/gorm"
|
name = "github.com/jinzhu/gorm"
|
||||||
@@ -287,6 +295,7 @@
|
|||||||
"github.com/go-audio/wav",
|
"github.com/go-audio/wav",
|
||||||
"github.com/gobuffalo/packr",
|
"github.com/gobuffalo/packr",
|
||||||
"github.com/hraban/opus",
|
"github.com/hraban/opus",
|
||||||
|
"github.com/jinzhu/copier",
|
||||||
"github.com/jinzhu/gorm",
|
"github.com/jinzhu/gorm",
|
||||||
"github.com/jinzhu/gorm/dialects/sqlite",
|
"github.com/jinzhu/gorm/dialects/sqlite",
|
||||||
"github.com/rylio/ytdl",
|
"github.com/rylio/ytdl",
|
||||||
|
|||||||
@@ -64,6 +64,10 @@
|
|||||||
name = "gopkg.in/dgrijalva/jwt-go.v3"
|
name = "gopkg.in/dgrijalva/jwt-go.v3"
|
||||||
version = "3.2.0"
|
version = "3.2.0"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
branch = "master"
|
||||||
|
name = "github.com/jinzhu/copier"
|
||||||
|
|
||||||
[prune]
|
[prune]
|
||||||
go-tests = true
|
go-tests = true
|
||||||
unused-packages = true
|
unused-packages = true
|
||||||
|
|||||||
@@ -16,10 +16,7 @@ func Start(token string) *discordgo.Session {
|
|||||||
|
|
||||||
// add bot handlers
|
// add bot handlers
|
||||||
addHandler(session, bothandlers.SoundsHandler)
|
addHandler(session, bothandlers.SoundsHandler)
|
||||||
|
addHandler(session, bothandlers.LoggerHandler)
|
||||||
if config.Config.Logger {
|
|
||||||
addHandler(session, bothandlers.LoggerHandler)
|
|
||||||
}
|
|
||||||
|
|
||||||
// start listening for commands
|
// start listening for commands
|
||||||
startListener(session)
|
startListener(session)
|
||||||
|
|||||||
@@ -13,17 +13,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type configType struct {
|
type configType struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `json:"client_id"`
|
||||||
ClientSecret string `json:"client_secret"`
|
ClientSecret string `json:"client_secret"`
|
||||||
RedirectURI string `json:"redirect_uri"`
|
RedirectURI string `json:"redirect_uri"`
|
||||||
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
|
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
|
||||||
AdminEmails []string `json:"admin_emails"`
|
ServerAddr string `json:"server_addr"`
|
||||||
ModEmails []string `json:"mod_emails"`
|
JWTSecret string `json:"jwt_secret"`
|
||||||
ServerAddr string `json:"server_addr"`
|
DefaultRoomID string `json:"default_room_id"`
|
||||||
JWTSecret string `json:"jwt_secret"`
|
|
||||||
Logger bool `json:"logger"`
|
|
||||||
DefaultRoomID string `json:"default_room_id"`
|
|
||||||
|
|
||||||
// hard coded folder paths
|
// hard coded folder paths
|
||||||
SoundsPath string
|
SoundsPath string
|
||||||
|
|||||||
27
server/scripts/main.go
Normal file
27
server/scripts/main.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
if len(os.Args) == 1 {
|
||||||
|
fmt.Print("Error - Invalid Args\n\nPossible Args:\nrestore-message\nupdate-db\n\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
arg := os.Args[1]
|
||||||
|
|
||||||
|
switch arg {
|
||||||
|
case "update-db":
|
||||||
|
updateDB()
|
||||||
|
case "restore-messages":
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
fmt.Println("Please provide a channel id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
restoreMessages(os.Args[2])
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
/**
|
||||||
|
This script will fetch all messages for the provided channel and store them in the database.
|
||||||
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@@ -10,13 +14,15 @@ import (
|
|||||||
"github.com/mgerb/go-discord-bot/server/db"
|
"github.com/mgerb/go-discord-bot/server/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
const everyoneChannel = "101198129352691712"
|
// var everyoneChannel = "101198129352691712"
|
||||||
|
var everyoneChannel string
|
||||||
|
|
||||||
// this is a script to go through chat history and log old message into database
|
// this is a script to go through chat history and log old message into database
|
||||||
func main() {
|
func restoreMessages(channelID string) {
|
||||||
config.Init()
|
config.Init()
|
||||||
db.Init()
|
db.Init()
|
||||||
session := bot.Start(config.Config.Token)
|
session := bot.Start(config.Config.Token)
|
||||||
|
everyoneChannel = channelID
|
||||||
fetchMessages(session, "")
|
fetchMessages(session, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
25
server/scripts/update-db.go
Normal file
25
server/scripts/update-db.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/mgerb/go-discord-bot/server/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
// keep change script embedded in go files for ease of use
|
||||||
|
const changeScript = `
|
||||||
|
|
||||||
|
-- script to update all users default permission to a value of 1 (user)
|
||||||
|
|
||||||
|
UPDATE users SET permissions = 1 WHERE permissions IS NULL OR permissions = 0;
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
func updateDB() {
|
||||||
|
db.Init()
|
||||||
|
conn := db.GetConn()
|
||||||
|
err := conn.Exec(changeScript).Error
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,22 +31,12 @@ type CustomClaims struct {
|
|||||||
// GetJWT - get json web token
|
// GetJWT - get json web token
|
||||||
func GetJWT(user model.User) (string, error) {
|
func GetJWT(user model.User) (string, error) {
|
||||||
|
|
||||||
permissions := PermUser
|
|
||||||
|
|
||||||
if checkEmailPermissions(user.Email, config.Config.ModEmails) {
|
|
||||||
permissions = PermMod
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkEmailPermissions(user.Email, config.Config.AdminEmails) {
|
|
||||||
permissions = PermAdmin
|
|
||||||
}
|
|
||||||
|
|
||||||
claims := CustomClaims{
|
claims := CustomClaims{
|
||||||
user.ID,
|
user.ID,
|
||||||
user.Username,
|
user.Username,
|
||||||
user.Discriminator,
|
user.Discriminator,
|
||||||
user.Email,
|
user.Email,
|
||||||
permissions,
|
*user.Permissions,
|
||||||
jwt.StandardClaims{
|
jwt.StandardClaims{
|
||||||
ExpiresAt: time.Now().AddDate(0, 1, 0).Unix(), // one month
|
ExpiresAt: time.Now().AddDate(0, 1, 0).Unix(), // one month
|
||||||
Issuer: "Go Discord Bot",
|
Issuer: "Go Discord Bot",
|
||||||
@@ -57,15 +47,6 @@ func GetJWT(user model.User) (string, error) {
|
|||||||
return token.SignedString([]byte(config.Config.JWTSecret))
|
return token.SignedString([]byte(config.Config.JWTSecret))
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkEmailPermissions(email string, emails []string) bool {
|
|
||||||
for _, e := range emails {
|
|
||||||
if email == e {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// AuthPermissions - secure end points based on auth levels
|
// AuthPermissions - secure end points based on auth levels
|
||||||
func AuthPermissions(p int) gin.HandlerFunc {
|
func AuthPermissions(p int) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package model
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jinzhu/copier"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,9 +21,15 @@ type User struct {
|
|||||||
Verified bool `json:"verified"`
|
Verified bool `json:"verified"`
|
||||||
MFAEnabled bool `json:"mfa_enabled"`
|
MFAEnabled bool `json:"mfa_enabled"`
|
||||||
Bot bool `json:"bot"`
|
Bot bool `json:"bot"`
|
||||||
|
Permissions *int `gorm:"default:1;not null" json:"permissions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserSave -
|
// UserSave -
|
||||||
func UserSave(conn *gorm.DB, u *User) error {
|
func UserSave(conn *gorm.DB, u *User) error {
|
||||||
return conn.Save(u).Error
|
var userCopy User
|
||||||
|
copier.Copy(&userCopy, u)
|
||||||
|
// insert or update user
|
||||||
|
// need to make copy of assign object because it must mess
|
||||||
|
// with the actual object in FirstOrCreate method
|
||||||
|
return conn.Where(&User{ID: u.ID}).Assign(userCopy).FirstOrCreate(u).Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const cashGuildID = "101198129352691712"
|
|
||||||
|
|
||||||
type oauthReq struct {
|
type oauthReq struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
}
|
}
|
||||||
@@ -50,6 +48,15 @@ func oauthHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save/update user in database
|
||||||
|
err = model.UserSave(db.GetConn(), &user)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
c.JSON(500, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// generate json web token
|
// generate json web token
|
||||||
token, err := middleware.GetJWT(user)
|
token, err := middleware.GetJWT(user)
|
||||||
|
|
||||||
@@ -59,12 +66,5 @@ func oauthHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// save/update user in database
|
|
||||||
err = model.UserSave(db.GetConn(), &user)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(200, token)
|
c.JSON(200, token)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user