Compare commits

..
16 Commits
Author SHA1 Message Date
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
Dustin Pianalto a6dc6f5e56 fix get user sql query
CI / build (push) Has been cancelled
2021-01-25 01:52:14 -09:00
Dustin Pianalto 203d1bbf69 fix get user sql query
CI / build (push) Has been cancelled
2021-01-25 01:43:31 -09:00
Dustin Pianalto c504f7b165 fix get user sql query
CI / build (push) Has been cancelled
2021-01-25 01:38:22 -09:00
Dustin Pianalto 34564140f5 add request command
CI / build (push) Has been cancelled
2021-01-25 01:33:46 -09:00
Dustin Pianalto e3ccc56dba add request command
CI / build (push) Has been cancelled
2021-01-25 01:21:49 -09:00
Dustin Pianalto e19f987c4e add request command
CI / build (push) Has been cancelled
2021-01-24 22:57:55 -09:00
Dustin Pianalto 552664268b use custom prefixes
CI / build (push) Has been cancelled
2021-01-24 01:11:22 -09:00
Dustin Pianalto 414757188e remove duplicate prefixes
CI / build (push) Has been cancelled
2021-01-24 01:05:16 -09:00
15 changed files with 246 additions and 8 deletions
+1
View File
@@ -14,4 +14,5 @@ type ChannelService interface {
DeleteChannel(c Channel) error
GuildChannels(g Guild) ([]Channel, error)
UpdateChannel(c Channel) (Channel, error)
GetOrCreateChannel(id string, guild_id string) (Channel, error)
}
+5 -1
View File
@@ -72,7 +72,11 @@ func main() {
}
func getPrefixes(guildID string) []string {
return []string{"G.", "g."}
guild, err := services.GuildService.Guild(guildID)
if err != nil || len(guild.Prefixes) == 0 {
return []string{"G$", "g$"}
}
return guild.Prefixes
}
func ErrorHandler(ErrorChan chan disgoman.CommandError) {
+1
View File
@@ -9,4 +9,5 @@ require (
github.com/golang-migrate/migrate v3.5.4+incompatible // indirect
github.com/golang-migrate/migrate/v4 v4.14.1
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/lib/pq v1.8.0
)
+2
View File
@@ -24,4 +24,6 @@ type GuildService interface {
Role(id string) (Role, error)
UpdateRole(r Role) (Role, error)
DeleteRole(r Role) error
GetOrCreateGuild(id string) (Guild, error)
CreateOrUpdateRole(r Role) (Role, error)
}
+20 -1
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
}
@@ -66,3 +66,22 @@ func (s channelService) GuildChannels(g geeksbot.Guild) ([]geeksbot.Channel, err
}
return channels, nil
}
func (s channelService) GetOrCreateChannel(id string, guild_id string) (geeksbot.Channel, error) {
channel, err := s.Channel(id)
if err == sql.ErrNoRows {
var guild geeksbot.Guild
guild, err = GuildService.GetOrCreateGuild(guild_id)
if err != nil {
return geeksbot.Channel{}, err
}
channel, err = s.CreateChannel(geeksbot.Channel{
ID: id,
Guild: guild,
Admin: false,
Default: false,
NewPatron: false,
})
}
return channel, err
}
+18
View File
@@ -99,3 +99,21 @@ func (s guildService) DeleteRole(r geeksbot.Role) error {
_, err := s.db.Exec(queryString, r.ID)
return err
}
func (s guildService) GetOrCreateGuild(id string) (geeksbot.Guild, error) {
guild, err := s.Guild(id)
if err == sql.ErrNoRows {
guild = geeksbot.Guild{
ID: id,
Prefixes: []string{},
}
guild, err = s.CreateGuild(guild)
}
return guild, err
}
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
}
+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 {
+14 -1
View File
@@ -12,7 +12,7 @@ type userService struct {
func (s userService) User(id string) (geeksbot.User, error) {
var user geeksbot.User
queryString := "SELECT id, steam_id, active, staff, admin WHERE id = $1"
queryString := "SELECT id, steam_id, active, staff, admin FROM users WHERE id = $1"
row := s.db.QueryRow(queryString, id)
err := row.Scan(&user.ID, &user.SteamID, &user.IsActive, &user.IsStaff, &user.IsAdmin)
return user, err
@@ -36,3 +36,16 @@ func (s userService) UpdateUser(u geeksbot.User) (geeksbot.User, error) {
_, err := s.db.Exec(queryString, u.ID, u.SteamID, u.IsActive, u.IsStaff, u.IsAdmin)
return u, err
}
func (s userService) GetOrCreateUser(id string) (geeksbot.User, error) {
user, err := s.User(id)
if err == sql.ErrNoRows {
user, err = s.CreateUser(geeksbot.User{
ID: id,
IsActive: true,
IsAdmin: false,
IsStaff: false,
})
}
return user, err
}
+4 -2
View File
@@ -2,12 +2,12 @@ package guild
import (
"fmt"
"log"
"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 AddPrefixCommand = &disgoman.Command{
@@ -39,6 +39,7 @@ func addPrefixCommandFunc(ctx disgoman.Context, args []string) {
}
} else {
guild.Prefixes = append(guild.Prefixes, args...)
guild.Prefixes = utils.RemoveDuplicateStrings(guild.Prefixes)
guild, err = services.GuildService.UpdateGuild(guild)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error adding prefixes to guild.", err)
@@ -88,10 +89,11 @@ func removePrefixCommandFunc(ctx disgoman.Context, args []string) {
l--
i--
removed = append(removed, a)
log.Println(a)
}
}
}
removed = utils.RemoveDuplicateStrings(removed)
guild.Prefixes = utils.RemoveDuplicateStrings(guild.Prefixes)
guild, err = services.GuildService.UpdateGuild(guild)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error removing prefixes from guild.", err)
+47
View File
@@ -0,0 +1,47 @@
package guild
import (
"fmt"
"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
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 {
_, err := services.GuildService.CreateRole(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
}
count++
_, _ = ctx.Send(fmt.Sprintf("Added <@&%s> as a moderator role.", id))
}
} else {
_, _ = ctx.Send("Please include at least one role to make a moderator role.")
}
}
+3
View File
@@ -3,6 +3,7 @@ package exts
import (
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot/internal/exts/guild"
"github.com/dustinpianalto/geeksbot/internal/exts/requests"
"github.com/dustinpianalto/geeksbot/internal/exts/utils"
)
@@ -23,4 +24,6 @@ func AddCommandHandlers(g *disgoman.CommandManager) {
_ = g.AddCommand(utils.PingCommand)
_ = g.AddCommand(guild.AddPrefixCommand)
_ = g.AddCommand(guild.RemovePrefixCommand)
_ = g.AddCommand(guild.AddModeratorRoleCommand)
_ = g.AddCommand(requests.RequestCommand)
}
+103
View File
@@ -0,0 +1,103 @@
package requests
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/dustinpianalto/disgoman"
"github.com/dustinpianalto/geeksbot"
"github.com/dustinpianalto/geeksbot/internal/discord_utils"
"github.com/dustinpianalto/geeksbot/internal/services"
)
var RequestCommand = &disgoman.Command{
Name: "request",
Aliases: nil,
Description: "Submit a request for the guild staff",
OwnerOnly: false,
Hidden: false,
RequiredPermissions: 0,
Invoke: requestCommandFunc,
}
func requestCommandFunc(ctx disgoman.Context, args []string) {
guild, err := services.GuildService.GetOrCreateGuild(ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error getting Guild from the database", err)
return
}
requestMsg := strings.Join(args, " ")
author, err := services.UserService.GetOrCreateUser(ctx.Message.Author.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error creating the request. Could not get user.", err)
return
}
channel, err := services.ChannelService.GetOrCreateChannel(ctx.Message.ChannelID, ctx.Guild.ID)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error creating the request. Could not get channel.", err)
return
}
int64ID, _ := strconv.ParseInt(ctx.Message.ID, 10, 64)
s := discord_utils.ParseSnowflake(int64ID)
message, err := services.MessageService.CreateMessage(geeksbot.Message{
ID: ctx.Message.ID,
CreatedAt: s.CreationTime,
Content: ctx.Message.Content,
Channel: channel,
Author: author,
})
request := geeksbot.Request{
Author: author,
Channel: channel,
Guild: guild,
Content: requestMsg,
RequestedAt: s.CreationTime,
Completed: false,
Message: message,
}
request, err = services.RequestService.CreateRequest(request)
if err != nil {
discord_utils.SendErrorMessage(ctx, "Error creating the request", err)
return
}
channels, err := services.ChannelService.GuildChannels(guild)
if err == nil {
var mentionRolesString string
roles, err := services.GuildService.GuildRoles(guild)
if err == nil {
for _, r := range roles {
if r.RoleType == "admin" || r.RoleType == "moderator" {
mentionRolesString += fmt.Sprintf("<@&%s> ", r.ID)
}
}
}
for _, c := range channels {
if c.Admin {
_, _ = ctx.Session.ChannelMessageSend(c.ID,
fmt.Sprintf("%s\n"+
"New Request ID %d "+
"%s has requested assistance: \n"+
"```\n%s\n```\n"+
"Requested At: %s\n"+
"In: %s",
mentionRolesString,
request.ID,
ctx.Message.Author.Mention(),
request.Content,
request.RequestedAt.UTC().Format(time.UnixDate),
ctx.Channel.Mention(),
),
)
}
}
}
_, 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,
))
if err != nil {
discord_utils.SendErrorMessage(ctx, "There was an error sending the message. The request was created.", err)
}
}
+13
View File
@@ -1 +1,14 @@
package utils
func RemoveDuplicateStrings(s []string) []string {
keys := make(map[string]bool)
o := []string{}
for _, e := range s {
if _, v := keys[e]; !v {
keys[e] = true
o = append(o, e)
}
}
return o
}
+5 -2
View File
@@ -1,10 +1,13 @@
package geeksbot
import "database/sql"
import (
"database/sql"
"time"
)
type Message struct {
ID string
CreatedAt int64
CreatedAt time.Time
ModifiedAt sql.NullTime
Content string
PreviousContent []string
+1
View File
@@ -15,4 +15,5 @@ type UserService interface {
CreateUser(u User) (User, error)
DeleteUser(u User) error
UpdateUser(u User) (User, error)
GetOrCreateUser(id string) (User, error)
}