Compare commits

...
5 Commits
Author SHA1 Message Date
Dustin Pianalto c34f475084 Add view command and update utils
CI / build (push) Has been cancelled
2021-02-23 23:53:28 -09:00
Dustin Pianalto 1a7d9b824f Add view command and update utils
CI / build (push) Has been cancelled
2021-02-23 23:42:02 -09:00
Dustin Pianalto 0bf31a3e5d Add view command and update utils
CI / build (push) Has been cancelled
2021-02-23 23:29:19 -09:00
Dustin Pianalto 128011c79a Add comment command
CI / build (push) Has been cancelled
2021-02-23 21:56:10 -09:00
Dustin Pianalto 53dc44abda Fix bug when updating request
CI / build (push) Has been cancelled
2021-02-23 17:28:51 -09:00
5 changed files with 226 additions and 5 deletions
+11
View File
@@ -0,0 +1,11 @@
package discord_utils
import "github.com/dustinpianalto/disgoman"
func GetChannelName(ctx disgoman.Context, id string) string {
channel, err := ctx.Session.Channel(id)
if err != nil {
return ""
}
return channel.Name
}
+14
View File
@@ -0,0 +1,14 @@
package discord_utils
import "github.com/dustinpianalto/disgoman"
func GetDisplayName(ctx disgoman.Context, id string) string {
member, err := ctx.Session.GuildMember(ctx.Guild.ID, id)
if err != nil {
return ""
}
if member.Nick != "" {
return member.Nick
}
return member.User.Username
}
+2
View File
@@ -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)
}
+200 -6
View File
@@ -2,6 +2,7 @@ package requests
import (
"fmt"
"log"
"strconv"
"strings"
"time"
@@ -179,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,
))
}
}
@@ -232,8 +243,81 @@ 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"+
"< %-11d %23s >\n"+
"%s\n\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,
))
}
_, _ = 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)], " ")
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
commentString = fmt.Sprintf("Comment added:\n```md\n"+
"< Request ID Requested By >\n"+
"< %-11d %23s >\n"+
"%s\n\n"+
@@ -242,12 +326,122 @@ func listCommandFunc(ctx disgoman.Context, args []string) {
"In: %s\n"+
"```",
request.ID,
authorName,
discord_utils.GetDisplayName(ctx, request.Author.ID),
request.Content,
request.RequestedAt.Format("2006-01-02 15:04:05 MST"),
channelName,
))
discord_utils.GetChannelName(ctx, request.Channel.ID),
)
for _, c := range comments {
if err != nil {
log.Println(err)
continue
}
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 MST"),
)
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 {
_, _ = ctx.Session.ChannelMessageSend(c.ID, s)
}
}
}
}
_, 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,
))
_, _ = ctx.Send(fmt.Sprintf("```There %s currently %d open %s```", utils.PluralizeString("is", len(requests)), len(requests), utils.PluralizeString("request", len(requests))))
}
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 MST"),
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 MST"),
)
if len(commentString+cs) >= 2000 {
commentStrings = append(commentStrings, commentString)
commentString = ""
}
commentString += cs
}
commentStrings = append(commentStrings, commentString)
for _, c := range commentStrings {
_, _ = ctx.Send(c)
}
}
+1 -1
View File
@@ -154,7 +154,7 @@ func (s requestService) UpdateRequest(r geeksbot.Request) (geeksbot.Request, err
queryString := `UPDATE requests SET
completed = $2, completed_at = $3, completed_by = $4, completed_message = $5
WHERE id = $1`
_, err := s.db.Exec(queryString, r.ID, r.Completed, r.CompletedAt, r.CompletedBy, r.CompletedMessage)
_, err := s.db.Exec(queryString, r.ID, r.Completed, r.CompletedAt, r.CompletedBy.ID, r.CompletedMessage)
return r, err
}