Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1fbe5ca7bf | ||
|
|
2e0762ee0f | ||
|
|
ccb450e543 | ||
|
|
c34f475084 | ||
|
|
1a7d9b824f | ||
|
|
0bf31a3e5d |
@@ -2,10 +2,10 @@ package discord_utils
|
||||
|
||||
import "github.com/dustinpianalto/disgoman"
|
||||
|
||||
func GetChannelName(ctx disgoman.Context, id string) (string, error) {
|
||||
func GetChannelName(ctx disgoman.Context, id string) string {
|
||||
channel, err := ctx.Session.Channel(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return ""
|
||||
}
|
||||
return channel.Name, nil
|
||||
return channel.Name
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ package discord_utils
|
||||
|
||||
import "github.com/dustinpianalto/disgoman"
|
||||
|
||||
func GetDisplayName(ctx disgoman.Context, id string) (string, error) {
|
||||
func GetDisplayName(ctx disgoman.Context, id string) string {
|
||||
member, err := ctx.Session.GuildMember(ctx.Guild.ID, id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return ""
|
||||
}
|
||||
if member.Nick != "" {
|
||||
return member.Nick, nil
|
||||
return member.Nick
|
||||
}
|
||||
return member.User.Username, nil
|
||||
return member.User.Username
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
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: "request",
|
||||
Aliases: nil,
|
||||
Description: "Submit a request for the guild staff",
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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")),
|
||||
)
|
||||
}
|
||||
@@ -34,4 +34,6 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
|
||||
_ = g.AddCommand(requests.RequestCommand)
|
||||
_ = g.AddCommand(requests.CloseCommand)
|
||||
_ = g.AddCommand(requests.ListCommand)
|
||||
_ = g.AddCommand(requests.ViewCommand)
|
||||
_ = g.AddCommand(requests.CommentCommand)
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func requestCommandFunc(ctx disgoman.Context, args []string) {
|
||||
if c.Admin {
|
||||
_, _ = ctx.Session.ChannelMessageSend(c.ID,
|
||||
fmt.Sprintf("%s\n"+
|
||||
"New Request ID %d "+
|
||||
"New Request ID %d\n"+
|
||||
"%s has requested assistance: \n"+
|
||||
"```\n%s\n```\n"+
|
||||
"Requested At: %s\n"+
|
||||
@@ -154,7 +154,7 @@ func closeCommandFunc(ctx disgoman.Context, args []string) {
|
||||
continue
|
||||
}
|
||||
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)
|
||||
continue
|
||||
}
|
||||
@@ -180,7 +180,17 @@ func closeCommandFunc(ctx disgoman.Context, args []string) {
|
||||
if err != nil {
|
||||
discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was closed.", err)
|
||||
}
|
||||
|
||||
dmChannel, err := ctx.Session.UserChannelCreate(request.Author.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, _ = ctx.Session.ChannelMessageSend(dmChannel.ID,
|
||||
fmt.Sprintf("%s has closed request %d which you opened in the %s channel.\n```%s```\n",
|
||||
discord_utils.GetDisplayName(ctx, request.CompletedBy.ID),
|
||||
request.ID,
|
||||
discord_utils.GetChannelName(ctx, request.Channel.ID),
|
||||
request.Content,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,18 +243,22 @@ func listCommandFunc(ctx disgoman.Context, args []string) {
|
||||
} else {
|
||||
channelName = channel.Name
|
||||
}
|
||||
|
||||
commentCount, err := services.RequestService.RequestCommentCount(request)
|
||||
if err != nil {
|
||||
commentCount = 0
|
||||
}
|
||||
_, _ = ctx.Send(fmt.Sprintf("```md\n"+
|
||||
"< Request ID Requested By >\n"+
|
||||
"< Request ID Requested By >\n"+
|
||||
"< %-11d %23s >\n"+
|
||||
"%s\n\n"+
|
||||
"Comments: Not Implemented Yet\n"+
|
||||
"Comments: %d\n"+
|
||||
"Requested At: %s\n"+
|
||||
"In: %s\n"+
|
||||
"```",
|
||||
request.ID,
|
||||
authorName,
|
||||
request.Content,
|
||||
commentCount,
|
||||
request.RequestedAt.Format("2006-01-02 15:04:05 MST"),
|
||||
channelName,
|
||||
))
|
||||
@@ -274,7 +288,7 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
|
||||
discord_utils.SendErrorMessage(ctx, "Please include the ID of the request to update.", err)
|
||||
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)
|
||||
if err != nil {
|
||||
discord_utils.SendErrorMessage(ctx, "Sorry, there was an issue finding your user account", err)
|
||||
@@ -303,10 +317,8 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
|
||||
comments, _ := services.RequestService.RequestComments(request)
|
||||
var commentString string
|
||||
var commentStrings []string
|
||||
displayName, _ := discord_utils.GetDisplayName(ctx, request.Author.ID)
|
||||
channelName, _ := discord_utils.GetChannelName(ctx, request.Channel.ID)
|
||||
commentString = fmt.Sprintf("Comment added:\n```md\n"+
|
||||
"< Request ID Requested By >\n"+
|
||||
"< Request ID Requested By >\n"+
|
||||
"< %-11d %23s >\n"+
|
||||
"%s\n\n"+
|
||||
"Comments: Not Implemented Yet\n"+
|
||||
@@ -314,27 +326,28 @@ func commentCommandFunc(ctx disgoman.Context, args []string) {
|
||||
"In: %s\n"+
|
||||
"```",
|
||||
request.ID,
|
||||
displayName,
|
||||
discord_utils.GetDisplayName(ctx, request.Author.ID),
|
||||
request.Content,
|
||||
request.RequestedAt.Format("2006-01-02 15:04:05 MST"),
|
||||
channelName,
|
||||
request.RequestedAt.Format("2006-01-02 15:04:05"),
|
||||
discord_utils.GetChannelName(ctx, request.Channel.ID),
|
||||
)
|
||||
for _, c := range comments {
|
||||
cAuthorName, err := discord_utils.GetDisplayName(ctx, c.Author.ID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
cs := fmt.Sprintf("```md\n%s\n- %s At %s\n```\n",
|
||||
c.Content,
|
||||
cAuthorName,
|
||||
c.CommentAt.Format("2006-01-02 15:04:05 MST"),
|
||||
discord_utils.GetDisplayName(ctx, c.Author.ID),
|
||||
c.CommentAt.Format("2006-01-02 15:04:05"),
|
||||
)
|
||||
if len(commentString+cs) >= 2000 {
|
||||
commentStrings = append(commentStrings, commentString)
|
||||
commentString = ""
|
||||
}
|
||||
commentString += cs
|
||||
}
|
||||
commentStrings = append(commentStrings, commentString)
|
||||
for _, c := range channels {
|
||||
if c.Admin {
|
||||
for _, s := range commentStrings {
|
||||
@@ -342,6 +355,95 @@ 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()))
|
||||
dmChannel, err := ctx.Session.UserChannelCreate(request.Author.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, _ = ctx.Session.ChannelMessageSend(dmChannel.ID,
|
||||
fmt.Sprintf("%s has add a comment to request %d which you opened in the %s channel.\n```%s```\n```%s```",
|
||||
discord_utils.GetDisplayName(ctx, author.ID),
|
||||
request.ID,
|
||||
discord_utils.GetChannelName(ctx, ctx.Channel.ID),
|
||||
request.Content,
|
||||
message,
|
||||
))
|
||||
|
||||
}
|
||||
|
||||
var ViewCommand = &disgoman.Command{
|
||||
Name: "view",
|
||||
Aliases: nil,
|
||||
Description: "View the details about a request.",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: viewCommandFunc,
|
||||
}
|
||||
|
||||
func viewCommandFunc(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
|
||||
}
|
||||
id, err := strconv.ParseInt(args[0], 10, 64)
|
||||
if err != nil {
|
||||
discord_utils.SendErrorMessage(ctx, "Please include the ID of the request to view.", err)
|
||||
return
|
||||
}
|
||||
request, err := services.RequestService.Request(id)
|
||||
if err != nil || request.Guild.ID != guild.ID {
|
||||
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%d is not a valid request in this guild", id), err)
|
||||
return
|
||||
}
|
||||
requestor, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID)
|
||||
if err != nil {
|
||||
discord_utils.SendErrorMessage(ctx, "Sorry, there was an issue finding your user account", err)
|
||||
return
|
||||
}
|
||||
if request.Author.ID != ctx.Message.Author.ID &&
|
||||
!discord_utils.IsGuildMod(ctx, requestor) &&
|
||||
!discord_utils.IsGuildAdmin(ctx, requestor) {
|
||||
discord_utils.SendErrorMessage(ctx, "You are not authorized to view that request", nil)
|
||||
return
|
||||
}
|
||||
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 commentStrings []string
|
||||
commentString = fmt.Sprintf("```md\n"+
|
||||
"< Request ID Requested By >\n"+
|
||||
"< %-11d %23s >\n"+
|
||||
"%s\n\n"+
|
||||
"Requested At: %s\n"+
|
||||
"In: %s\n"+
|
||||
"```",
|
||||
request.ID,
|
||||
discord_utils.GetDisplayName(ctx, request.Author.ID),
|
||||
request.Content,
|
||||
request.RequestedAt.Format("2006-01-02 15:04:05"),
|
||||
discord_utils.GetChannelName(ctx, request.Channel.ID),
|
||||
)
|
||||
for _, c := range comments {
|
||||
cs := fmt.Sprintf("```md\n%s\n- %s At %s\n```\n",
|
||||
c.Content,
|
||||
discord_utils.GetDisplayName(ctx, c.Author.ID),
|
||||
c.CommentAt.Format("2006-01-02 15:04:05"),
|
||||
)
|
||||
if len(commentString+cs) >= 2000 {
|
||||
commentStrings = append(commentStrings, commentString)
|
||||
commentString = ""
|
||||
}
|
||||
commentString += cs
|
||||
}
|
||||
commentStrings = append(commentStrings, commentString)
|
||||
for _, c := range commentStrings {
|
||||
_, _ = ctx.Send(c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,3 +49,14 @@ func (s userService) GetOrCreateUser(id string) (geeksbot.User, error) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user