This commit is contained in:
Atridad Lahiji 2024-10-22 10:02:41 -06:00
parent d2f94dec79
commit 999605c2fc
Signed by: atridad
SSH key fingerprint: SHA256:LGomp8Opq0jz+7kbwNcdfTcuaLRb5Nh0k5AchDDb438

View file

@ -12,35 +12,57 @@ import (
func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) { func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
log.Println("MarkovCommand called") log.Println("MarkovCommand called")
// Get the channel ID from the interaction // Get the channel ID from the interaction
channelID := i.ChannelID channelID := i.ChannelID
log.Printf("Channel ID: %s", channelID) log.Printf("Channel ID: %s", channelID)
// Get the number of messages to fetch from the option // Get the number of messages to fetch from the option
numMessages := 100 // Default value numMessages := 100 // Default value
if len(i.ApplicationCommandData().Options) > 0 { if len(i.ApplicationCommandData().Options) > 0 {
if i.ApplicationCommandData().Options[0].Name == "messages" { if i.ApplicationCommandData().Options[0].Name == "messages" {
numMessages = int(i.ApplicationCommandData().Options[0].IntValue()) numMessages = int(i.ApplicationCommandData().Options[0].IntValue())
if numMessages <= 0 { if numMessages <= 0 {
numMessages = 100 numMessages = 100
} else if numMessages > 1000 { } else if numMessages > 1000 {
numMessages = 1000 numMessages = 1000 // Limit to 1000 messages max
} }
} }
} }
log.Printf("Fetching %d messages", numMessages) log.Printf("Fetching up to %d messages", numMessages)
// Fetch the specified number of messages from the channel // Fetch messages in batches
messages, err := s.ChannelMessages(channelID, numMessages, "", "", "") var allMessages []*discordgo.Message
if err != nil { var lastMessageID string
log.Printf("Error fetching messages: %v", err)
respondWithError(s, i, "Failed to fetch messages") for len(allMessages) < numMessages {
return batchSize := 100
} if numMessages-len(allMessages) < 100 {
log.Printf("Fetched %d messages", len(messages)) batchSize = numMessages - len(allMessages)
}
batch, err := s.ChannelMessages(channelID, batchSize, lastMessageID, "", "")
if err != nil {
log.Printf("Error fetching messages: %v", err)
respondWithError(s, i, "Failed to fetch messages")
return
}
if len(batch) == 0 {
break // No more messages to fetch
}
allMessages = append(allMessages, batch...)
lastMessageID = batch[len(batch)-1].ID
if len(batch) < 100 {
break // Less than 100 messages returned, we've reached the end
}
}
log.Printf("Fetched %d messages", len(allMessages))
// Build the Markov chain from the fetched messages // Build the Markov chain from the fetched messages
chain := buildMarkovChain(messages) chain := buildMarkovChain(allMessages)
log.Printf("Built Markov chain with %d entries", len(chain)) log.Printf("Built Markov chain with %d entries", len(chain))
// Generate a new message using the Markov chain // Generate a new message using the Markov chain
@ -53,7 +75,7 @@ func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
} }
// Respond to the interaction with the generated message // Respond to the interaction with the generated message
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: newMessage, Content: newMessage,