-
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.
Implemented server list and linking to dashboard page. Server list sy…
…ncs to database
- Loading branch information
Scott Bragg
committed
Dec 28, 2024
1 parent
461cbec
commit a5b9095
Showing
12 changed files
with
438 additions
and
86 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
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,54 @@ | ||
// Package db provides a database connection and helper functions to interact with the database. | ||
// server.go has functions to interact with the server table in the database. | ||
package db | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/faulteh/nap-and-go/discordtypes" | ||
"github.com/faulteh/nap-and-go/models" | ||
) | ||
|
||
// SyncGuilds synchronizes the servers in the database with the servers in the Discord API | ||
func SyncGuilds(guilds []discordtypes.Guild) error { | ||
// Sync the guilds with the database | ||
conn := GetDB() | ||
for _, guild := range guilds { | ||
var server models.Server | ||
if err := conn.Where("id = ?", guild.ID).First(&server).Error; err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
// Create a new server | ||
server = models.Server{ | ||
ID: guild.ID, | ||
Name: guild.Name, | ||
Icon: guild.Icon, | ||
Owner: guild.Owner, | ||
Permissions: guild.Permissions, | ||
PermissionsNew: guild.PermissionsNew, | ||
} | ||
if err := conn.Create(&server).Error; err != nil { | ||
log.Printf("Failed to create server: %v\n", err) | ||
return err | ||
} | ||
} else { | ||
log.Printf("Failed to query server: %v\n", err) | ||
return err | ||
} | ||
} else { | ||
// Update the server | ||
server.Name = guild.Name | ||
server.Icon = guild.Icon | ||
server.Owner = guild.Owner | ||
server.Permissions = guild.Permissions | ||
server.PermissionsNew = guild.PermissionsNew | ||
if err := conn.Save(&server).Error; err != nil { | ||
log.Printf("Failed to update server: %v\n", err) | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,87 @@ | ||
// Package discordapi interacts with the Discord API to fetch server information and provide data types | ||
// to represent the data. | ||
package discordapi | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"golang.org/x/oauth2" | ||
|
||
"github.com/faulteh/nap-and-go/config" | ||
"github.com/faulteh/nap-and-go/db" | ||
"github.com/faulteh/nap-and-go/discordtypes" | ||
) | ||
|
||
// UserAdminGuildList returns a list of servers the user has admin permissions in queried from the Discord API | ||
func UserAdminGuildList(token *oauth2.Token) ([]discordtypes.Guild, error) { | ||
// Retrieve list of servers for user from discord | ||
oauth2Config := config.LoadDiscordConfig().OAuth2Config() | ||
client := oauth2Config.Client(context.Background(), token) | ||
resp, err := client.Get("https://discord.com/api/users/@me/guilds") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() //nolint:errcheck | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, err | ||
} | ||
|
||
var guilds []discordtypes.Guild | ||
if err := json.NewDecoder(resp.Body).Decode(&guilds); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Filter guilds where the user has admin permissions | ||
var adminGuilds []discordtypes.Guild | ||
const ADMINISTRATOR = 0x00000008 | ||
for _, guild := range guilds { | ||
if guild.Permissions&ADMINISTRATOR != 0 { | ||
adminGuilds = append(adminGuilds, guild) | ||
} | ||
} | ||
|
||
return adminGuilds, nil | ||
} | ||
|
||
// BotGuildList returns a list of servers the bot is in queried from the Discord API | ||
// and syncs the database with the list of servers | ||
func BotGuildList() ([]discordtypes.Guild, error) { | ||
// Bot uses a simple Authorization: Bot <token> header | ||
token := config.LoadDiscordConfig().BotToken | ||
if token == "" { | ||
return nil, fmt.Errorf("missing bot token") | ||
} | ||
// Use the bot authorization token to request the guild list | ||
client := &http.Client{} | ||
req, err := http.NewRequest("GET", "https://discord.com/api/users/@me/guilds", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Authorization", "Bot "+token) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() //nolint:errcheck | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, err | ||
} | ||
|
||
var guilds []discordtypes.Guild | ||
if err := json.NewDecoder(resp.Body).Decode(&guilds); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Sync the guilds with the database | ||
err = db.SyncGuilds(guilds) | ||
if err != nil { | ||
return guilds, err | ||
} | ||
|
||
return guilds, nil | ||
} |
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,14 @@ | ||
// Package discordtypes provides the types used in the Discord API. | ||
// Used by db and discordapi packages but kept separate to avoid circular dependencies. | ||
package discordtypes | ||
|
||
// Guild represents a Discord guild/server | ||
type Guild struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Icon string `json:"icon"` | ||
Owner bool `json:"owner"` | ||
Permissions int64 `json:"permissions"` | ||
PermissionsNew string `json:"permissions_new"` | ||
HasBot bool | ||
} |
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,3 +1,4 @@ | ||
{{ template "header.html" . }} | ||
<h1>Dashboard</h1> | ||
{{ template "header.html" }} | ||
<h1 class="title">Dashboard</h1> | ||
<p>Welcome to your dashboard! This is where your main content will go.</p> | ||
{{ template "footer.html" }} |
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,8 +1,22 @@ | ||
{{ define "footer.html"}} | ||
</div> | ||
<footer> | ||
<p>© 2024 Nap-and-Go</p> | ||
</footer> | ||
</main> | ||
</div> | ||
<script> | ||
// Toggle navbar burger menu | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); | ||
if ($navbarBurgers.length > 0) { | ||
$navbarBurgers.forEach(el => { | ||
el.addEventListener('click', () => { | ||
const target = el.dataset.target; | ||
const $target = document.getElementById(target); | ||
el.classList.toggle('is-active'); | ||
$target.classList.toggle('is-active'); | ||
}); | ||
}); | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> | ||
{{ end }} |
Oops, something went wrong.