2023-07-09 01:17:47 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-07-09 20:00:49 +00:00
|
|
|
"unicode/utf8"
|
2023-07-09 01:17:47 +00:00
|
|
|
|
2023-07-09 04:42:58 +00:00
|
|
|
"github.com/Cynosphere/comcord/lib"
|
2023-07-09 01:17:47 +00:00
|
|
|
"github.com/Cynosphere/comcord/state"
|
2023-07-27 02:27:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
2023-07-09 01:17:47 +00:00
|
|
|
"github.com/mgutz/ansi"
|
|
|
|
)
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
func SendMode() {
|
|
|
|
client := state.GetClient()
|
|
|
|
|
2023-07-09 01:17:47 +00:00
|
|
|
channelId := state.GetCurrentChannel()
|
|
|
|
if channelId == "" {
|
|
|
|
fmt.Print("<not in a channel>\n\r")
|
2023-07-11 03:46:47 +00:00
|
|
|
return
|
2023-07-09 01:17:47 +00:00
|
|
|
}
|
2023-07-27 02:27:04 +00:00
|
|
|
parsedChannelId, err := discord.ParseSnowflake(channelId)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print("<failed to parse channel id: ", err.Error(), ">\n\r")
|
|
|
|
return
|
|
|
|
}
|
2023-07-09 01:17:47 +00:00
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
channel, err := client.ChannelStore.Channel(discord.ChannelID(parsedChannelId))
|
2023-07-16 00:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Print("<error getting channel: ", err.Error(), ">\n\r")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
guild, err := client.GuildStore.Guild(channel.GuildID)
|
2023-07-16 00:30:49 +00:00
|
|
|
if err != nil {
|
2023-07-27 02:27:04 +00:00
|
|
|
fmt.Print("<failed to get current guild: ", err.Error(), ">\n\r")
|
2023-07-16 00:30:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
self, err := client.MeStore.Me()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print("<failed to get self: ", err.Error(), ">\n\r")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
selfMember, err := client.MemberStore.Member(guild.ID, self.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print("<failed to get self as member: ", err.Error(), ">\n\r")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
perms := discord.CalcOverwrites(*guild, *channel, *selfMember)
|
|
|
|
|
|
|
|
if !perms.Has(discord.PermissionSendMessages) {
|
2023-07-16 00:30:49 +00:00
|
|
|
fmt.Print("<you do not have permission to send messages here>\n\r")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
length := utf8.RuneCountInString(self.Username) + 2
|
2023-07-09 01:17:47 +00:00
|
|
|
curLength := state.GetNameLength()
|
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
prompt := fmt.Sprintf("[%s]%s", self.Username, strings.Repeat(" ", (curLength - length) + 1))
|
2023-07-09 04:42:58 +00:00
|
|
|
if !state.HasNoColor() {
|
|
|
|
prompt = ansi.Color(prompt, "cyan+b")
|
|
|
|
}
|
2023-07-09 01:17:47 +00:00
|
|
|
|
2023-07-27 02:27:04 +00:00
|
|
|
lib.MakePrompt(prompt, true, func(input string, interrupt bool) {
|
2023-07-11 03:41:51 +00:00
|
|
|
if input == "" {
|
|
|
|
if interrupt {
|
|
|
|
fmt.Print("^C<no message sent>\n\r")
|
|
|
|
} else {
|
|
|
|
fmt.Print(prompt, "<no message sent>\n\r")
|
|
|
|
}
|
2023-07-09 01:17:47 +00:00
|
|
|
} else {
|
2023-07-11 03:41:51 +00:00
|
|
|
fmt.Print(prompt, input, "\n\r")
|
2023-07-27 02:27:04 +00:00
|
|
|
_, err := client.SendMessage(channel.ID, input)
|
2023-07-09 01:17:47 +00:00
|
|
|
|
2023-07-11 03:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Print("<failed to send message: ", err, ">\n\r")
|
|
|
|
}
|
2023-07-09 01:17:47 +00:00
|
|
|
|
2023-07-11 03:41:51 +00:00
|
|
|
// TODO: update afk state
|
|
|
|
}
|
|
|
|
})
|
2023-07-09 01:17:47 +00:00
|
|
|
}
|