Better command abstraction
This commit is contained in:
@ -3,40 +3,23 @@ package command
|
||||
import (
|
||||
"fmt"
|
||||
"himbot/lib"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func HsCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !lib.CheckAndApplyCooldown(s, i, "hs", 10*time.Second) {
|
||||
return
|
||||
}
|
||||
func HsCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
|
||||
options := i.ApplicationCommandData().Options
|
||||
if len(options) == 0 || options[0].Type != discordgo.ApplicationCommandOptionString {
|
||||
return "", fmt.Errorf("please provide a nickname")
|
||||
}
|
||||
nickname := options[0].StringValue()
|
||||
|
||||
options := i.ApplicationCommandData().Options
|
||||
if len(options) == 0 || options[0].Type != discordgo.ApplicationCommandOptionString {
|
||||
lib.RespondWithError(s, i, "Please provide a nickname.")
|
||||
return
|
||||
}
|
||||
nickname := options[0].StringValue()
|
||||
user, err := lib.GetUser(i)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error processing command: %w", err)
|
||||
}
|
||||
|
||||
user, err := lib.GetUser(i)
|
||||
if err != nil {
|
||||
lib.RespondWithError(s, i, "Error processing command: "+err.Error())
|
||||
return
|
||||
}
|
||||
response := fmt.Sprintf("%s was %s's nickname in high school!", nickname, user.Username)
|
||||
|
||||
response := fmt.Sprintf("%s was %s's nickname in high school!", nickname, user.Username)
|
||||
|
||||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: response,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error responding to interaction:", err)
|
||||
lib.RespondWithError(s, i, "An error occurred while processing the command")
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
@ -1,24 +1,15 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"himbot/lib"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !lib.CheckAndApplyCooldown(s, i, "markov", 30*time.Second) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get the channel ID from the interaction
|
||||
func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
|
||||
channelID := i.ChannelID
|
||||
|
||||
// Get the number of messages to fetch from the option
|
||||
numMessages := 100 // Default value
|
||||
if len(i.ApplicationCommandData().Options) > 0 {
|
||||
if i.ApplicationCommandData().Options[0].Name == "messages" {
|
||||
@ -34,8 +25,7 @@ func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
// Fetch messages
|
||||
allMessages, err := fetchMessages(s, channelID, numMessages)
|
||||
if err != nil {
|
||||
lib.RespondWithError(s, i, "Failed to fetch messages: "+err.Error())
|
||||
return
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Build the Markov chain from the fetched messages
|
||||
@ -49,18 +39,7 @@ func MarkovCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
newMessage = "I couldn't generate a message. The channel might be empty or contain no usable text."
|
||||
}
|
||||
|
||||
// Respond to the interaction with the generated message
|
||||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: newMessage,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error responding to interaction: %v", err)
|
||||
lib.RespondWithError(s, i, "An error occurred while processing the command")
|
||||
}
|
||||
return newMessage, nil
|
||||
}
|
||||
|
||||
func fetchMessages(s *discordgo.Session, channelID string, numMessages int) ([]*discordgo.Message, error) {
|
||||
|
@ -1,31 +1,12 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"himbot/lib"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func PingCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !lib.CheckAndApplyCooldown(s, i, "ping", 5*time.Second) {
|
||||
return
|
||||
}
|
||||
|
||||
func PingCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
|
||||
// Customize the response based on whether it's a guild or DM
|
||||
responseContent := "Pong!"
|
||||
|
||||
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: responseContent,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error responding to interaction:", err)
|
||||
// Optionally, you could try to send an error message to the user
|
||||
lib.RespondWithError(s, i, "An error occurred while processing the command")
|
||||
}
|
||||
return responseContent, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user