Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71aac148e0 | ||
|
|
132bf94332 | ||
|
|
964be05c48 | ||
|
|
4895294969 | ||
|
|
9d0d1ddd56 | ||
|
|
a587a64bd9 | ||
|
|
53276a1442 | ||
|
|
cdbeac195f | ||
|
|
8256b596ff | ||
|
|
9e1702fa4b | ||
|
|
b6f90b61c5 |
@@ -25,4 +25,5 @@ type GuildService interface {
|
||||
UpdateRole(r Role) (Role, error)
|
||||
DeleteRole(r Role) error
|
||||
GetOrCreateGuild(id string) (Guild, error)
|
||||
CreateOrUpdateRole(r Role) (Role, error)
|
||||
}
|
||||
|
||||
@@ -111,3 +111,11 @@ func (s guildService) GetOrCreateGuild(id string) (geeksbot.Guild, error) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
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"
|
||||
"github.com/dustinpianalto/geeksbot/internal/utils"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
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))
|
||||
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 %s.", count, utils.PluralizeString("role", 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))
|
||||
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 %s.", count, utils.PluralizeString("role", count)))
|
||||
} else {
|
||||
_, _ = ctx.Send("Please include at least one role to make an admin role.")
|
||||
}
|
||||
}
|
||||
|
||||
var RemoveModRoleCommand = &disgoman.Command{
|
||||
Name: "removeMod",
|
||||
Aliases: []string{"removeModeratorRole", "removeModRole", "removeAdmin", "removeAdminRole"},
|
||||
Description: "Remove a role or several roles from the moderator or admin list",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: disgoman.PermissionManageServer,
|
||||
Invoke: removeModRoleCommandFunc,
|
||||
}
|
||||
|
||||
func removeModRoleCommandFunc(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 {
|
||||
if r, err := services.GuildService.Role(id); err != nil {
|
||||
_, _ = ctx.Send(fmt.Sprintf("%s does not reference a valid role for this guild.", id))
|
||||
continue
|
||||
} else {
|
||||
err = services.GuildService.DeleteRole(r)
|
||||
if err != nil {
|
||||
discord_utils.SendErrorMessage(ctx, "Something went wrong deleting the role", err)
|
||||
}
|
||||
_, _ = ctx.Send(fmt.Sprintf("Deleted <@&%s> as a no longer a valid role.", id))
|
||||
continue
|
||||
}
|
||||
}
|
||||
_, err := services.GuildService.CreateOrUpdateRole(geeksbot.Role{
|
||||
ID: id,
|
||||
RoleType: "normal",
|
||||
Guild: guild,
|
||||
})
|
||||
if err != nil {
|
||||
discord_utils.SendErrorMessage(ctx, fmt.Sprintf("There was a problem updating <@&%s>", id), err)
|
||||
continue
|
||||
}
|
||||
added[id] = true
|
||||
count++
|
||||
_, _ = ctx.Send(fmt.Sprintf("Set <@&%s> as a normal role.", id))
|
||||
}
|
||||
_, _ = ctx.Send(fmt.Sprintf("Set %d %s to normal.", count, utils.PluralizeString("role", count)))
|
||||
} else {
|
||||
_, _ = ctx.Send("Please include at least one role to remove from the moderator or admin lists.")
|
||||
}
|
||||
}
|
||||
@@ -24,5 +24,8 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
|
||||
_ = g.AddCommand(utils.PingCommand)
|
||||
_ = g.AddCommand(guild.AddPrefixCommand)
|
||||
_ = g.AddCommand(guild.RemovePrefixCommand)
|
||||
_ = g.AddCommand(guild.AddModeratorRoleCommand)
|
||||
_ = g.AddCommand(guild.AddAdminRoleCommand)
|
||||
_ = g.AddCommand(guild.RemoveModRoleCommand)
|
||||
_ = g.AddCommand(requests.RequestCommand)
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ func requestCommandFunc(ctx disgoman.Context, args []string) {
|
||||
"In: %s",
|
||||
mentionRolesString,
|
||||
request.ID,
|
||||
ctx.Message.Author.Mention,
|
||||
ctx.Message.Author.Mention(),
|
||||
request.Content,
|
||||
request.RequestedAt.UTC().Format(time.UnixDate),
|
||||
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 "+
|
||||
"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 {
|
||||
discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was created.", err)
|
||||
|
||||
@@ -12,3 +12,10 @@ func RemoveDuplicateStrings(s []string) []string {
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func PluralizeString(s string, i int) string {
|
||||
if i == 1 {
|
||||
return s
|
||||
}
|
||||
return s + "s"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user