2024-09-18 07:59:20 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin implements the interface expected by the Mattermost server to communicate between the server and plugin processes.
|
|
|
|
type Plugin struct {
|
|
|
|
plugin.MattermostPlugin
|
|
|
|
|
|
|
|
// configurationLock synchronizes access to the configuration.
|
|
|
|
configurationLock sync.RWMutex
|
|
|
|
|
|
|
|
// configuration is the active plugin configuration. Consult getConfiguration and
|
|
|
|
// setConfiguration for usage.
|
|
|
|
configuration *configuration
|
|
|
|
}
|
|
|
|
|
2024-09-18 08:43:24 -07:00
|
|
|
// Known game processes
|
|
|
|
var knownGames = map[string]bool{
|
2024-09-18 08:53:20 -07:00
|
|
|
"fortnite.exe": true,
|
|
|
|
"gameprocess": true,
|
2024-09-18 08:43:24 -07:00
|
|
|
// Add more known game processes here
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetActiveGame scans processes to determine the active game based on known game names
|
2024-09-18 07:59:20 -07:00
|
|
|
func (p *Plugin) GetActiveGame() (string, error) {
|
2024-09-18 08:43:24 -07:00
|
|
|
// Use appropriate command for your OS
|
2024-09-18 07:59:20 -07:00
|
|
|
cmd := exec.Command("ps", "-e")
|
|
|
|
output, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(output), "\n")
|
|
|
|
for _, line := range lines {
|
2024-09-18 08:43:24 -07:00
|
|
|
for gameName := range knownGames {
|
|
|
|
if strings.Contains(line, gameName) {
|
|
|
|
return gameName, nil
|
|
|
|
}
|
2024-09-18 07:59:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "No game detected", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) ExecuteCommand(c *plugin.Context, command *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
|
|
|
|
split := strings.Fields(command.Command)
|
|
|
|
|
|
|
|
if len(split) == 0 {
|
|
|
|
return &model.CommandResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch split[0] {
|
|
|
|
case "/setgame":
|
|
|
|
var game string
|
|
|
|
var err error
|
|
|
|
if len(split) < 2 {
|
|
|
|
// Scan for active game if no specific game is mentioned
|
|
|
|
game, err = p.GetActiveGame()
|
|
|
|
if err != nil {
|
|
|
|
return nil, &model.AppError{Message: "Failed to scan processes", StatusCode: 500}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
game = strings.Join(split[1:], " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
status := fmt.Sprintf("Playing: %s", game)
|
|
|
|
user, appErr := p.API.GetUser(command.UserId)
|
|
|
|
if appErr != nil {
|
|
|
|
return nil, appErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the custom status to JSON
|
|
|
|
customStatus := model.CustomStatus{
|
|
|
|
Emoji: "video_game",
|
|
|
|
Text: status,
|
|
|
|
Duration: "0", // Optional: Time duration the status will stay
|
|
|
|
}
|
|
|
|
customStatusJSON, err := json.Marshal(customStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &model.AppError{Message: "Failed to serialize custom status", StatusCode: 500}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the custom status in user.Props
|
|
|
|
user.Props = map[string]string{
|
|
|
|
"custom_status": string(customStatusJSON),
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, appErr := p.API.UpdateUser(user); appErr != nil {
|
|
|
|
return nil, appErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.CommandResponse{
|
|
|
|
Text: fmt.Sprintf("Your status has been updated to: %s", status),
|
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return &model.CommandResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnActivate() error {
|
|
|
|
command := &model.Command{
|
|
|
|
Trigger: "setgame",
|
|
|
|
AutoComplete: true,
|
|
|
|
AutoCompleteDesc: "Set the game you are currently playing",
|
|
|
|
AutoCompleteHint: "[game]",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.API.RegisterCommand(command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|