Skip to content

Commit

Permalink
feat: longest command
Browse files Browse the repository at this point in the history
Prints the 10 longest messages
  • Loading branch information
TheTipo01 committed Mar 19, 2022
1 parent 5393199 commit ba7a2b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ var (
Name: "markov",
Description: "Generates a message from the current markov chain",
},
{
Name: "longest",
Description: "Links the longest messages",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "Optional channel for the stat",
Required: false,
},
},
},
}

// Handler
Expand Down Expand Up @@ -586,5 +598,37 @@ var (
sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Markov", strings.Join(tokens[1:len(tokens)-1], " ")).
SetColor(0x7289DA).MessageEmbed, i.Interaction)
},

"longest": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var (
rows *sql.Rows
links string
messageID, channelID string
cont int = 1
err error
)

if len(i.ApplicationCommandData().Options) > 0 {
rows, err = db.Query("SELECT messageID, channelID FROM messages WHERE channelID=? AND deleted=0 ORDER BY length(JSON_VALUE(message, '$.content')) DESC LIMIT 10", i.ApplicationCommandData().Options[0].ChannelValue(nil).ID)
} else {
rows, err = db.Query("SELECT messageID, channelID FROM messages WHERE guildID=? AND deleted=0 ORDER BY length(JSON_VALUE(message, '$.content')) DESC LIMIT 10", i.GuildID)
}

if err != nil {
lit.Error("Error querying database: %s", err.Error())
return
}

for rows.Next() {
err = rows.Scan(&messageID, &channelID)
if err == nil {
links += strconv.Itoa(cont) + ") https://discord.com/channels/" + i.GuildID + "/" + channelID + "/" + messageID + "\n"
cont++
}
}

sendEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Longest", links).
SetColor(0x7289DA).MessageEmbed, i.Interaction)
},
}
)
2 changes: 1 addition & 1 deletion markov.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func saveModel(guildID string) {

// saveAllModels saves all the models in the map server
func saveAllModels() {
for guildID, _ := range server {
for guildID := range server {
saveModel(guildID)
}
}
Expand Down

0 comments on commit ba7a2b7

Please sign in to comment.