Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only show reminders in the server if reminders command is used in the server, using dm will show all reminders across all servers. #1645

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions reminders/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var cmds = []*commands.YAGCommand{
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
currentReminders, _ := GetUserReminders(parsed.Author.ID)
if len(currentReminders) >= 25 {
return "You can have a maximum of 25 active reminders, list your reminders with the `reminders` command", nil
return "You can have a maximum of 25 active reminders, list all your reminders with the `reminders` command in DM, doing it in a server will only show reminders set in the server", nil
}

if parsed.Author.Bot {
Expand Down Expand Up @@ -96,21 +96,33 @@ var cmds = []*commands.YAGCommand{
{
CmdCategory: commands.CategoryTool,
Name: "Reminders",
Description: "Lists your active reminders",
Description: "Lists your active reminders in the server, use in DM to see all your reminders",
SlashCommandEnabled: true,
DefaultEnabled: true,
IsResponseEphemeral: true,
RunInDM: true,
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
currentReminders, err := GetUserReminders(parsed.Author.ID)

var currentReminders []*Reminder
var err error
//command was used in DM
inServerString := ""
if parsed.GuildData == nil {
currentReminders, err = GetUserReminders(parsed.Author.ID)
} else {
inServerString = " in this server"
currentReminders, err = GetGuildUserReminder(parsed.Author.ID, parsed.GuildData.GS.ID)
}

if err != nil {
return nil, err
}

if len(currentReminders) == 0 {
return "You have no reminders. Create reminders with the `remindme` command.", nil
return fmt.Sprintf("You have no reminders%s. Create reminders with the `remindme` command.", inServerString), nil
}

out := "Your reminders:\n"
out := fmt.Sprintf("Your reminders%s:\n", inServerString)
out += stringReminders(currentReminders, false)
out += "\nRemove a reminder with `delreminder/rmreminder (id)` where id is the first number for each reminder above.\nTo clear all reminders, use `delreminder` with the `-a` switch."
return out, nil
Expand Down Expand Up @@ -154,6 +166,7 @@ var cmds = []*commands.YAGCommand{
Aliases: []string{"rmreminder"},
Description: "Deletes a reminder. You can delete reminders from other users provided you are running this command in the same guild the reminder was created in and have the Manage Channel permission in the channel the reminder was created in.",
RequiredArgs: 0,
RunInDM: true,
Arguments: []*dcmd.ArgDef{
{Name: "ID", Type: dcmd.Int},
},
Expand Down
37 changes: 8 additions & 29 deletions reminders/reminders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package reminders

import (
"strconv"
"strings"
"time"

"github.com/botlabs-gg/yagpdb/v2/common"
Expand Down Expand Up @@ -81,6 +80,14 @@ func (r *Reminder) Trigger() error {
return nil
}

func GetGuildUserReminder(userID, guildID int64) (results []*Reminder, err error) {
err = common.GORM.Where(&Reminder{UserID: discordgo.StrID(userID), GuildID: guildID}).Find(&results).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
return
}

func GetUserReminders(userID int64) (results []*Reminder, err error) {
err = common.GORM.Where(&Reminder{UserID: discordgo.StrID(userID)}).Find(&results).Error
if err == gorm.ErrRecordNotFound {
Expand Down Expand Up @@ -116,31 +123,3 @@ func NewReminder(userID int64, guildID int64, channelID int64, message string, w
// err = scheduledevents.ScheduleEvent("reminders_check_user:"+strconv.FormatInt(whenUnix, 10), discordgo.StrID(userID), when)
return reminder, err
}

func checkUserEvtHandlerLegacy(evt string) error {
split := strings.Split(evt, ":")
if len(split) < 2 {
logger.Error("Handled invalid check user scheduled event: ", evt)
return nil
}

parsed, _ := strconv.ParseInt(split[1], 10, 64)
reminders, err := GetUserReminders(parsed)
if err != nil {
return err
}

now := time.Now()
nowUnix := now.Unix()
for _, v := range reminders {
if v.When <= nowUnix {
err := v.Trigger()
if err != nil {
// Try again
return err
}
}
}

return nil
}