Compare commits

...
19 Commits
Author SHA1 Message Date
Dustin Pianalto 83c41e2788 Add ftp credentials to db schema
CI / build (push) Has been cancelled
2021-02-22 23:10:57 -09:00
Dustin Pianalto 24aff1be72 Fix request message
CI / build (push) Has been cancelled
2021-02-22 22:19:25 -09:00
Dustin Pianalto b28beaba0c Fix request message 2021-02-22 22:18:59 -09:00
Dustin Pianalto 4f8aaff94a Fix request message 2021-02-22 22:17:18 -09:00
Dustin Pianalto a21e884bee Fix bug getting messages from db
CI / build (push) Has been cancelled
2021-02-22 22:12:07 -09:00
Dustin Pianalto faf64a8bf9 Add message to end of list with count
CI / build (push) Has been cancelled
2021-02-22 22:03:06 -09:00
Dustin Pianalto 2abe574977 Update database schema to not allow null previous_content
CI / build (push) Has been cancelled
2021-02-22 21:56:18 -09:00
Dustin Pianalto 04c32955c7 Update database schema to not allow null previous_content
CI / build (push) Has been cancelled
2021-02-22 21:29:41 -09:00
Dustin Pianalto 1f70318d26 Update database schema to not allow null previous_content
CI / build (push) Has been cancelled
2021-02-22 21:24:55 -09:00
Dustin Pianalto 47ed519376 Update database schema to not allow null previous_content
CI / build (push) Has been cancelled
2021-02-22 21:16:28 -09:00
Dustin Pianalto 22912c7367 Fix broken SQL query
CI / build (push) Has been cancelled
2021-02-22 20:13:29 -09:00
Dustin Pianalto 6b15540d4e Add close and list commands
CI / build (push) Has been cancelled
2021-02-22 19:59:39 -09:00
Dustin Pianalto e526d27f4a update actions
CI / build (push) Has been cancelled
2021-02-22 19:51:04 -09:00
Dustin Pianalto 68c06a7356 Add request close and list commands
CI / build (push) Has been cancelled
2021-02-22 19:37:38 -09:00
Dustin Pianalto c8277cce42 Merge branch 'development' of github.com:dustinpianalto/Geeksbot into development 2021-02-22 14:05:09 -09:00
Dustin Pianalto 3069e24e8d Move db and services to pkg 2021-02-22 14:04:51 -09:00
Dustin Pianalto 472f23cc2b start close command 2021-02-07 11:27:33 -09:00
Dustin Pianalto 6d920e615d Add sar commands
CI / build (push) Has been cancelled
2021-01-30 22:52:25 -09:00
Dustin Pianalto 0b8b57a884 Add sar commands
CI / build (push) Has been cancelled
2021-01-30 22:42:14 -09:00
31 changed files with 270 additions and 19 deletions
+5 -3
View File
@@ -26,8 +26,8 @@ jobs:
GOPATH: /home/runner/work/Geeksbot/
run: |
go get -u github.com/go-bindata/go-bindata/...
/home/runner/work/Geeksbot/bin/go-bindata -pkg migrations -prefix $GITHUB_WORKSPACE/internal/database/migrations/ -o $GITHUB_WORKSPACE/internal/database/migrations/bindata.go $GITHUB_WORKSPACE/internal/database/migrations/
head $GITHUB_WORKSPACE/internal/database/migrations/bindata.go
/home/runner/work/Geeksbot/bin/go-bindata -pkg migrations -prefix $GITHUB_WORKSPACE/pkg/database/migrations/ -o $GITHUB_WORKSPACE/pkg/database/migrations/bindata.go $GITHUB_WORKSPACE/pkg/database/migrations/
head $GITHUB_WORKSPACE/pkg/database/migrations/bindata.go
- name: Build container image
env:
@@ -43,7 +43,9 @@ jobs:
run: doctl registry login --expiry-seconds 600
- name: Push image to DigitalOcean Container Registry
run: docker push registry.digitalocean.com/djpianalto/geeksbot
run: docker push registry.digitalocean.com/djpianalto/geeksbot:$IMAGE_TAG
env:
IMAGE_TAG: ${{ steps.get_version.outputs.version-without-v }}
- name: Update deployment file
run: TAG=${{ steps.get_version.outputs.version-without-v }} && sed -i 's|<IMAGE>|registry.digitalocean.com/djpianalto/geeksbot:'${TAG}'|' $GITHUB_WORKSPACE/deployment.yml
+2 -2
View File
@@ -8,9 +8,9 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot/internal/database"
"github.com/dustinpianalto/geeksbot/internal/exts"
"github.com/dustinpianalto/geeksbot/internal/services"
"github.com/dustinpianalto/geeksbot/pkg/database"
"github.com/dustinpianalto/geeksbot/pkg/services"
)
func main() {
+49
View File
@@ -0,0 +1,49 @@
package discord_utils
import (
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/pkg/services"
)
func IsGuildMod(ctx disgoman.Context, user geeksbot.User) bool {
discordCloser, err := ctx.Session.GuildMember(ctx.Guild.ID, user.ID)
if err != nil {
return false
}
guildRoles, err := services.GuildService.GuildRoles(geeksbot.Guild{ID: ctx.Guild.ID})
if err != nil {
return false
}
for _, role := range guildRoles {
if role.RoleType == "moderator" {
for _, rID := range discordCloser.Roles {
if rID == role.ID {
return true
}
}
}
}
return false
}
func IsGuildAdmin(ctx disgoman.Context, user geeksbot.User) bool {
discordCloser, err := ctx.Session.GuildMember(ctx.Guild.ID, user.ID)
if err != nil {
return false
}
guildRoles, err := services.GuildService.GuildRoles(geeksbot.Guild{ID: ctx.Guild.ID})
if err != nil {
return false
}
for _, role := range guildRoles {
if role.RoleType == "admin" {
for _, rID := range discordCloser.Roles {
if rID == role.ID {
return true
}
}
}
}
return false
}
+1 -1
View File
@@ -6,8 +6,8 @@ import (
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/internal/services"
"github.com/dustinpianalto/geeksbot/internal/utils"
"github.com/dustinpianalto/geeksbot/pkg/services"
)
var AddPrefixCommand = &disgoman.Command{
+26 -6
View File
@@ -2,14 +2,15 @@ package guild
import (
"fmt"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/internal/services"
"github.com/dustinpianalto/geeksbot/internal/utils"
"github.com/dustinpianalto/geeksbot/pkg/services"
)
var AddModeratorRoleCommand = &disgoman.Command{
@@ -277,7 +278,7 @@ func removeSelfAssignableRoleCommandFunc(ctx disgoman.Context, args []string) {
var SelfAssignRoleCommand = &disgoman.Command{
Name: "giverole",
Aliases: []string{"iwant", "givetome"},
Aliases: []string{"iwant", "givetome", "addrole"},
Description: "Assigns a person the passed in role if it is self assignable",
OwnerOnly: false,
Hidden: false,
@@ -290,16 +291,26 @@ func selfAssignRoleCommandFunc(ctx disgoman.Context, args []string) {
roles := append(args, ctx.Message.MentionRoles...)
if len(roles) > 0 {
for _, id := range roles {
var roleID string
if strings.HasPrefix(id, "<@&") && strings.HasSuffix(id, ">") {
continue
} else if _, err := strconv.Atoi(id); err == nil {
roleID = id
} else {
for _, role := range ctx.Guild.Roles {
if strings.ToLower(id) == strings.ToLower(role.Name) {
roleID = role.ID
}
}
}
if _, ok := added[id]; ok {
continue
}
var role *discordgo.Role
var err error
if role, err = ctx.Session.State.Role(ctx.Guild.ID, id); err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%s does not reference a valid role for this guild", id), err)
if role, err = ctx.Session.State.Role(ctx.Guild.ID, roleID); err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%s does not reference a valid role for this guild", roleID), err)
return
}
if memberHasRole(ctx.Member, role.ID) {
@@ -339,8 +350,17 @@ func unAssignRoleCommandFunc(ctx disgoman.Context, args []string) {
roles := append(args, ctx.Message.MentionRoles...)
if len(roles) > 0 {
for _, id := range roles {
var roleID string
if strings.HasPrefix(id, "<@&") && strings.HasSuffix(id, ">") {
continue
} else if _, err := strconv.Atoi(id); err == nil {
roleID = id
} else {
for _, role := range ctx.Guild.Roles {
if strings.ToLower(id) == strings.ToLower(role.Name) {
roleID = role.ID
}
}
}
if _, ok := removed[id]; ok {
continue
@@ -348,8 +368,8 @@ func unAssignRoleCommandFunc(ctx disgoman.Context, args []string) {
var role *discordgo.Role
var err error
if role, err = ctx.Session.State.Role(ctx.Guild.ID, id); err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%s does not reference a valid role for this guild", id), err)
if role, err = ctx.Session.State.Role(ctx.Guild.ID, roleID); err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%s does not reference a valid role for this guild", roleID), err)
return
}
if !memberHasRole(ctx.Member, role.ID) {
+2
View File
@@ -32,4 +32,6 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
_ = g.AddCommand(guild.SelfAssignRoleCommand)
_ = g.AddCommand(guild.UnAssignRoleCommand)
_ = g.AddCommand(requests.RequestCommand)
_ = g.AddCommand(requests.CloseCommand)
_ = g.AddCommand(requests.ListCommand)
}
+151 -1
View File
@@ -9,7 +9,8 @@ import (
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/internal/services"
"github.com/dustinpianalto/geeksbot/internal/utils"
"github.com/dustinpianalto/geeksbot/pkg/services"
)
var RequestCommand = &disgoman.Command{
@@ -101,3 +102,152 @@ func requestCommandFunc(ctx disgoman.Context, args []string) {
discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was created.", err)
}
}
var CloseCommand = &disgoman.Command{
Name: "close",
Aliases: nil,
Description: "Close a request and mark it as completed.",
OwnerOnly: false,
Hidden: false,
RequiredPermissions: 0,
Invoke: closeCommandFunc,
}
func closeCommandFunc(ctx disgoman.Context, args []string) {
var ids []int64
var reason string
_, err := strconv.ParseInt(args[len(args)-1], 10, 64)
if err != nil {
reason = args[len(args)-1]
args = args[0 : len(args)-1]
}
for _, a := range args {
a = strings.Trim(a, ",")
id, err := strconv.ParseInt(a, 10, 64)
if err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%s is not a valid request id", a), err)
continue
}
ids = append(ids, id)
}
if len(ids) == 0 {
discord_utils.SendErrorMessage(ctx, "No requests to close", nil)
return
}
guild, err := services.GuildService.GetOrCreateGuild(ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error getting Guild from the database", err)
return
}
closer, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error closing the request. Could not get user.", err)
return
}
int64ID, _ := strconv.ParseInt(ctx.Message.ID, 10, 64)
s := discord_utils.ParseSnowflake(int64ID)
for _, id := range ids {
request, err := services.RequestService.Request(id)
if err != nil || request.Guild.ID != guild.ID {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%d is not a request in this guild.", id), err)
continue
}
if request.Author != closer && !closer.IsStaff && !closer.IsAdmin {
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
}
}
if request.Completed {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("%d is already closed", id), err)
continue
}
request.Completed = true
request.CompletedAt.Valid = true
request.CompletedAt.Time = s.CreationTime
request.CompletedBy = &closer
if reason != "" {
request.CompletedMessage.Valid = true
request.CompletedMessage.String = reason
}
request, err = services.RequestService.UpdateRequest(request)
if err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("Error closing %d", id), err)
continue
}
_, err = ctx.Send(fmt.Sprintf("%d has been closed", request.ID))
if err != nil {
discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was closed.", err)
}
}
}
var ListCommand = &disgoman.Command{
Name: "list",
Aliases: []string{"request_list", "requests_list"},
Description: "List your open requests or all open requests if the caller is a moderator",
OwnerOnly: false,
Hidden: false,
RequiredPermissions: 0,
Invoke: listCommandFunc,
}
func listCommandFunc(ctx disgoman.Context, args []string) {
user, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error closing the request. Could not get user.", err)
return
}
guild, err := services.GuildService.GetOrCreateGuild(ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error getting Guild from the database", err)
return
}
var requests []geeksbot.Request
if discord_utils.IsGuildMod(ctx, user) || discord_utils.IsGuildAdmin(ctx, user) {
requests, err = services.RequestService.GuildRequests(guild, false)
} else {
requests, err = services.RequestService.UserRequests(user, false)
}
for _, request := range requests {
var authorName string
author, err := ctx.Session.GuildMember(guild.ID, request.Author.ID)
if err != nil {
authorName = "Unknown"
} else {
if author.Nick == "" {
authorName = author.User.Username
} else {
authorName = author.Nick
}
}
var channelName string
channel, err := ctx.Session.Channel(request.Channel.ID)
if err != nil {
channelName = "Unknown"
} else {
channelName = channel.Name
}
_, _ = ctx.Send(fmt.Sprintf("```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,
authorName,
request.Content,
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))))
}
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/internal/services"
"github.com/dustinpianalto/geeksbot/pkg/services"
)
var PingCommand = &disgoman.Command{
+3
View File
@@ -17,5 +17,8 @@ func PluralizeString(s string, i int) string {
if i == 1 {
return s
}
if s == "is" {
return "are"
}
return s + "s"
}
@@ -5,7 +5,7 @@ import (
"fmt"
"log"
"github.com/dustinpianalto/geeksbot/internal/database/migrations"
"github.com/dustinpianalto/geeksbot/pkg/database/migrations"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
@@ -5,6 +5,7 @@ import (
"log"
"github.com/dustinpianalto/geeksbot"
"github.com/lib/pq"
)
type messageService struct {
@@ -21,7 +22,7 @@ func (s messageService) Message(id string) (geeksbot.Message, error) {
WHERE m.id = $1`
row := s.db.QueryRow(queryString, id)
err := row.Scan(&m.ID, &m.CreatedAt, &m.ModifiedAt, &m.Content,
&m.PreviousContent, &channel_id, &author_id)
pq.Array(&m.PreviousContent), &channel_id, &author_id)
if err != nil {
return geeksbot.Message{}, err
}
@@ -0,0 +1,6 @@
BEGIN;
ALTER TABLE messages
ALTER COLUMN previous_content DROP NOT NULL;
ALTER TABLE messages
ALTER COLUMN previous_content DROP DEFAULT;
COMMIT;
@@ -0,0 +1,6 @@
BEGIN;
ALTER TABLE messages
ALTER COLUMN previous_content SET NOT NULL;
ALTER TABLE messages
ALTER COLUMN previous_content SET DEFAULT array[]::varchar[];
COMMIT;
@@ -0,0 +1,6 @@
BEGIN;
ALTER TABLE servers
DROP COLUMN ftp_port,
DROP COLUMN ftp_username,
DROP COLUMN ftp_password;
COMMIT;
@@ -0,0 +1,6 @@
BEGIN;
ALTER TABLE servers
ADD COLUMN ftp_port int4,
ADD COLUMN ftp_username varchar(200),
ADD COLUMN ftp_password varchar(200);
COMMIT;
@@ -19,7 +19,7 @@ func (s requestService) Request(id int64) (geeksbot.Request, error) {
var uID sql.NullString
var mID string
queryString := `SELECT id, author_id, channel_id, guild_id, content, requested_at, completed,
completed_at, completed_by, message_id, completed_message
completed_at, completed_by, message_id, completed_message FROM requests
WHERE id = $1`
row := s.db.QueryRow(queryString, id)
err := row.Scan(&r.ID, &aID, &cID, &gID, &r.Content, &r.RequestedAt, &r.Completed,
@@ -168,7 +168,7 @@ func (s requestService) Comment(id int64) (geeksbot.Comment, error) {
var c geeksbot.Comment
var aID string
var rID int64
queryString := "SELECT id, author_id, request_id, comment_at, content WHERE id = $1"
queryString := "SELECT id, author_id, request_id, comment_at, content FROM comments WHERE id = $1"
row := s.db.QueryRow(queryString, id)
err := row.Scan(&c.ID, &aID, &rID, &c.CommentAt, &c.Content)
if err != nil {
@@ -2,7 +2,7 @@ package services
import (
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/database"
"github.com/dustinpianalto/geeksbot/pkg/database"
)
var (