Skip to content

Commit

Permalink
fixes to warnings, notifications, moderation config, paginated messag…
Browse files Browse the repository at this point in the history
…es, poll
  • Loading branch information
ashishjh-bst committed Jun 29, 2024
1 parent 2603ec3 commit b7fa128
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 33 deletions.
19 changes: 14 additions & 5 deletions bot/paginatedmessages/paginatedinteractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type PaginatedResponse struct {

var _ dcmd.Response = (*PaginatedResponse)(nil)

func (p *PaginatedResponse) Send(*dcmd.Data) ([]*discordgo.Message, error) {
func (p *PaginatedResponse) Send(data *dcmd.Data) ([]*discordgo.Message, error) {
pm := &PaginatedMessage{
GuildID: p.guildID,
ChannelID: p.channelID,
Expand All @@ -144,11 +144,20 @@ func (p *PaginatedResponse) Send(*dcmd.Data) ([]*discordgo.Message, error) {
Text: footer,
}
embed.Timestamp = time.Now().Format(time.RFC3339)
msg := &discordgo.Message{}
switch data.TriggerType {
case dcmd.TriggerTypeSlashCommands:
msg, err = common.BotSession.CreateFollowupMessage(data.SlashCommandTriggerData.Interaction.ApplicationID, data.SlashCommandTriggerData.Interaction.Token, &discordgo.WebhookParams{
Embeds: []*discordgo.MessageEmbed{embed},
Components: createNavigationButtons(true, nextButtonDisabled),
})
default:
msg, err = common.BotSession.ChannelMessageSendComplex(p.channelID, &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{embed},
Components: createNavigationButtons(true, nextButtonDisabled),
})
}

msg, err := common.BotSession.ChannelMessageSendComplex(p.channelID, &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{embed},
Components: createNavigationButtons(true, nextButtonDisabled),
})
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ require (
github.com/volatiletech/strmangle v0.0.6
goji.io v2.0.2+incompatible
golang.org/x/crypto v0.21.0
golang.org/x/image v0.10.0
golang.org/x/image v0.18.0
golang.org/x/net v0.23.0
golang.org/x/oauth2 v0.10.0
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.14.0
golang.org/x/text v0.16.0
golang.org/x/time v0.3.0
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.131.0
Expand Down
11 changes: 5 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,8 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.10.0 h1:gXjUUtwtx5yOE0VKWq1CH4IJAClq4UGgUA3i+rpON9M=
golang.org/x/image v0.10.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand Down Expand Up @@ -954,7 +954,7 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -1071,9 +1071,8 @@ golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
4 changes: 2 additions & 2 deletions moderation/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ func PaginateWarnings(parsed *dcmd.Data) func(p *paginatedmessages.PaginatedMess
count, err := models.ModerationWarnings(
models.ModerationWarningWhere.UserID.EQ(userIDStr),
models.ModerationWarningWhere.GuildID.EQ(parsed.GuildData.GS.ID),
).CountG(parsed.Context())
).CountG(context.Background())
if err != nil {
return nil, err
}
Expand All @@ -1397,7 +1397,7 @@ func PaginateWarnings(parsed *dcmd.Data) func(p *paginatedmessages.PaginatedMess
qm.OrderBy("id desc"),
qm.Offset(skip),
qm.Limit(limit),
).AllG(parsed.Context())
).AllG(context.Background())
if err != nil {
return nil, err
}
Expand Down
9 changes: 3 additions & 6 deletions moderation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ func GetCachedConfigOrDefault(guildID int64) (*Config, error) {
const cacheDuration = 10 * time.Minute

item, err := configCache.Fetch(cacheKey(guildID), cacheDuration, func() (interface{}, error) {
return GetConfig(guildID)
return GetConfigOrDefault(guildID)
})
if err != nil {
if err == sql.ErrNoRows {
return &Config{GuildID: guildID}, nil
}
return nil, err
}
return item.Value().(*Config), nil
Expand All @@ -117,14 +114,14 @@ func cacheKey(guildID int64) string {
return discordgo.StrID(guildID)
}

func GetConfig(guildID int64) (*Config, error) {
func GetConfigOrDefault(guildID int64) (*Config, error) {
conf, err := models.FindModerationConfigG(context.Background(), guildID)
if err == nil {
return configFromModel(conf), nil
}

if err == sql.ErrNoRows {
return nil, err
return &Config{GuildID: guildID}, nil
}

return nil, err
Expand Down
4 changes: 2 additions & 2 deletions moderation/plugin_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func HandleModeration(w http.ResponseWriter, r *http.Request) (web.TemplateData,
templateData["DefaultTimeoutDuration"] = int(DefaultTimeoutDuration.Minutes())

if _, ok := templateData["ModConfig"]; !ok {
config, err := GetCachedConfigOrDefault(activeGuild.ID)
config, err := GetConfigOrDefault(activeGuild.ID)
if err != nil {
return templateData, err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (w
templateData["WidgetTitle"] = "Moderation"
templateData["SettingsPath"] = "/moderation"

config, err := GetCachedConfigOrDefault(activeGuild.ID)
config, err := GetConfigOrDefault(activeGuild.ID)
if err != nil {
return templateData, err
}
Expand Down
15 changes: 6 additions & 9 deletions notifications/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ func GetCachedConfigOrDefault(guildID int64) (*Config, error) {
const cacheDuration = 10 * time.Minute

item, err := configCache.Fetch(cacheKey(guildID), cacheDuration, func() (interface{}, error) {
return GetConfig(guildID)
return GetConfigOrDefault(guildID)
})
if err != nil {
if err == sql.ErrNoRows {
return &Config{
JoinServerMsgs: []string{"<@{{.User.ID}}> Joined!"},
LeaveMsgs: []string{"**{{.User.Username}}** Left... :'("},
}, nil
}
return nil, err
}
return item.Value().(*Config), nil
Expand All @@ -69,14 +63,17 @@ func cacheKey(guildID int64) string {
return discordgo.StrID(guildID)
}

func GetConfig(guildID int64) (*Config, error) {
func GetConfigOrDefault(guildID int64) (*Config, error) {
conf, err := models.FindGeneralNotificationConfigG(context.Background(), guildID)
if err == nil {
return configFromModel(conf), nil
}

if err == sql.ErrNoRows {
return nil, err
return &Config{
JoinServerMsgs: []string{"<@{{.User.ID}}> Joined!"},
LeaveMsgs: []string{"**{{.User.Username}}** Left... :'("},
}, nil
}

return nil, err
Expand Down
15 changes: 14 additions & 1 deletion stdcommands/poll/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,20 @@ func createPoll(data *dcmd.Data) (interface{}, error) {
common.BotSession.ChannelMessageDelete(data.ChannelID, data.TraditionalTriggerData.Message.ID)
}

pollMsg, err := common.BotSession.ChannelMessageSendEmbed(data.ChannelID, &response)
pollMsg := &discordgo.Message{}
var err error

switch data.TriggerType {
case dcmd.TriggerTypeSlashCommands:
pollMsg, err = common.BotSession.CreateFollowupMessage(data.SlashCommandTriggerData.Interaction.ApplicationID, data.SlashCommandTriggerData.Interaction.Token, &discordgo.WebhookParams{
Embeds: []*discordgo.MessageEmbed{&response},
})
default:
pollMsg, err = common.BotSession.ChannelMessageSendComplex(data.ChannelID, &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{&response},
})
}

if err != nil {
return nil, errors.WrapIf(err, "failed to add poll description")
}
Expand Down

0 comments on commit b7fa128

Please sign in to comment.