Compare commits

...
7 Commits
Author SHA1 Message Date
Dustin Pianalto 9d0d1ddd56 fix role commands
CI / build (push) Has been cancelled
2021-01-25 23:04:43 -09:00
Dustin Pianalto a587a64bd9 fix role commands
CI / build (push) Has been cancelled
2021-01-25 23:01:02 -09:00
Dustin Pianalto 53276a1442 add admin role command
CI / build (push) Has been cancelled
2021-01-25 22:41:51 -09:00
Dustin Pianalto cdbeac195f add moderator role command
CI / build (push) Has been cancelled
2021-01-25 22:21:56 -09:00
Dustin Pianalto 8256b596ff add moderator role command
CI / build (push) Has been cancelled
2021-01-25 21:52:12 -09:00
Dustin Pianalto 9e1702fa4b fix create request command
CI / build (push) Has been cancelled
2021-01-25 02:31:36 -09:00
Dustin Pianalto b6f90b61c5 fix create request command
CI / build (push) Has been cancelled
2021-01-25 02:27:56 -09:00
5 changed files with 116 additions and 2 deletions
+1
View File
@@ -25,4 +25,5 @@ type GuildService interface {
UpdateRole(r Role) (Role, error) UpdateRole(r Role) (Role, error)
DeleteRole(r Role) error DeleteRole(r Role) error
GetOrCreateGuild(id string) (Guild, error) GetOrCreateGuild(id string) (Guild, error)
CreateOrUpdateRole(r Role) (Role, error)
} }
+8
View File
@@ -111,3 +111,11 @@ func (s guildService) GetOrCreateGuild(id string) (geeksbot.Guild, error) {
} }
return guild, err return guild, err
} }
func (s guildService) CreateOrUpdateRole(r geeksbot.Role) (geeksbot.Role, error) {
role, err := s.CreateRole(r)
if err != nil && err.Error() == `pq: duplicate key value violates unique constraint "roles_pkey"` {
role, err = s.UpdateRole(r)
}
return role, err
}
+103
View File
@@ -0,0 +1,103 @@
package guild
import (
"fmt"
"strings"
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/internal/services"
)
var AddModeratorRoleCommand = &disgoman.Command{
Name: "addMod",
Aliases: []string{"addModerator", "addModRole"},
Description: "Add a role which is allowed to run moderator commands",
OwnerOnly: false,
Hidden: false,
RequiredPermissions: disgoman.PermissionManageServer,
Invoke: addModeratorRoleCommandFunc,
}
func addModeratorRoleCommandFunc(ctx disgoman.Context, args []string) {
var count int
added := make(map[string]bool)
guild, err := services.GuildService.GetOrCreateGuild(ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Something went wrong getting the guild", err)
return
}
roles := append(args, ctx.Message.MentionRoles...)
if len(roles) > 0 {
for _, id := range roles {
if strings.HasPrefix(id, "<@&") && strings.HasSuffix(id, ">") {
continue
}
if _, ok := added[id]; ok {
continue
}
_, err := services.GuildService.CreateOrUpdateRole(geeksbot.Role{
ID: id,
RoleType: "moderator",
Guild: guild,
})
if err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("There was a problem adding <@&%s>", id), err)
continue
}
added[id] = true
count++
_, _ = ctx.Send(fmt.Sprintf("Added <@&%s> as a moderator role.", id))
}
_, _ = ctx.Send(fmt.Sprintf("Added %d moderator roles.", count))
} else {
_, _ = ctx.Send("Please include at least one role to make a moderator role.")
}
}
var AddAdminRoleCommand = &disgoman.Command{
Name: "addAdmin",
Aliases: []string{"addAdminRole"},
Description: "Add a role which is allowed to run admin commands",
OwnerOnly: false,
Hidden: false,
RequiredPermissions: disgoman.PermissionManageServer,
Invoke: addAdminRoleCommandFunc,
}
func addAdminRoleCommandFunc(ctx disgoman.Context, args []string) {
var count int
added := make(map[string]bool)
guild, err := services.GuildService.GetOrCreateGuild(ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Something went wrong getting the guild", err)
return
}
roles := append(args, ctx.Message.MentionRoles...)
if len(roles) > 0 {
for _, id := range roles {
if strings.HasPrefix(id, "<@&") && strings.HasSuffix(id, ">") {
continue
}
if _, ok := added[id]; ok {
continue
}
_, err := services.GuildService.CreateOrUpdateRole(geeksbot.Role{
ID: id,
RoleType: "admin",
Guild: guild,
})
if err != nil {
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("There was a problem adding <@&%s>", id), err)
continue
}
added[id] = true
count++
_, _ = ctx.Send(fmt.Sprintf("Added <@&%s> as an admin role.", id))
}
_, _ = ctx.Send(fmt.Sprintf("Added %d admin roles.", count))
} else {
_, _ = ctx.Send("Please include at least one role to make an admin role.")
}
}
+2
View File
@@ -24,5 +24,7 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
_ = g.AddCommand(utils.PingCommand) _ = g.AddCommand(utils.PingCommand)
_ = g.AddCommand(guild.AddPrefixCommand) _ = g.AddCommand(guild.AddPrefixCommand)
_ = g.AddCommand(guild.RemovePrefixCommand) _ = g.AddCommand(guild.RemovePrefixCommand)
_ = g.AddCommand(guild.AddModeratorRoleCommand)
_ = g.AddCommand(guild.AddAdminRoleCommand)
_ = g.AddCommand(requests.RequestCommand) _ = g.AddCommand(requests.RequestCommand)
} }
+2 -2
View File
@@ -84,7 +84,7 @@ func requestCommandFunc(ctx disgoman.Context, args []string) {
"In: %s", "In: %s",
mentionRolesString, mentionRolesString,
request.ID, request.ID,
ctx.Message.Author.Mention, ctx.Message.Author.Mention(),
request.Content, request.Content,
request.RequestedAt.UTC().Format(time.UnixDate), request.RequestedAt.UTC().Format(time.UnixDate),
ctx.Channel.Mention(), ctx.Channel.Mention(),
@@ -95,7 +95,7 @@ func requestCommandFunc(ctx disgoman.Context, args []string) {
} }
_, err = ctx.Send(fmt.Sprintf("%s The admin have recieved your request.\n "+ _, err = ctx.Send(fmt.Sprintf("%s The admin have recieved your request.\n "+
"If you would like to close or add a comment to this request please reference ID `%v`", "If you would like to close or add a comment to this request please reference ID `%v`",
ctx.Message.Author.Mention, request.ID, ctx.Message.Author.Mention(), request.ID,
)) ))
if err != nil { if err != nil {
discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was created.", err) discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was created.", err)