comcord/commands/send.go

61 lines
1.3 KiB
Go
Raw Normal View History

package commands
import (
"fmt"
"strings"
2023-07-09 20:00:49 +00:00
"unicode/utf8"
2023-07-09 04:42:58 +00:00
"github.com/Cynosphere/comcord/lib"
"github.com/Cynosphere/comcord/state"
"github.com/bwmarrin/discordgo"
"github.com/ergochat/readline"
"github.com/mgutz/ansi"
)
func SendMode(session *discordgo.Session) {
channelId := state.GetCurrentChannel()
if channelId == "" {
fmt.Print("<not in a channel>\n\r")
return
}
state.SetInPrompt(true)
2023-07-09 20:00:49 +00:00
length := utf8.RuneCountInString(session.State.User.Username) + 2
curLength := state.GetNameLength()
2023-07-09 04:42:58 +00:00
prompt := fmt.Sprintf("[%s]%s", session.State.User.Username, strings.Repeat(" ", (curLength - length) + 1))
if !state.HasNoColor() {
prompt = ansi.Color(prompt, "cyan+b")
}
input, _ := readline.NewFromConfig(&readline.Config{
Prompt: prompt,
UniqueEditLine: true,
})
defer input.Close()
out, err := input.Readline()
out = strings.TrimSpace(out)
input.Close()
if out == "" {
if err == readline.ErrInterrupt {
fmt.Print("^C<no message sent>\n\r")
} else {
fmt.Print(prompt, "<no message sent>\n\r")
}
} else {
fmt.Print(prompt, out, "\n\r")
_, err := session.ChannelMessageSend(channelId, out)
if err != nil {
fmt.Print("<failed to send message: ", err, ">\n\r")
}
// TODO: update afk state
}
state.SetInPrompt(false)
2023-07-09 04:42:58 +00:00
lib.ProcessQueue(session)
}