save servers to pocketbase by user id
This commit is contained in:
parent
34f3bcac3d
commit
d971843fee
13
bot/bot.go
13
bot/bot.go
@ -44,6 +44,7 @@ func Run() {
|
|||||||
var conversationHistoryMap = make(map[string][]openai.ChatCompletionMessage)
|
var conversationHistoryMap = make(map[string][]openai.ChatCompletionMessage)
|
||||||
var sshConnections = make(map[string]*SSHConnection)
|
var sshConnections = make(map[string]*SSHConnection)
|
||||||
|
|
||||||
|
|
||||||
func hasAdminRole(roles []string) bool {
|
func hasAdminRole(roles []string) bool {
|
||||||
for _, role := range roles {
|
for _, role := range roles {
|
||||||
if role == AllowedUserID {
|
if role == AllowedUserID {
|
||||||
@ -129,6 +130,18 @@ func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
|
|||||||
// Store the SSH connection for later use
|
// Store the SSH connection for later use
|
||||||
sshConnections[message.Author.ID] = sshConn
|
sshConnections[message.Author.ID] = sshConn
|
||||||
|
|
||||||
|
// Save server information to PocketBase
|
||||||
|
serverInfo := &pb.ServerInfo{
|
||||||
|
UserID: message.Author.ID,
|
||||||
|
ConnectionDetails: connectionDetails,
|
||||||
|
}
|
||||||
|
err = pb.CreateRecord("servers", serverInfo)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
discord.ChannelMessageSend(message.ChannelID, "Error saving server information.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
discord.ChannelMessageSend(message.ChannelID, "Connected to remote server!")
|
discord.ChannelMessageSend(message.ChannelID, "Connected to remote server!")
|
||||||
} else {
|
} else {
|
||||||
discord.ChannelMessageSend(message.ChannelID, "You are not authorized to use this command.")
|
discord.ChannelMessageSend(message.ChannelID, "You are not authorized to use this command.")
|
||||||
|
|||||||
34
pb/pb.go
34
pb/pb.go
@ -14,6 +14,11 @@ var (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ServerInfo struct {
|
||||||
|
UserID string
|
||||||
|
ConnectionDetails string
|
||||||
|
}
|
||||||
|
|
||||||
// Init initializes the PocketBase app
|
// Init initializes the PocketBase app
|
||||||
func Init() {
|
func Init() {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
@ -40,3 +45,32 @@ func GetRecordById(collectionID string, recordID string) (*models.Record, error)
|
|||||||
|
|
||||||
return record, nil
|
return record, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateRecord(collectionName string, record *ServerInfo) error {
|
||||||
|
// Find the collection
|
||||||
|
collection, err := app.Dao().FindCollectionByNameOrId(collectionName)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new record
|
||||||
|
newRecord := models.NewRecord(collection)
|
||||||
|
|
||||||
|
// Convert ServerInfo to a map
|
||||||
|
recordMap := map[string]interface{}{
|
||||||
|
"UserID": record.UserID,
|
||||||
|
"ConnectionDetails": record.ConnectionDetails,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bulk load with record.Load(map[string]interface{})
|
||||||
|
newRecord.Load(recordMap)
|
||||||
|
|
||||||
|
// Save the record
|
||||||
|
if err := app.Dao().SaveRecord(newRecord); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
BIN
pb_data/data.db
BIN
pb_data/data.db
Binary file not shown.
BIN
pb_data/logs.db
BIN
pb_data/logs.db
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user