Compare commits

...
6 Commits
Author SHA1 Message Date
Dustin Pianalto 5a384305b0 Add initial listplayers
CI / build (push) Has been cancelled
2021-02-24 14:06:18 -09:00
Dustin Pianalto fb5f508fdf Add initial listplayers
CI / build (push) Has been cancelled
2021-02-24 14:01:20 -09:00
Dustin Pianalto 1fbe5ca7bf Add initial listplayers
CI / build (push) Has been cancelled
2021-02-24 13:53:43 -09:00
Dustin Pianalto 2e0762ee0f Fix permission bug in close
CI / build (push) Has been cancelled
2021-02-24 11:37:52 -09:00
Dustin Pianalto ccb450e543 Add view command and update utils
CI / build (push) Has been cancelled
2021-02-24 00:03:12 -09:00
Dustin Pianalto c34f475084 Add view command and update utils
CI / build (push) Has been cancelled
2021-02-23 23:53:28 -09:00
5 changed files with 136 additions and 17 deletions
+103
View File
@@ -0,0 +1,103 @@
package arcon
import (
"fmt"
"strings"
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/pkg/services"
"github.com/gorcon/rcon"
)
var ListplayersCommand = &disgoman.Command{
Name: "listplayers",
Aliases: nil,
Description: "List the players currently connected to a ARK server.",
OwnerOnly: false,
Hidden: false,
RequiredPermissions: 0,
Invoke: listplayersCommandFunc,
}
func listplayersCommandFunc(ctx disgoman.Context, args []string) {
guild, err := services.GuildService.GetOrCreateGuild(ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error getting Guild from the database", err)
return
}
author, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Sorry, there was a problem getting your user.", err)
return
}
if !discord_utils.IsGuildAdmin(ctx, author) && !discord_utils.IsGuildMod(ctx, author) {
return
}
if len(args) == 0 {
servers, err := services.ServerService.GuildServers(guild)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Could not find any servers for this guild", err)
return
}
for _, server := range servers {
go listplayers(ctx, server)
}
return
}
serverName := strings.Join(args, " ")
server, err := services.ServerService.ServerByName(serverName, guild)
if err != nil {
discord_utils.SendErrorMessage(ctx,
fmt.Sprintf("Could not find **%s** in this guild.", serverName),
err,
)
return
}
listplayers(ctx, server)
}
func listplayers(ctx disgoman.Context, server geeksbot.Server) {
msg, err := ctx.Send(fmt.Sprintf("**Getting data for %s**", server.Name))
if err != nil {
discord_utils.SendErrorMessage(ctx, "There was an error getting the player list", err)
return
}
conn, err := rcon.Dial(fmt.Sprintf("%s:%d", server.IPAddr, server.Port), server.Password)
if err != nil {
_, _ = ctx.Session.ChannelMessageEdit(ctx.Channel.ID, msg.ID,
fmt.Sprintf("**Could not open connection to %s**", server.Name),
)
return
}
defer conn.Close()
response, err := conn.Execute("listplayers")
if err != nil {
_, _ = ctx.Session.ChannelMessageEdit(ctx.Channel.ID, msg.ID,
fmt.Sprintf("**There was a problem getting a response from %s**", server.Name),
)
return
}
if strings.HasPrefix(response, "No Players") {
_, _ = ctx.Session.ChannelMessageEdit(ctx.Channel.ID, msg.ID,
fmt.Sprintf("**%s: %s**", server.Name, response),
)
return
}
players := strings.Split(response, "\n")
for i, player := range players {
parts := strings.Split(player, ", ")
steamID := parts[len(parts)-1]
user, err := services.UserService.GetBySteamID(steamID)
if err == nil {
duser, err := ctx.Session.GuildMember(ctx.Guild.ID, user.ID)
if err == nil {
players[i] = fmt.Sprintf("%s (%s)", player, duser.Mention())
}
}
}
_, _ = ctx.Session.ChannelMessageEdit(ctx.Channel.ID, msg.ID,
fmt.Sprintf("**%s:**\n%s", server.Name, strings.Join(players, "\n")),
)
}
+2
View File
@@ -2,6 +2,7 @@ package exts
import ( import (
"github.com/dustinpianalto/disgoman" "github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot/internal/exts/arcon"
"github.com/dustinpianalto/geeksbot/internal/exts/guild" "github.com/dustinpianalto/geeksbot/internal/exts/guild"
"github.com/dustinpianalto/geeksbot/internal/exts/requests" "github.com/dustinpianalto/geeksbot/internal/exts/requests"
"github.com/dustinpianalto/geeksbot/internal/exts/utils" "github.com/dustinpianalto/geeksbot/internal/exts/utils"
@@ -36,4 +37,5 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
_ = g.AddCommand(requests.ListCommand) _ = g.AddCommand(requests.ListCommand)
_ = g.AddCommand(requests.ViewCommand) _ = g.AddCommand(requests.ViewCommand)
_ = g.AddCommand(requests.CommentCommand) _ = g.AddCommand(requests.CommentCommand)
_ = g.AddCommand(arcon.ListplayersCommand)
} }
+19 -17
View File
@@ -79,7 +79,7 @@ func requestCommandFunc(ctx disgoman.Context, args []string) {
if c.Admin { if c.Admin {
_, _ = ctx.Session.ChannelMessageSend(c.ID, _, _ = ctx.Session.ChannelMessageSend(c.ID,
fmt.Sprintf("%s\n"+ fmt.Sprintf("%s\n"+
"New Request ID %d "+ "New Request ID %d\n"+
"%s has requested assistance: \n"+ "%s has requested assistance: \n"+
"```\n%s\n```\n"+ "```\n%s\n```\n"+
"Requested At: %s\n"+ "Requested At: %s\n"+
@@ -154,7 +154,7 @@ func closeCommandFunc(ctx disgoman.Context, args []string) {
continue continue
} }
if request.Author != closer && !closer.IsStaff && !closer.IsAdmin { if request.Author != closer && !closer.IsStaff && !closer.IsAdmin {
if !discord_utils.IsGuildMod(ctx, closer) || !discord_utils.IsGuildAdmin(ctx, closer) { if !discord_utils.IsGuildMod(ctx, closer) && !discord_utils.IsGuildAdmin(ctx, closer) {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("You are not authorized to close %d", id), nil) discord_utils.SendErrorMessage(ctx, fmt.Sprintf("You are not authorized to close %d", id), nil)
continue continue
} }
@@ -248,7 +248,7 @@ func listCommandFunc(ctx disgoman.Context, args []string) {
commentCount = 0 commentCount = 0
} }
_, _ = ctx.Send(fmt.Sprintf("```md\n"+ _, _ = ctx.Send(fmt.Sprintf("```md\n"+
"< Request ID Requested By >\n"+ "< Request ID Requested By >\n"+
"< %-11d %23s >\n"+ "< %-11d %23s >\n"+
"%s\n\n"+ "%s\n\n"+
"Comments: %d\n"+ "Comments: %d\n"+
@@ -288,7 +288,7 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
discord_utils.SendErrorMessage(ctx, "Please include the ID of the request to update.", err) discord_utils.SendErrorMessage(ctx, "Please include the ID of the request to update.", err)
return return
} }
message := strings.Join(args[1:len(args)-1], " ") message := strings.Join(args[1:len(args)], " ")
author, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID) author, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID)
if err != nil { if err != nil {
discord_utils.SendErrorMessage(ctx, "Sorry, there was an issue finding your user account", err) discord_utils.SendErrorMessage(ctx, "Sorry, there was an issue finding your user account", err)
@@ -318,7 +318,7 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
var commentString string var commentString string
var commentStrings []string var commentStrings []string
commentString = fmt.Sprintf("Comment added:\n```md\n"+ commentString = fmt.Sprintf("Comment added:\n```md\n"+
"< Request ID Requested By >\n"+ "< Request ID Requested By >\n"+
"< %-11d %23s >\n"+ "< %-11d %23s >\n"+
"%s\n\n"+ "%s\n\n"+
"Comments: Not Implemented Yet\n"+ "Comments: Not Implemented Yet\n"+
@@ -328,7 +328,7 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
request.ID, request.ID,
discord_utils.GetDisplayName(ctx, request.Author.ID), discord_utils.GetDisplayName(ctx, request.Author.ID),
request.Content, request.Content,
request.RequestedAt.Format("2006-01-02 15:04:05 MST"), request.RequestedAt.Format("2006-01-02 15:04:05"),
discord_utils.GetChannelName(ctx, request.Channel.ID), discord_utils.GetChannelName(ctx, request.Channel.ID),
) )
for _, c := range comments { for _, c := range comments {
@@ -339,12 +339,13 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
cs := fmt.Sprintf("```md\n%s\n- %s At %s\n```\n", cs := fmt.Sprintf("```md\n%s\n- %s At %s\n```\n",
c.Content, c.Content,
discord_utils.GetDisplayName(ctx, c.Author.ID), discord_utils.GetDisplayName(ctx, c.Author.ID),
c.CommentAt.Format("2006-01-02 15:04:05 MST"), c.CommentAt.Format("2006-01-02 15:04:05"),
) )
if len(commentString+cs) >= 2000 { if len(commentString+cs) >= 2000 {
commentStrings = append(commentStrings, commentString) commentStrings = append(commentStrings, commentString)
commentString = "" commentString = ""
} }
commentString += cs
} }
commentStrings = append(commentStrings, commentString) commentStrings = append(commentStrings, commentString)
for _, c := range channels { for _, c := range channels {
@@ -354,6 +355,8 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
} }
} }
} }
} else {
log.Println(err)
} }
_, err = ctx.Send(fmt.Sprintf("%s your comment has been added.", ctx.Message.Author.Mention())) _, err = ctx.Send(fmt.Sprintf("%s your comment has been added.", ctx.Message.Author.Mention()))
dmChannel, err := ctx.Session.UserChannelCreate(request.Author.ID) dmChannel, err := ctx.Session.UserChannelCreate(request.Author.ID)
@@ -408,37 +411,36 @@ func viewCommandFunc(ctx disgoman.Context, args []string) {
discord_utils.SendErrorMessage(ctx, "You are not authorized to view that request", nil) discord_utils.SendErrorMessage(ctx, "You are not authorized to view that request", nil)
return return
} }
comments, _ := services.RequestService.RequestComments(request) comments, err := services.RequestService.RequestComments(request)
if err != nil {
discord_utils.SendErrorMessage(ctx, "There was an error getting the comments.", err)
}
var commentString string var commentString string
var commentStrings []string var commentStrings []string
commentString = fmt.Sprintf("Comment added:\n```md\n"+ commentString = fmt.Sprintf("```md\n"+
"< Request ID Requested By >\n"+ "< Request ID Requested By >\n"+
"< %-11d %23s >\n"+ "< %-11d %23s >\n"+
"%s\n\n"+ "%s\n\n"+
"Comments: Not Implemented Yet\n"+
"Requested At: %s\n"+ "Requested At: %s\n"+
"In: %s\n"+ "In: %s\n"+
"```", "```",
request.ID, request.ID,
discord_utils.GetDisplayName(ctx, request.Author.ID), discord_utils.GetDisplayName(ctx, request.Author.ID),
request.Content, request.Content,
request.RequestedAt.Format("2006-01-02 15:04:05 MST"), request.RequestedAt.Format("2006-01-02 15:04:05"),
discord_utils.GetChannelName(ctx, request.Channel.ID), discord_utils.GetChannelName(ctx, request.Channel.ID),
) )
for _, c := range comments { for _, c := range comments {
if err != nil {
log.Println(err)
continue
}
cs := fmt.Sprintf("```md\n%s\n- %s At %s\n```\n", cs := fmt.Sprintf("```md\n%s\n- %s At %s\n```\n",
c.Content, c.Content,
discord_utils.GetDisplayName(ctx, c.Author.ID), discord_utils.GetDisplayName(ctx, c.Author.ID),
c.CommentAt.Format("2006-01-02 15:04:05 MST"), c.CommentAt.Format("2006-01-02 15:04:05"),
) )
if len(commentString+cs) >= 2000 { if len(commentString+cs) >= 2000 {
commentStrings = append(commentStrings, commentString) commentStrings = append(commentStrings, commentString)
commentString = "" commentString = ""
} }
commentString += cs
} }
commentStrings = append(commentStrings, commentString) commentStrings = append(commentStrings, commentString)
for _, c := range commentStrings { for _, c := range commentStrings {
+11
View File
@@ -49,3 +49,14 @@ func (s userService) GetOrCreateUser(id string) (geeksbot.User, error) {
} }
return user, err return user, err
} }
func (s userService) GetBySteamID(steamID string) (geeksbot.User, error) {
var id string
queryString := "SELECT id FROM users WHERE steam_id = $1"
err := s.db.QueryRow(queryString, steamID).Scan(&id)
if err != nil {
return geeksbot.User{}, err
}
user, err := s.User(id)
return user, err
}
+1
View File
@@ -16,4 +16,5 @@ type UserService interface {
DeleteUser(u User) error DeleteUser(u User) error
UpdateUser(u User) (User, error) UpdateUser(u User) (User, error)
GetOrCreateUser(id string) (User, error) GetOrCreateUser(id string) (User, error)
GetBySteamID(steamID string) (User, error)
} }