comcord/commands/history.go

152 lines
3.3 KiB
Go
Raw Normal View History

2023-07-16 20:58:14 +00:00
package commands
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/Cynosphere/comcord/lib"
"github.com/Cynosphere/comcord/state"
"github.com/diamondburned/arikawa/v3/discord"
2023-07-16 20:58:14 +00:00
)
func GetHistory(limit int, channel string) {
client := state.GetClient()
parsedChannelId, err := discord.ParseSnowflake(channel)
if err != nil {
fmt.Print("<failed to parse channel id: ", err.Error(), ">\n\r")
return
}
messages, err := client.Messages(discord.ChannelID(parsedChannelId), 50)
2023-07-16 20:58:14 +00:00
if err != nil {
fmt.Print("<failed to get messages: ", err.Error(), ">\n\r")
return
}
for i, j := 0, len(messages) - 1; i < j; i, j = i + 1, j - 1 {
messages[i], messages[j] = messages[j], messages[i]
}
fmt.Print("--Beginning-Review", strings.Repeat("-", 62), "\n\r")
lines := make([]string, 0)
for _, msg := range messages {
msgLines := lib.ProcessMessage(msg, lib.MessageOptions{NoColor: true, InHistory: true})
2023-07-16 20:58:14 +00:00
for _, line := range msgLines {
lines = append(lines, line)
}
}
length := len(lines)
startIndex := int(math.Max(float64(0), float64(length - limit)))
for _, line := range lines[startIndex:] {
fmt.Print(line)
}
fmt.Print("--Review-Complete", strings.Repeat("-", 63), "\n\r")
}
func HistoryCommand() {
2023-07-16 20:58:14 +00:00
currentChannel := state.GetCurrentChannel()
if currentChannel == "" {
fmt.Print("<not in a channel>\n\r")
return
}
GetHistory(20, currentChannel)
2023-07-16 20:58:14 +00:00
}
func ExtendedHistoryCommand() {
2023-07-16 20:58:14 +00:00
currentChannel := state.GetCurrentChannel()
if currentChannel == "" {
fmt.Print("<not in a channel>\n\r")
return
}
lib.MakePrompt(":lines> ", false, func(input string, interrupt bool) {
2023-07-16 20:58:14 +00:00
fmt.Print("\r")
limit, err := strconv.Atoi(input)
if err != nil {
fmt.Print("<not a number>\n\r")
} else {
GetHistory(limit, currentChannel)
2023-07-16 20:58:14 +00:00
}
})
}
func PeekHistory(guild, channel string) {
2023-07-16 20:58:14 +00:00
target := ""
channels := GetSortedChannels(guild, false, false)
2023-07-16 20:58:14 +00:00
for _, c := range channels {
if strings.Index(strings.ToLower(c.Name), strings.ToLower(channel)) > -1 {
target = c.ID.String()
2023-07-16 20:58:14 +00:00
break
}
}
if target == "" {
fmt.Print("<channel not found>\n\r")
} else {
GetHistory(20, target)
2023-07-16 20:58:14 +00:00
}
}
func PeekCommand() {
2023-07-16 20:58:14 +00:00
currentGuild := state.GetCurrentGuild()
if currentGuild == "" {
fmt.Print("<not in a guild>\n\r")
return
}
lib.MakePrompt(":peek> ", false, func(input string, interrupt bool) {
2023-07-16 20:58:14 +00:00
fmt.Print("\r")
if input != "" {
PeekHistory(currentGuild, input)
2023-07-16 20:58:14 +00:00
}
})
}
func CrossPeekCommand() {
client := state.GetClient()
lib.MakePrompt(":guild> ", false, func(input string, interrupt bool) {
2023-07-16 20:58:14 +00:00
fmt.Print("\r")
if input != "" {
targetGuild := ""
guilds, err := client.GuildStore.Guilds()
if err != nil {
fmt.Print("<failed to get guilds: ", err.Error(), ">\n\r")
return
}
for _, guild := range guilds {
2023-07-16 20:58:14 +00:00
if strings.Index(strings.ToLower(guild.Name), strings.ToLower(input)) > -1 {
targetGuild = guild.ID.String()
2023-07-16 20:58:14 +00:00
break;
}
}
if targetGuild == "" {
fmt.Print("<guild not found>\n\r")
} else {
lib.MakePrompt(":peek> ", false, func(input string, interrupt bool) {
2023-07-16 20:58:14 +00:00
fmt.Print("\r")
if input != "" {
PeekHistory(targetGuild, input)
2023-07-16 20:58:14 +00:00
}
})
}
}
})
}