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"
|
2023-07-27 02:27:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
2023-07-16 20:58:14 +00:00
|
|
|
)
|
|
|
|
|
2023-07-27 02:27:04 +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 {
|
2023-07-27 02:27:04 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
func HistoryCommand() {
|
2023-07-16 20:58:14 +00:00
|
|
|
currentChannel := state.GetCurrentChannel()
|
|
|
|
if currentChannel == "" {
|
|
|
|
fmt.Print("<not in a channel>\n\r")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
GetHistory(20, currentChannel)
|
2023-07-16 20:58:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +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
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
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 {
|
2023-07-27 02:27:04 +00:00
|
|
|
GetHistory(limit, currentChannel)
|
2023-07-16 20:58:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
func PeekHistory(guild, channel string) {
|
2023-07-16 20:58:14 +00:00
|
|
|
target := ""
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
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 {
|
2023-07-27 02:27:04 +00:00
|
|
|
target = c.ID.String()
|
2023-07-16 20:58:14 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == "" {
|
|
|
|
fmt.Print("<channel not found>\n\r")
|
|
|
|
} else {
|
2023-07-27 02:27:04 +00:00
|
|
|
GetHistory(20, target)
|
2023-07-16 20:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +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
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
lib.MakePrompt(":peek> ", false, func(input string, interrupt bool) {
|
2023-07-16 20:58:14 +00:00
|
|
|
fmt.Print("\r")
|
|
|
|
|
|
|
|
if input != "" {
|
2023-07-27 02:27:04 +00:00
|
|
|
PeekHistory(currentGuild, input)
|
2023-07-16 20:58:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +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 := ""
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
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 {
|
2023-07-27 02:27:04 +00:00
|
|
|
targetGuild = guild.ID.String()
|
2023-07-16 20:58:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetGuild == "" {
|
|
|
|
fmt.Print("<guild not found>\n\r")
|
|
|
|
} else {
|
2023-07-27 02:27:04 +00:00
|
|
|
lib.MakePrompt(":peek> ", false, func(input string, interrupt bool) {
|
2023-07-16 20:58:14 +00:00
|
|
|
fmt.Print("\r")
|
|
|
|
|
|
|
|
if input != "" {
|
2023-07-27 02:27:04 +00:00
|
|
|
PeekHistory(targetGuild, input)
|
2023-07-16 20:58:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|