Compare commits

...
14 Commits
Author SHA1 Message Date
Dustin Pianalto 132bf94332 remove mod/admin role command
CI / build (push) Has been cancelled
2021-01-25 23:53:32 -09:00
Dustin Pianalto 964be05c48 remove mod/admin role command
CI / build (push) Has been cancelled
2021-01-25 23:49:58 -09:00
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
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
Dustin Pianalto 764ceb79ec fix create request sql query
CI / build (push) Has been cancelled
2021-01-25 02:20:11 -09:00
Dustin Pianalto e89805bf8e fix create channel sql query
CI / build (push) Has been cancelled
2021-01-25 02:11:28 -09:00
Dustin Pianalto cb7c487b42 fix create channel sql query
CI / build (push) Has been cancelled
2021-01-25 02:05:13 -09:00
Dustin Pianalto e1ea9cab1f fix create channel sql query
CI / build (push) Has been cancelled
2021-01-25 01:59:57 -09:00
7 changed files with 190 additions and 12 deletions
+1
View File
@@ -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)
}
+4 -9
View File
@@ -29,7 +29,7 @@ func (s channelService) Channel(id string) (geeksbot.Channel, error) {
}
func (s channelService) CreateChannel(c geeksbot.Channel) (geeksbot.Channel, error) {
queryString := "INSERT INTO channels (id, guild_id, admin, default_channel, new_patron VALUES ($1, $2, $3, $4, $5))"
queryString := "INSERT INTO channels (id, guild_id, admin, default_channel, new_patron) VALUES ($1, $2, $3, $4, $5)"
_, err := s.db.Exec(queryString, c.ID, c.Guild.ID, c.Admin, c.Default, c.NewPatron)
return c, err
}
@@ -69,12 +69,9 @@ func (s channelService) GuildChannels(g geeksbot.Guild) ([]geeksbot.Channel, err
func (s channelService) GetOrCreateChannel(id string, guild_id string) (geeksbot.Channel, error) {
channel, err := s.Channel(id)
log.Println(err)
log.Println(1)
if err.Error() == sql.ErrNoRows.Error() {
guild, err := GuildService.GetOrCreateGuild(guild_id)
log.Println(err)
log.Println(2)
if err == sql.ErrNoRows {
var guild geeksbot.Guild
guild, err = GuildService.GetOrCreateGuild(guild_id)
if err != nil {
return geeksbot.Channel{}, err
}
@@ -86,7 +83,5 @@ func (s channelService) GetOrCreateChannel(id string, guild_id string) (geeksbot
NewPatron: false,
})
}
log.Println(err)
log.Println(3)
return channel, err
}
+8
View File
@@ -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
}
+9 -1
View File
@@ -124,6 +124,14 @@ func (s requestService) CreateRequest(r geeksbot.Request) (geeksbot.Request, err
completed, completed_at, completed_by, message_id, completed_message)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id`
var id int64
var completedByID sql.NullString
if r.CompletedBy == nil {
completedByID.String = ""
completedByID.Valid = false
} else {
completedByID.String = r.CompletedBy.ID
completedByID.Valid = true
}
err := s.db.QueryRow(queryString,
r.Author.ID,
r.Channel.ID,
@@ -132,7 +140,7 @@ func (s requestService) CreateRequest(r geeksbot.Request) (geeksbot.Request, err
r.RequestedAt,
r.Completed,
r.CompletedAt,
r.CompletedBy.ID,
completedByID,
r.Message.ID,
r.CompletedMessage).Scan(&id)
if err != nil {
+163
View File
@@ -0,0 +1,163 @@
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
}
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 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))
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.")
}
}
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 _, err := services.GuildService.Role(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: "normal",
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("Set <@&%s> as a normal role.", id))
}
_, _ = ctx.Send(fmt.Sprintf("Set %d roles to normal.", count))
} else {
_, _ = ctx.Send("Please include at least one role to remove from the moderator or admin lists.")
}
}
+3
View File
@@ -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)
}
+2 -2
View File
@@ -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)