history repeats itself
discordgo -> arikawa
This commit is contained in:
parent
7af5462d54
commit
0d6a1398f0
19 changed files with 775 additions and 378 deletions
|
@ -2,10 +2,8 @@ package commands
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func ClearCommand(session *discordgo.Session) {
|
||||
func ClearCommand() {
|
||||
fmt.Print("\n\r\033[H\033[2J")
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
|
||||
"github.com/Cynosphere/comcord/lib"
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
)
|
||||
|
||||
func EmoteCommand(session *discordgo.Session) {
|
||||
func EmoteCommand() {
|
||||
channelId := state.GetCurrentChannel()
|
||||
if channelId == "" {
|
||||
fmt.Print("<not in a channel>\n\r")
|
||||
|
@ -16,7 +16,7 @@ func EmoteCommand(session *discordgo.Session) {
|
|||
}
|
||||
|
||||
prompt := ":emote> "
|
||||
lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
lib.MakePrompt(prompt, true, func(input string, interrupt bool) {
|
||||
if input == "" {
|
||||
if interrupt {
|
||||
fmt.Print("^C<no message sent>\n\r")
|
||||
|
@ -25,10 +25,18 @@ func EmoteCommand(session *discordgo.Session) {
|
|||
}
|
||||
} else {
|
||||
fmt.Print(prompt, input, "\n\r")
|
||||
_, err := session.ChannelMessageSend(channelId, "*" + input + "*")
|
||||
client := state.GetClient()
|
||||
|
||||
snowflake, err := discord.ParseSnowflake(channelId)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to parse channel id: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
_, err = client.SendMessage(discord.ChannelID(snowflake), "*" + input + "*")
|
||||
|
||||
if err != nil {
|
||||
fmt.Print("<failed to send message: ", err, ">\n\r")
|
||||
fmt.Print("<failed to send message: ", err.Error(), ">\n\r")
|
||||
}
|
||||
|
||||
// TODO: update afk state
|
||||
|
|
|
@ -10,9 +10,9 @@ import (
|
|||
|
||||
"github.com/Cynosphere/comcord/lib"
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
tsize "github.com/kopoli/go-terminal-size"
|
||||
"github.com/mgutz/ansi"
|
||||
tsize "github.com/kopoli/go-terminal-size"
|
||||
)
|
||||
|
||||
var REGEX_EMOTE = regexp.MustCompile(`<(?:\x{200b}|&)?a?:(\w+):(\d+)>`)
|
||||
|
@ -23,35 +23,38 @@ type GuildListing struct {
|
|||
Online int
|
||||
}
|
||||
|
||||
func ListGuildsCommand(session *discordgo.Session) {
|
||||
func ListGuildsCommand() {
|
||||
client := state.GetClient()
|
||||
|
||||
longest := 0
|
||||
guilds := make([]GuildListing, 0)
|
||||
|
||||
for _, guild := range session.State.Guilds {
|
||||
clientGuilds, err := client.Guilds()
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get guilds: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
for _, guild := range clientGuilds {
|
||||
length := utf8.RuneCountInString(guild.Name)
|
||||
if length > longest {
|
||||
longest = length
|
||||
}
|
||||
|
||||
guildWithCounts, err := session.GuildWithCounts(guild.ID)
|
||||
if err != nil {
|
||||
withCount, err := client.GuildWithCount(guild.ID)
|
||||
if err == nil {
|
||||
guilds = append(guilds, GuildListing{
|
||||
Name: guild.Name,
|
||||
Members: guild.MemberCount,
|
||||
Online: 0,
|
||||
Members: int(withCount.ApproximateMembers),
|
||||
Online: int(withCount.ApproximatePresences),
|
||||
})
|
||||
} else {
|
||||
guilds = append(guilds, GuildListing{
|
||||
Name: guild.Name,
|
||||
Members: int(guild.ApproximateMembers),
|
||||
Online: int(guild.ApproximatePresences),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if guild.Name == "" && guildWithCounts.Name != "" {
|
||||
guild.Name = guildWithCounts.Name
|
||||
}
|
||||
|
||||
guilds = append(guilds, GuildListing{
|
||||
Name: guild.Name,
|
||||
Members: guildWithCounts.ApproximateMemberCount,
|
||||
Online: guildWithCounts.ApproximatePresenceCount,
|
||||
})
|
||||
}
|
||||
|
||||
fmt.Print("\n\r")
|
||||
|
@ -64,66 +67,89 @@ func ListGuildsCommand(session *discordgo.Session) {
|
|||
fmt.Print("\n\r")
|
||||
}
|
||||
|
||||
func GetSortedChannels(session *discordgo.Session, guildId string, withCategories bool, withPrivate bool) []*discordgo.Channel {
|
||||
channels := make([]*discordgo.Channel, 0)
|
||||
guild, err := session.State.Guild(guildId)
|
||||
func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []discord.Channel {
|
||||
client := state.GetClient()
|
||||
channels := make([]discord.Channel, 0)
|
||||
|
||||
guildSnowflake, err := discord.ParseSnowflake(guildId)
|
||||
if err != nil {
|
||||
return channels
|
||||
}
|
||||
guildChannels := guild.Channels
|
||||
if len(guildChannels) == 0 {
|
||||
c, err := session.GuildChannels(guildId)
|
||||
if err != nil {
|
||||
return channels
|
||||
}
|
||||
guildChannels = c
|
||||
|
||||
parsedGuildId := discord.GuildID(guildSnowflake)
|
||||
|
||||
guild, err := client.GuildStore.Guild(parsedGuildId)
|
||||
if err != nil {
|
||||
return channels
|
||||
}
|
||||
|
||||
guildChannels, err := client.ChannelStore.Channels(guild.ID)
|
||||
if err != nil {
|
||||
return channels
|
||||
}
|
||||
|
||||
self, err := client.MeStore.Me()
|
||||
if err != nil {
|
||||
return channels
|
||||
}
|
||||
|
||||
selfMember, err := client.MemberStore.Member(guild.ID, self.ID)
|
||||
if err != nil {
|
||||
return channels
|
||||
}
|
||||
|
||||
if withCategories {
|
||||
categories := make(map[string][]*discordgo.Channel)
|
||||
categories := make(map[string][]discord.Channel)
|
||||
|
||||
for _, channel := range guildChannels {
|
||||
if channel.Type != discordgo.ChannelTypeGuildText && channel.Type != discordgo.ChannelTypeGuildNews {
|
||||
if channel.Type != discord.GuildText && channel.Type != discord.GuildAnnouncement {
|
||||
continue
|
||||
}
|
||||
|
||||
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
|
||||
private := perms & discordgo.PermissionViewChannel == 0
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if private && !withPrivate {
|
||||
continue
|
||||
}
|
||||
|
||||
categoryID := "0"
|
||||
if channel.ParentID != "" {
|
||||
categoryID = channel.ParentID
|
||||
if channel.ParentID.IsValid() {
|
||||
categoryID = channel.ParentID.String()
|
||||
}
|
||||
|
||||
_, has := categories[categoryID]
|
||||
if !has {
|
||||
categories[categoryID] = make([]*discordgo.Channel, 0)
|
||||
categories[categoryID] = make([]discord.Channel, 0)
|
||||
}
|
||||
|
||||
categories[categoryID] = append(categories[categoryID], channel)
|
||||
}
|
||||
|
||||
for id, channels := range categories {
|
||||
sort.Slice(channels, func(i, j int) bool {
|
||||
return channels[i].Position < channels[j].Position
|
||||
for id, category := range categories {
|
||||
// sort channels by position
|
||||
sort.Slice(category, func(i, j int) bool {
|
||||
return category[i].Position < category[j].Position
|
||||
})
|
||||
categoryChannels := make([]*discordgo.Channel, 0)
|
||||
categoryChannels := make([]discord.Channel, 0)
|
||||
|
||||
// append category channel to top
|
||||
if id != "0" {
|
||||
for _, channel := range guild.Channels {
|
||||
if channel.ID == id {
|
||||
parsedCategoryId, err := discord.ParseSnowflake(id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, channel := range guildChannels {
|
||||
if channel.ID == discord.ChannelID(parsedCategoryId) {
|
||||
categoryChannels = append(categoryChannels, channel)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, channel := range channels {
|
||||
|
||||
// append channels
|
||||
for _, channel := range category {
|
||||
categoryChannels = append(categoryChannels, channel)
|
||||
}
|
||||
categories[id] = categoryChannels
|
||||
|
@ -137,12 +163,15 @@ func GetSortedChannels(session *discordgo.Session, guildId string, withCategorie
|
|||
keys = append(keys, id)
|
||||
}
|
||||
sort.Slice(keys, func(i, j int) bool {
|
||||
ca, _ := session.State.Channel(keys[i])
|
||||
cb, _ := session.State.Channel(keys[j])
|
||||
pa, _ := discord.ParseSnowflake(keys[i])
|
||||
pb, _ := discord.ParseSnowflake(keys[i])
|
||||
|
||||
ca, _ := client.ChannelStore.Channel(discord.ChannelID(pa))
|
||||
cb, _ := client.ChannelStore.Channel(discord.ChannelID(pb))
|
||||
|
||||
return ca.Position < cb.Position
|
||||
})
|
||||
sortedCategories := make(map[string][]*discordgo.Channel)
|
||||
sortedCategories := make(map[string][]discord.Channel)
|
||||
sortedCategories["0"] = categories["0"]
|
||||
|
||||
for _, id := range keys {
|
||||
|
@ -156,16 +185,13 @@ func GetSortedChannels(session *discordgo.Session, guildId string, withCategorie
|
|||
}
|
||||
} else {
|
||||
for _, channel := range guildChannels {
|
||||
if channel.Type != discordgo.ChannelTypeGuildText && channel.Type != discordgo.ChannelTypeGuildNews {
|
||||
if channel.Type != discord.GuildText && channel.Type != discord.GuildAnnouncement {
|
||||
continue
|
||||
}
|
||||
|
||||
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
|
||||
private := perms & discordgo.PermissionViewChannel == 0
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if private && !withPrivate {
|
||||
continue
|
||||
}
|
||||
|
@ -181,24 +207,47 @@ func GetSortedChannels(session *discordgo.Session, guildId string, withCategorie
|
|||
return channels
|
||||
}
|
||||
|
||||
func ListChannelsCommand(session *discordgo.Session) {
|
||||
func ListChannelsCommand() {
|
||||
client := state.GetClient()
|
||||
self, err := client.MeStore.Me()
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get self: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
currentGuild := state.GetCurrentGuild()
|
||||
if currentGuild == "" {
|
||||
fmt.Print("<not in a guild>\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
guildSnowflake, err := discord.ParseSnowflake(currentGuild)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to parse current guild id: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
parsedGuildId := discord.GuildID(guildSnowflake)
|
||||
guild, err := client.GuildStore.Guild(parsedGuildId)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get current guild: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
selfMember, err := client.MemberStore.Member(parsedGuildId, self.ID)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get self member: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
longest := 0
|
||||
channels := GetSortedChannels(session, currentGuild, true, false)
|
||||
channels := GetSortedChannels(currentGuild, true, false)
|
||||
|
||||
for _, channel := range channels {
|
||||
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
|
||||
private := perms & discordgo.PermissionViewChannel == 0
|
||||
category := channel.Type == discordgo.ChannelTypeGuildCategory
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
category := channel.Type == discord.GuildCategory
|
||||
|
||||
catLen := 0
|
||||
if category {
|
||||
|
@ -220,13 +269,10 @@ func ListChannelsCommand(session *discordgo.Session) {
|
|||
fmt.Printf(" %*s created topic\n\r", longest, "channel-name")
|
||||
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
||||
for _, channel := range channels {
|
||||
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
|
||||
private := perms & discordgo.PermissionViewChannel == 0
|
||||
category := channel.Type == discordgo.ChannelTypeGuildCategory
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
category := channel.Type == discord.GuildCategory
|
||||
topic := REGEX_EMOTE.ReplaceAllString(channel.Topic, ":$1:")
|
||||
topic = strings.ReplaceAll(topic, "\n", " ")
|
||||
name := channel.Name
|
||||
|
@ -249,10 +295,8 @@ func ListChannelsCommand(session *discordgo.Session) {
|
|||
}
|
||||
|
||||
created := "??-???-??"
|
||||
timestamp, err := discordgo.SnowflakeTimestamp(channel.ID)
|
||||
if err == nil {
|
||||
created = timestamp.UTC().Format("02-Jan-06")
|
||||
}
|
||||
timestamp := channel.CreatedAt()
|
||||
created = timestamp.UTC().Format("02-Jan-06")
|
||||
|
||||
fmt.Printf(" %*s %s %s\n\r", longest, name, created, topic)
|
||||
}
|
||||
|
@ -263,11 +307,13 @@ func ListChannelsCommand(session *discordgo.Session) {
|
|||
type ListedMember struct {
|
||||
Name string
|
||||
Bot bool
|
||||
Status discordgo.Status
|
||||
Status discord.Status
|
||||
Position int
|
||||
}
|
||||
|
||||
func ListUsersCommand(session *discordgo.Session) {
|
||||
func ListUsersCommand() {
|
||||
client := state.GetClient()
|
||||
|
||||
currentGuild := state.GetCurrentGuild()
|
||||
currentChannel := state.GetCurrentChannel()
|
||||
|
||||
|
@ -280,37 +326,49 @@ func ListUsersCommand(session *discordgo.Session) {
|
|||
return
|
||||
}
|
||||
|
||||
guild, err := session.State.Guild(state.GetCurrentGuild())
|
||||
parsedGuildId, err := discord.ParseSnowflake(currentGuild)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to parse guild id: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
channel, err := session.State.Channel(currentChannel)
|
||||
parsedChannelId, err := discord.ParseSnowflake(currentChannel)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to parse channel id: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print("\n\r")
|
||||
fmt.Printf("[you are in '%s' in '#%s' among %d]\n\r", guild.Name, channel.Name, guild.MemberCount)
|
||||
fmt.Print("\n\r")
|
||||
guild, err := client.GuildStore.Guild(discord.GuildID(parsedGuildId))
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get guild: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
channel, err := client.ChannelStore.Channel(discord.ChannelID(parsedChannelId))
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get channel: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
longest := 0
|
||||
|
||||
sortedMembers := make([]ListedMember, 0)
|
||||
|
||||
for _, presence := range guild.Presences {
|
||||
if presence.Status == discordgo.StatusOffline {
|
||||
presences, err := client.Presences(guild.ID)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get presences: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
for _, presence := range presences {
|
||||
if presence.Status == discord.OfflineStatus {
|
||||
continue
|
||||
}
|
||||
perms, err := session.State.UserChannelPermissions(presence.User.ID, currentChannel)
|
||||
member, err := client.MemberStore.Member(guild.ID, presence.User.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if perms & discordgo.PermissionViewChannel == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
member, err := session.State.Member(currentGuild, presence.User.ID)
|
||||
if err != nil {
|
||||
perms := discord.CalcOverwrites(*guild, *channel, *member)
|
||||
if !perms.Has(discord.PermissionViewChannel) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -320,8 +378,8 @@ func ListUsersCommand(session *discordgo.Session) {
|
|||
}
|
||||
|
||||
position := 0
|
||||
for _, id := range member.Roles {
|
||||
role, err := session.State.Role(currentGuild, id)
|
||||
for _, id := range member.RoleIDs {
|
||||
role, err := client.RoleStore.Role(guild.ID, id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
@ -339,6 +397,10 @@ func ListUsersCommand(session *discordgo.Session) {
|
|||
})
|
||||
}
|
||||
|
||||
fmt.Print("\n\r")
|
||||
fmt.Printf("[you are in '%s' in '#%s' among %d]\n\r", guild.Name, channel.Name, len(sortedMembers))
|
||||
fmt.Print("\n\r")
|
||||
|
||||
membersByPosition := make(map[int][]ListedMember)
|
||||
for _, member := range sortedMembers {
|
||||
_, has := membersByPosition[member.Position]
|
||||
|
@ -377,11 +439,11 @@ func ListUsersCommand(session *discordgo.Session) {
|
|||
for _, member := range members {
|
||||
|
||||
statusColor := "reset"
|
||||
if member.Status == discordgo.StatusOnline {
|
||||
if member.Status == discord.OnlineStatus {
|
||||
statusColor = "green+b"
|
||||
} else if member.Status == discordgo.StatusIdle {
|
||||
} else if member.Status == discord.IdleStatus {
|
||||
statusColor = "yellow+b"
|
||||
} else if member.Status == discordgo.StatusDoNotDisturb {
|
||||
} else if member.Status == discord.DoNotDisturbStatus {
|
||||
statusColor = "red+b"
|
||||
}
|
||||
|
||||
|
@ -425,16 +487,24 @@ func ListUsersCommand(session *discordgo.Session) {
|
|||
}
|
||||
}
|
||||
|
||||
func SwitchGuild(session *discordgo.Session, input string) {
|
||||
func SwitchGuild(input string) {
|
||||
client := state.GetClient()
|
||||
|
||||
if input == "" {
|
||||
ListChannelsCommand(session)
|
||||
ListUsersCommand(session)
|
||||
ListChannelsCommand()
|
||||
ListUsersCommand()
|
||||
} else {
|
||||
target := ""
|
||||
|
||||
for _, guild := range session.State.Guilds {
|
||||
guilds, err := client.GuildStore.Guilds()
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get guilds: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
for _, guild := range guilds {
|
||||
if strings.Index(strings.ToLower(guild.Name), strings.ToLower(input)) > -1 {
|
||||
target = guild.ID
|
||||
target = guild.ID.String()
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -445,34 +515,33 @@ func SwitchGuild(session *discordgo.Session, input string) {
|
|||
state.SetCurrentGuild(target)
|
||||
last := state.GetLastChannel(target)
|
||||
if last == "" {
|
||||
channels := GetSortedChannels(session, target, false, false)
|
||||
// NB: ????????????????
|
||||
channels := GetSortedChannels(target, false, false)
|
||||
if len(channels) > 0 {
|
||||
topChannel := channels[0]
|
||||
|
||||
state.SetCurrentChannel(topChannel.ID)
|
||||
state.SetLastChannel(target, topChannel.ID)
|
||||
state.SetCurrentChannel(topChannel.ID.String())
|
||||
state.SetLastChannel(target, topChannel.ID.String())
|
||||
}
|
||||
} else {
|
||||
state.SetCurrentChannel(last)
|
||||
}
|
||||
|
||||
ListChannelsCommand(session)
|
||||
ListUsersCommand(session)
|
||||
ListChannelsCommand()
|
||||
ListUsersCommand()
|
||||
|
||||
lib.UpdatePresence(session)
|
||||
lib.UpdatePresence()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SwitchGuildsCommand(session *discordgo.Session) {
|
||||
lib.MakePrompt(session, ":guild> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
func SwitchGuildsCommand() {
|
||||
lib.MakePrompt(":guild> ", false, func(input string, interrupt bool) {
|
||||
fmt.Print("\r")
|
||||
SwitchGuild(session, input)
|
||||
SwitchGuild(input)
|
||||
})
|
||||
}
|
||||
|
||||
func SwitchChannelsCommand(session *discordgo.Session) {
|
||||
func SwitchChannelsCommand() {
|
||||
currentGuild := state.GetCurrentGuild()
|
||||
|
||||
if currentGuild == "" {
|
||||
|
@ -480,18 +549,18 @@ func SwitchChannelsCommand(session *discordgo.Session) {
|
|||
return
|
||||
}
|
||||
|
||||
lib.MakePrompt(session, ":channel> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
lib.MakePrompt(":channel> ", false, func(input string, interrupt bool) {
|
||||
fmt.Print("\r")
|
||||
if input == "" {
|
||||
ListUsersCommand(session)
|
||||
ListUsersCommand()
|
||||
} else {
|
||||
target := ""
|
||||
|
||||
channels := GetSortedChannels(session, currentGuild, false, false)
|
||||
channels := GetSortedChannels(currentGuild, false, false)
|
||||
|
||||
for _, channel := range channels {
|
||||
if strings.Index(strings.ToLower(channel.Name), strings.ToLower(input)) > -1 {
|
||||
target = channel.ID
|
||||
target = channel.ID.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -502,9 +571,9 @@ func SwitchChannelsCommand(session *discordgo.Session) {
|
|||
state.SetCurrentChannel(target)
|
||||
state.SetLastChannel(currentGuild, target)
|
||||
|
||||
ListUsersCommand(session)
|
||||
ListUsersCommand()
|
||||
|
||||
lib.UpdatePresence(session)
|
||||
lib.UpdatePresence()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"unicode/utf8"
|
||||
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/mgutz/ansi"
|
||||
)
|
||||
|
||||
|
@ -43,7 +42,7 @@ func lessLower(sa, sb string) bool {
|
|||
}
|
||||
}
|
||||
|
||||
func HelpCommand(session *discordgo.Session) {
|
||||
func HelpCommand() {
|
||||
noColor := state.HasNoColor()
|
||||
|
||||
fmt.Println("\r\nCOMcord (c)left 2023\n\r")
|
||||
|
|
|
@ -8,11 +8,19 @@ import (
|
|||
|
||||
"github.com/Cynosphere/comcord/lib"
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
)
|
||||
|
||||
func GetHistory(session *discordgo.Session, limit int, channel string) {
|
||||
messages, err := session.ChannelMessages(channel, 100, "", "", "")
|
||||
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)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get messages: ", err.Error(), ">\n\r")
|
||||
return
|
||||
|
@ -26,7 +34,7 @@ func GetHistory(session *discordgo.Session, limit int, channel string) {
|
|||
|
||||
lines := make([]string, 0)
|
||||
for _, msg := range messages {
|
||||
msgLines := lib.ProcessMessage(session, msg, lib.MessageOptions{NoColor: true, InHistory: true})
|
||||
msgLines := lib.ProcessMessage(msg, lib.MessageOptions{NoColor: true, InHistory: true})
|
||||
for _, line := range msgLines {
|
||||
lines = append(lines, line)
|
||||
}
|
||||
|
@ -41,43 +49,43 @@ func GetHistory(session *discordgo.Session, limit int, channel string) {
|
|||
fmt.Print("--Review-Complete", strings.Repeat("-", 63), "\n\r")
|
||||
}
|
||||
|
||||
func HistoryCommand(session *discordgo.Session) {
|
||||
func HistoryCommand() {
|
||||
currentChannel := state.GetCurrentChannel()
|
||||
if currentChannel == "" {
|
||||
fmt.Print("<not in a channel>\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
GetHistory(session, 20, currentChannel)
|
||||
GetHistory(20, currentChannel)
|
||||
}
|
||||
|
||||
func ExtendedHistoryCommand(session *discordgo.Session) {
|
||||
func ExtendedHistoryCommand() {
|
||||
currentChannel := state.GetCurrentChannel()
|
||||
if currentChannel == "" {
|
||||
fmt.Print("<not in a channel>\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
lib.MakePrompt(session, ":lines> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
lib.MakePrompt(":lines> ", false, func(input string, interrupt bool) {
|
||||
fmt.Print("\r")
|
||||
limit, err := strconv.Atoi(input)
|
||||
|
||||
if err != nil {
|
||||
fmt.Print("<not a number>\n\r")
|
||||
} else {
|
||||
GetHistory(session, limit, currentChannel)
|
||||
GetHistory(limit, currentChannel)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func PeekHistory(session *discordgo.Session, guild, channel string) {
|
||||
func PeekHistory(guild, channel string) {
|
||||
target := ""
|
||||
|
||||
channels := GetSortedChannels(session, guild, false, false)
|
||||
channels := GetSortedChannels(guild, false, false)
|
||||
|
||||
for _, c := range channels {
|
||||
if strings.Index(strings.ToLower(c.Name), strings.ToLower(channel)) > -1 {
|
||||
target = c.ID
|
||||
target = c.ID.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -85,36 +93,44 @@ func PeekHistory(session *discordgo.Session, guild, channel string) {
|
|||
if target == "" {
|
||||
fmt.Print("<channel not found>\n\r")
|
||||
} else {
|
||||
GetHistory(session, 20, target)
|
||||
GetHistory(20, target)
|
||||
}
|
||||
}
|
||||
|
||||
func PeekCommand(session *discordgo.Session) {
|
||||
func PeekCommand() {
|
||||
currentGuild := state.GetCurrentGuild()
|
||||
if currentGuild == "" {
|
||||
fmt.Print("<not in a guild>\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
lib.MakePrompt(session, ":peek> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
lib.MakePrompt(":peek> ", false, func(input string, interrupt bool) {
|
||||
fmt.Print("\r")
|
||||
|
||||
if input != "" {
|
||||
PeekHistory(session, currentGuild, input)
|
||||
PeekHistory(currentGuild, input)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func CrossPeekCommand(session *discordgo.Session) {
|
||||
lib.MakePrompt(session, ":guild> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
func CrossPeekCommand() {
|
||||
client := state.GetClient()
|
||||
|
||||
lib.MakePrompt(":guild> ", false, func(input string, interrupt bool) {
|
||||
fmt.Print("\r")
|
||||
|
||||
if input != "" {
|
||||
targetGuild := ""
|
||||
|
||||
for _, guild := range session.State.Guilds {
|
||||
guilds, err := client.GuildStore.Guilds()
|
||||
if err != nil {
|
||||
fmt.Print("<failed to get guilds: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
for _, guild := range guilds {
|
||||
if strings.Index(strings.ToLower(guild.Name), strings.ToLower(input)) > -1 {
|
||||
targetGuild = guild.ID
|
||||
targetGuild = guild.ID.String()
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -122,11 +138,11 @@ func CrossPeekCommand(session *discordgo.Session) {
|
|||
if targetGuild == "" {
|
||||
fmt.Print("<guild not found>\n\r")
|
||||
} else {
|
||||
lib.MakePrompt(session, ":peek> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
lib.MakePrompt(":peek> ", false, func(input string, interrupt bool) {
|
||||
fmt.Print("\r")
|
||||
|
||||
if input != "" {
|
||||
PeekHistory(session, targetGuild, input)
|
||||
PeekHistory(targetGuild, input)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package commands
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
var commandMap map[string]Command
|
||||
|
||||
type Command struct {
|
||||
Run func(*discordgo.Session)
|
||||
Run func()
|
||||
Description string
|
||||
}
|
||||
|
||||
|
|
|
@ -4,11 +4,13 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
)
|
||||
|
||||
func QuitCommand(session *discordgo.Session) {
|
||||
func QuitCommand() {
|
||||
client := state.GetClient()
|
||||
|
||||
fmt.Print("Unlinking TTY...\n\r")
|
||||
session.Close()
|
||||
client.Close()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
@ -7,43 +7,64 @@ import (
|
|||
|
||||
"github.com/Cynosphere/comcord/lib"
|
||||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
"github.com/mgutz/ansi"
|
||||
)
|
||||
|
||||
func SendMode(session *discordgo.Session) {
|
||||
func SendMode() {
|
||||
client := state.GetClient()
|
||||
|
||||
channelId := state.GetCurrentChannel()
|
||||
if channelId == "" {
|
||||
fmt.Print("<not in a channel>\n\r")
|
||||
return
|
||||
}
|
||||
parsedChannelId, err := discord.ParseSnowflake(channelId)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to parse channel id: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
channel, err := session.State.Channel(channelId)
|
||||
channel, err := client.ChannelStore.Channel(discord.ChannelID(parsedChannelId))
|
||||
if err != nil {
|
||||
fmt.Print("<error getting channel: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||
guild, err := client.GuildStore.Guild(channel.GuildID)
|
||||
if err != nil {
|
||||
fmt.Print("<failed to check permissions: ", err.Error(), ">\n\r")
|
||||
fmt.Print("<failed to get current guild: ", err.Error(), ">\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
if perms & discordgo.PermissionSendMessages == 0 {
|
||||
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) {
|
||||
fmt.Print("<you do not have permission to send messages here>\n\r")
|
||||
return
|
||||
}
|
||||
|
||||
length := utf8.RuneCountInString(session.State.User.Username) + 2
|
||||
length := utf8.RuneCountInString(self.Username) + 2
|
||||
curLength := state.GetNameLength()
|
||||
|
||||
prompt := fmt.Sprintf("[%s]%s", session.State.User.Username, strings.Repeat(" ", (curLength - length) + 1))
|
||||
prompt := fmt.Sprintf("[%s]%s", self.Username, strings.Repeat(" ", (curLength - length) + 1))
|
||||
if !state.HasNoColor() {
|
||||
prompt = ansi.Color(prompt, "cyan+b")
|
||||
}
|
||||
|
||||
lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) {
|
||||
lib.MakePrompt(prompt, true, func(input string, interrupt bool) {
|
||||
if input == "" {
|
||||
if interrupt {
|
||||
fmt.Print("^C<no message sent>\n\r")
|
||||
|
@ -52,7 +73,7 @@ func SendMode(session *discordgo.Session) {
|
|||
}
|
||||
} else {
|
||||
fmt.Print(prompt, input, "\n\r")
|
||||
_, err := session.ChannelMessageSend(channelId, input)
|
||||
_, err := client.SendMessage(channel.ID, input)
|
||||
|
||||
if err != nil {
|
||||
fmt.Print("<failed to send message: ", err, ">\n\r")
|
||||
|
|
|
@ -3,11 +3,9 @@ package commands
|
|||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func TimeCommand(session *discordgo.Session) {
|
||||
func TimeCommand() {
|
||||
now := time.Now().UTC()
|
||||
|
||||
fmt.Printf("%s\n\r", now.Format("[Mon 02-Jan-06 15:04:05]"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue