Skip to content

Commit

Permalink
feat: long-lived access tokens
Browse files Browse the repository at this point in the history
You can now scare the shit out of your user by auto playing something at a certain time.
  • Loading branch information
TheTipo01 committed Oct 23, 2023
1 parent 2e6db02 commit 1bd8e97
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
18 changes: 17 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func NewApi(servers map[string]*manager.Server, address, owner string, clients *
func (a *Api) AddUser(user *discordgo.User, userInfo UserInfo) string {
if u, ok := a.userInfo[user.ID]; ok {
delete(a.tokensToUsers, u.token)
delete(a.userInfo, user.ID)

if a.userInfo[user.ID].LongLivedToken == "" {
delete(a.userInfo, user.ID)
}
}

// Generate a new token until it is unique
Expand All @@ -45,9 +48,22 @@ func (a *Api) AddUser(user *discordgo.User, userInfo UserInfo) string {
}

a.tokensToUsers[token] = user

if a.userInfo[user.ID] != nil {
userInfo.LongLivedToken = a.userInfo[user.ID].LongLivedToken
}
userInfo.token = token

a.userInfo[user.ID] = &userInfo

return token
}

func (a *Api) AddLongLivedToken(user *discordgo.User, userInfo UserInfo) {
a.tokensToUsers[userInfo.LongLivedToken] = user

if a.userInfo[user.ID] != nil {
userInfo.token = a.userInfo[user.ID].token
}
a.userInfo[user.ID] = &userInfo
}
7 changes: 4 additions & 3 deletions api/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type Api struct {
}

type UserInfo struct {
token string
Guild string
TextChannel string
token string
LongLivedToken string
Guild string
TextChannel string
}
13 changes: 12 additions & 1 deletion example_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ drivername: sqlite
loglevel: informational

# Web server address, to access the web interface
address: :8080
address: :8080

# Long-lived API tokens, to access the API without having to log in every time.
#apitokens:
# - userid: 0000000000000000
# token: correct-horse-battery-staple
# guildid: 0000000000000000
# textchannelid: 0000000000000000
# - userid: 1111111111111111
# token: correct-horse-battery-staple
# guildid: 0000000000000000
# textchannelid: 0000000000000000
17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ var (
// Clients
clients manager.Clients
// API
webApi api.Api
webApi api.Api
longLivedTokens []apiToken
)

func init() {
Expand All @@ -54,6 +55,7 @@ func init() {
token = cfg.Token
owner = cfg.Owner
address = cfg.Address
longLivedTokens = cfg.ApiTokens

// Set lit.LogLevel to the given value
switch strings.ToLower(cfg.LogLevel) {
Expand Down Expand Up @@ -205,6 +207,19 @@ func main() {
lit.Error("Can't register commands, %s", err)
}

if address != "" && len(longLivedTokens) > 0 {
lit.Info("Loading long lived tokens")
for _, t := range longLivedTokens {
userInfo := api.UserInfo{
LongLivedToken: t.Token,
Guild: t.Guild,
TextChannel: t.TextChannel,
}
user, _ := dg.User(t.UserID)
webApi.AddLongLivedToken(user, userInfo)
}
}

// Wait here until CTRL-C or another term signal is received.
lit.Info("YADMB is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
Expand Down
26 changes: 17 additions & 9 deletions structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package main

// Config holds data parsed from the config.yml
type Config struct {
Token string `fig:"token" validate:"required"`
Owner string `fig:"owner" validate:"required"`
ClientID string `fig:"clientid"`
ClientSecret string `fig:"clientsecret"`
DSN string `fig:"datasourcename" validate:"required"`
Driver string `fig:"drivername" validate:"required"`
LogLevel string `fig:"loglevel" validate:"required"`
YouTubeAPI string `fig:"youtubeapikey"`
Address string `fig:"address"`
Token string `fig:"token" validate:"required"`
Owner string `fig:"owner" validate:"required"`
ClientID string `fig:"clientid"`
ClientSecret string `fig:"clientsecret"`
DSN string `fig:"datasourcename" validate:"required"`
Driver string `fig:"drivername" validate:"required"`
LogLevel string `fig:"loglevel" validate:"required"`
YouTubeAPI string `fig:"youtubeapikey"`
Address string `fig:"address"`
ApiTokens []apiToken `fig:"apitokens"`
}

type apiToken struct {
UserID string `fig:"userid" validate:"required"`
Token string `fig:"token" validate:"required"`
TextChannel string `fig:"textchannelid" validate:"required"`
Guild string `fig:"guildid" validate:"required"`
}

0 comments on commit 1bd8e97

Please sign in to comment.