2020-12-05 02:35:40 +00:00
|
|
|
package discord
|
2020-12-05 02:24:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"git.ddd.rip/ptrcnull/modweb"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Module struct {
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
RedirectURI string
|
|
|
|
Scope string
|
|
|
|
Callback func(me *MeResponse) *modweb.User
|
|
|
|
}
|
|
|
|
|
|
|
|
const API_ENDPOINT = "https://discord.com/api/v6"
|
|
|
|
|
|
|
|
func (m *Module) LoginURL() string {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("client_id", m.ClientID)
|
|
|
|
params.Set("redirect_uri", m.RedirectURI)
|
|
|
|
params.Set("response_type", "code")
|
|
|
|
params.Set("scope", m.Scope)
|
|
|
|
return "https://discord.com/api/oauth2/authorize?" + params.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) RegisterURL() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) FriendlyName() string {
|
|
|
|
return "Discord"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) Name() string {
|
|
|
|
return "discord"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) MinAccessLevel() int64 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) Hidden() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID string
|
|
|
|
AccessLevel int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) Init(mm *modweb.ModuleManager) {
|
|
|
|
app := mm.Fiber()
|
|
|
|
app.Get("/callback", func(ctx *fiber.Ctx) error {
|
|
|
|
session := mm.Session(ctx)
|
|
|
|
defer session.Save()
|
|
|
|
|
|
|
|
if session.Get("user") != nil {
|
|
|
|
return ctx.Redirect("/")
|
|
|
|
}
|
|
|
|
|
|
|
|
code := ctx.Query("code")
|
|
|
|
if code == "" {
|
|
|
|
return ctx.Redirect("/")
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := m.getToken(code)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return ctx.Redirect("/")
|
|
|
|
}
|
|
|
|
|
|
|
|
me, err := getMe(token)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return ctx.Redirect("/")
|
|
|
|
}
|
|
|
|
|
|
|
|
user := m.Callback(me)
|
|
|
|
user.Save(session)
|
|
|
|
|
|
|
|
return ctx.Redirect("/")
|
|
|
|
})
|
|
|
|
}
|