-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
latest: Spring 24 Final Exam Routine Added
Schedule message added bug fix. Remove total message use command
- Loading branch information
Showing
19 changed files
with
10,503 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"cSpell.words": ["syronously"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package bot | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/monzim/uiuBot/models" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func (b *Bot) CronSchedule() { | ||
interval, err := time.ParseDuration(os.Getenv("CRON_INTERVAL_DURATION")) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Error parsing the interval") | ||
interval = time.Minute * 5 | ||
} | ||
|
||
ticker := time.NewTicker(interval) | ||
defer ticker.Stop() | ||
|
||
for range ticker.C { | ||
go func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Error().Msgf("Recovered from panic: %v", r) | ||
} | ||
}() | ||
|
||
b.sendScheduledMessages(interval) | ||
}() | ||
} | ||
} | ||
|
||
func (b *Bot) sendScheduledMessages(interval time.Duration) { | ||
var messages []models.CronMessage | ||
err := b.DB.Where("done = ? AND send_time < ?", false, time.Now().Add(interval)).Find(&messages).Error | ||
|
||
log.Info().Msgf("CRON Found %d messages to send", len(messages)) | ||
|
||
if err != nil { | ||
log.Error().Err(err).Msg("Error fetching scheduled messages") | ||
} | ||
|
||
for _, message := range messages { | ||
if message.SendTime.Before(time.Now()) { | ||
discordMessage, err := b.Session.ChannelMessageSend(message.ChannelID, message.Message) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Error sending scheduled message") | ||
} | ||
|
||
message.Done = true | ||
err = b.DB.Save(&message).Error | ||
|
||
if err != nil { | ||
log.Error().Err(err).Msg("Error updating scheduled message") | ||
} | ||
|
||
log.Info().Msgf("Message sent to channel %s with message id %s", message.ChannelID, discordMessage.ID) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/monzim/uiuBot/models" | ||
) | ||
|
||
var handleSendMessage = Commnad{ | ||
Trigger: "send-message", | ||
Command: &discordgo.ApplicationCommand{ | ||
Name: "send-message", | ||
Description: "This will send a message to you", | ||
Options: []*discordgo.ApplicationCommandOption{ | ||
{ | ||
Type: discordgo.ApplicationCommandOptionString, | ||
Name: "message", | ||
Description: "Input your message", | ||
Required: true, | ||
}, | ||
|
||
{ | ||
Type: discordgo.ApplicationCommandOptionString, | ||
Name: "time", | ||
Description: "Choose time unit", | ||
Required: true, | ||
Choices: []*discordgo.ApplicationCommandOptionChoice{ | ||
{ | ||
Name: "minutes", | ||
Value: "minutes", | ||
}, | ||
{ | ||
Name: "hours", | ||
Value: "hours", | ||
}, | ||
{ | ||
Name: "days", | ||
Value: "days", | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
Type: discordgo.ApplicationCommandOptionString, | ||
Name: "value", | ||
Description: "Input the value", | ||
Required: true, | ||
}, | ||
|
||
{ | ||
Type: discordgo.ApplicationCommandOptionChannel, | ||
Name: "channel", | ||
Description: "Select the channel", | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
|
||
Handler: func(op *options) { | ||
if !hasRole(op.in.Member.Roles, os.Getenv("ADMIN_ROLE_ID")) { | ||
op.ses.InteractionRespond(op.in.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: "You don't have permission to run this command", | ||
}, | ||
}) | ||
|
||
return | ||
} | ||
|
||
input := op.in.ApplicationCommandData().Options | ||
|
||
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(input)) | ||
for _, opt := range input { | ||
optionMap[opt.Name] = opt | ||
} | ||
|
||
message := optionMap["message"].StringValue() | ||
timeUnit := optionMap["time"].StringValue() | ||
value := optionMap["value"].StringValue() | ||
channel := optionMap["channel"].ChannelValue(op.ses) | ||
|
||
sendTime := timeToDuration(timeUnit, value) | ||
res := op.db.Create(&models.CronMessage{ | ||
Message: message, | ||
ChannelID: channel.ID, | ||
UserID: op.in.Member.User.ID, | ||
SendTime: sendTime, | ||
}) | ||
|
||
if res.Error != nil { | ||
op.ses.InteractionRespond(op.in.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: "Failed to save schedule message. Please try again later.", | ||
}, | ||
}) | ||
} | ||
|
||
op.ses.InteractionRespond(op.in.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: fmt.Sprintf("Message scheduled for %s in %s", sendTime.Format(time.RFC1123), channel.Mention()), | ||
}, | ||
}) | ||
|
||
}, | ||
} | ||
|
||
func timeToDuration(timeUnit string, value string) time.Time { | ||
var duration time.Duration | ||
after, err := strconv.Atoi(value) | ||
if err != nil { | ||
duration = time.Minute * 10 | ||
} | ||
|
||
switch timeUnit { | ||
case "minutes": | ||
duration = time.Minute * time.Duration(after).Abs() | ||
case "hours": | ||
duration = time.Hour * time.Duration(after).Abs() | ||
case "days": | ||
duration = time.Hour * 24 * time.Duration(after).Abs() | ||
} | ||
|
||
return time.Now().Add(duration) | ||
} | ||
|
||
func hasRole(roles []string, role string) bool { | ||
for _, r := range roles { | ||
if r == role { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.