diff --git a/internal/discord_utils/channel_utils.go b/internal/discord_utils/channel_utils.go new file mode 100644 index 0000000..5d55ac6 --- /dev/null +++ b/internal/discord_utils/channel_utils.go @@ -0,0 +1,11 @@ +package discord_utils + +import "github.com/dustinpianalto/disgoman" + +func GetChannelName(ctx disgoman.Context, id string) (string, error) { + channel, err := ctx.Session.Channel(id) + if err != nil { + return "", err + } + return channel.Name, nil +} diff --git a/internal/discord_utils/user_utils.go b/internal/discord_utils/user_utils.go new file mode 100644 index 0000000..01be8fe --- /dev/null +++ b/internal/discord_utils/user_utils.go @@ -0,0 +1,14 @@ +package discord_utils + +import "github.com/dustinpianalto/disgoman" + +func GetDisplayName(ctx disgoman.Context, id string) (string, error) { + member, err := ctx.Session.GuildMember(ctx.Guild.ID, id) + if err != nil { + return "", err + } + if member.Nick != "" { + return member.Nick, nil + } + return member.User.Username, nil +} diff --git a/internal/exts/requests/requests.go b/internal/exts/requests/requests.go index 1beee27..03c3b09 100644 --- a/internal/exts/requests/requests.go +++ b/internal/exts/requests/requests.go @@ -2,6 +2,7 @@ package requests import ( "fmt" + "log" "strconv" "strings" "time" @@ -251,3 +252,96 @@ func listCommandFunc(ctx disgoman.Context, args []string) { _, _ = ctx.Send(fmt.Sprintf("```There %s currently %d open %s```", utils.PluralizeString("is", len(requests)), len(requests), utils.PluralizeString("request", len(requests)))) } + +var CommentCommand = &disgoman.Command{ + Name: "comment", + Aliases: []string{"update", "add_comment"}, + Description: "Add a comment to an existing request.", + OwnerOnly: false, + Hidden: false, + RequiredPermissions: 0, + Invoke: commentCommandFunc, +} + +func commentCommandFunc(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 update.", err) + return + } + message := strings.Join(args[1:len(args)-1], " ") + 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) + 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 for this guild", id), err) + return + } + int64ID, _ := strconv.ParseInt(ctx.Message.ID, 10, 64) + s := discord_utils.ParseSnowflake(int64ID) + comment := geeksbot.Comment{ + Author: author, + Request: request, + CommentAt: s.CreationTime, + Content: message, + } + comment, err = services.RequestService.CreateComment(comment) + if err != nil { + discord_utils.SendErrorMessage(ctx, "There was a problem adding your comment", err) + return + } + channels, err := services.ChannelService.GuildChannels(guild) + if err == nil { + 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"+ + "< %-11d %23s >\n"+ + "%s\n\n"+ + "Comments: Not Implemented Yet\n"+ + "Requested At: %s\n"+ + "In: %s\n"+ + "```", + request.ID, + displayName, + request.Content, + request.RequestedAt.Format("2006-01-02 15:04:05 MST"), + channelName, + ) + 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"), + ) + if len(commentString+cs) >= 2000 { + commentStrings = append(commentStrings, commentString) + commentString = "" + } + } + for _, c := range channels { + if c.Admin { + for _, s := range commentStrings { + _, _ = ctx.Session.ChannelMessageSend(c.ID, s) + } + } + } + } + _, err = ctx.Send(fmt.Sprintf("%s your comment has been added.", ctx.Message.Author.Mention())) +}