123 lines
3 KiB
Go
123 lines
3 KiB
Go
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
|
|
}
|
|
|
|
// Known game processes
|
|
var knownGames = map[string]bool{
|
|
// Add known game processes here
|
|
"FortniteClient-Win64-Shipping.exe": true,
|
|
"gameprocess": true,
|
|
}
|
|
|
|
// GetActiveGame scans processes to determine the active game based on known game names
|
|
func (p *Plugin) GetActiveGame() (string, error) {
|
|
// Use appropriate command for your OS
|
|
cmd := exec.Command("ps", "-e")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
lines := strings.Split(string(output), "\n")
|
|
for _, line := range lines {
|
|
for gameName := range knownGames {
|
|
if strings.Contains(line, gameName) {
|
|
return gameName, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|