Compare commits

..
5 Commits
Author SHA1 Message Date
Dustin Pianalto 4895294969 fix role commands
CI / build (push) Has been cancelled
2021-01-25 23:09:21 -09:00
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
3 changed files with 70 additions and 5 deletions
+5 -3
View File
@@ -113,7 +113,9 @@ func (s guildService) GetOrCreateGuild(id string) (geeksbot.Guild, error) {
}
func (s guildService) CreateOrUpdateRole(r geeksbot.Role) (geeksbot.Role, error) {
queryString := "INSERT INTO roles (id, role_type, guild_id) VALUES ($1, $2, $3)"
_, err := s.db.Exec(queryString, r.ID, r.RoleType, r.Guild.ID)
return r, err
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
}
+64 -2
View File
@@ -2,6 +2,7 @@ package guild
import (
"fmt"
"strings"
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
@@ -21,6 +22,7 @@ var AddModeratorRoleCommand = &disgoman.Command{
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)
@@ -28,8 +30,17 @@ func addModeratorRoleCommandFunc(ctx disgoman.Context, args []string) {
}
roles := append(args, ctx.Message.MentionRoles...)
if len(roles) > 0 {
for _, id := range ctx.Message.MentionRoles {
_, err := services.GuildService.CreateRole(geeksbot.Role{
for _, id := range roles {
if strings.HasPrefix(id, "<@&") && strings.HasSuffix(id, ">") {
continue
}
if _, ok := added[id]; ok {
continue
}
if _, err = ctx.Session.State.Role(ctx.Guild.ID, id); err != nil {
_, _ = ctx.Send(fmt.Sprintf("%s does not reference a valid role for this guild.", id))
}
_, err := services.GuildService.CreateOrUpdateRole(geeksbot.Role{
ID: id,
RoleType: "moderator",
Guild: guild,
@@ -38,10 +49,61 @@ func addModeratorRoleCommandFunc(ctx disgoman.Context, args []string) {
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
}
if _, err = ctx.Session.State.Role(ctx.Guild.ID, id); err != nil {
_, _ = ctx.Send(fmt.Sprintf("%s does not reference a valid role for this guild.", id))
}
_, 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.")
}
}
+1
View File
@@ -25,5 +25,6 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
_ = g.AddCommand(guild.AddPrefixCommand)
_ = g.AddCommand(guild.RemovePrefixCommand)
_ = g.AddCommand(guild.AddModeratorRoleCommand)
_ = g.AddCommand(guild.AddAdminRoleCommand)
_ = g.AddCommand(requests.RequestCommand)
}