| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- package main
- import (
- "encoding/json"
- "fmt"
- "regexp"
- "sort"
- "strings"
- "github.com/ayntgl/discordgo"
- "github.com/gen2brain/beeep"
- "github.com/rivo/tview"
- )
- var (
- boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`)
- italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`)
- underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`)
- strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
- )
- func newSession() *discordgo.Session {
- s, err := discordgo.New()
- if err != nil {
- panic(err)
- }
- s.UserAgent = conf.UserAgent
- s.Identify.Compress = false
- s.Identify.Intents = 0
- s.Identify.LargeThreshold = 0
- s.Identify.Properties.Device = ""
- s.Identify.Properties.Browser = "Chrome"
- s.Identify.Properties.OS = "Linux"
- s.AddHandlerOnce(onSessionReady)
- s.AddHandler(onSessionMessageCreate)
- return s
- }
- func onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
- dmNode := tview.NewTreeNode("Direct Messages").
- Collapse()
- n := channelsTree.GetRoot()
- n.AddChild(dmNode)
- sort.Slice(r.Guilds, func(a, b int) bool {
- found := false
- for _, gID := range r.Settings.GuildPositions {
- if found {
- if gID == r.Guilds[b].ID {
- return true
- }
- } else {
- if gID == r.Guilds[a].ID {
- found = true
- }
- }
- }
- return false
- })
- for _, g := range r.Guilds {
- gn := tview.NewTreeNode(g.Name).
- SetReference(g.ID).
- Collapse()
- n.AddChild(gn)
- }
- channelsTree.SetCurrentNode(n)
- }
- func onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
- c, err := session.State.Channel(m.ChannelID)
- if err != nil {
- return
- }
- if selectedChannel == nil || selectedChannel.ID != m.ChannelID {
- if conf.Notifications {
- for _, u := range m.Mentions {
- if u.ID == session.State.User.ID {
- g, err := session.State.Guild(m.GuildID)
- if err != nil {
- return
- }
- go beeep.Alert(fmt.Sprintf("%s (#%s)", g.Name, c.Name), m.ContentWithMentionsReplaced(), "")
- return
- }
- }
- }
- cn := getTreeNodeByReference(channelsTree, c.ID)
- if cn == nil {
- return
- }
- cn.SetText("[::b]" + channelToString(c) + "[::-]")
- app.Draw()
- } else {
- selectedChannel.Messages = append(selectedChannel.Messages, m.Message)
- messagesView.Write(buildMessage(m.Message))
- // Scroll to the end of the text if any message is selected.
- if len(messagesView.GetHighlights()) != 0 {
- messagesView.ScrollToEnd()
- }
- }
- }
- // channelToString constructs a string representation of the given channel. The string representation may vary for different channel types.
- func channelToString(c *discordgo.Channel) string {
- var repr string
- if c.Name != "" {
- repr = "#" + c.Name
- } else if len(c.Recipients) == 1 {
- rp := c.Recipients[0]
- repr = rp.Username + "#" + rp.Discriminator
- } else {
- rps := make([]string, len(c.Recipients))
- for i, r := range c.Recipients {
- rps[i] = r.Username + "#" + r.Discriminator
- }
- repr = strings.Join(rps, ", ")
- }
- return repr
- }
- type loginResponse struct {
- MFA bool `json:"mfa"`
- SMS bool `json:"sms"`
- Ticket string `json:"ticket"`
- Token string `json:"token"`
- }
- func login(email, password string) (*loginResponse, error) {
- data := struct {
- Email string `json:"email"`
- Password string `json:"password"`
- }{email, password}
- resp, err := session.RequestWithBucketID(
- "POST",
- discordgo.EndpointLogin,
- data,
- discordgo.EndpointLogin,
- )
- if err != nil {
- return nil, err
- }
- var lr loginResponse
- err = json.Unmarshal(resp, &lr)
- if err != nil {
- return nil, err
- }
- return &lr, nil
- }
- func totp(code, ticket string) (*loginResponse, error) {
- data := struct {
- Code string `json:"code"`
- Ticket string `json:"ticket"`
- }{code, ticket}
- e := discordgo.EndpointAuth + "mfa/totp"
- resp, err := session.RequestWithBucketID("POST", e, data, e)
- if err != nil {
- return nil, err
- }
- var lr loginResponse
- err = json.Unmarshal(resp, &lr)
- if err != nil {
- return nil, err
- }
- return &lr, nil
- }
- func buildMessage(m *discordgo.Message) []byte {
- var b strings.Builder
- switch m.Type {
- case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
- // Define a new region and assign message ID as the region ID.
- // Learn more:
- // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
- b.WriteString("[\"")
- b.WriteString(m.ID)
- b.WriteString("\"]")
- // Build the message associated with crosspost, channel follow add, pin, or a reply.
- buildReferencedMessage(&b, m.ReferencedMessage)
- // Build the author of this message.
- buildAuthor(&b, m.Author)
- // Build the contents of the message.
- buildContent(&b, m)
- if m.EditedTimestamp != "" {
- b.WriteString(" [::d](edited)[::-]")
- }
- // Build the embeds associated with the message.
- buildEmbeds(&b, m.Embeds)
- // Build the message attachments (attached files to the message).
- buildAttachments(&b, m.Attachments)
- // Tags with no region ID ([""]) do not start new regions. They can
- // therefore be used to mark the end of a region.
- b.WriteString("[\"\"]")
- b.WriteByte('\n')
- case discordgo.MessageTypeGuildMemberJoin:
- b.WriteString("[#5865F2]")
- b.WriteString(m.Author.Username)
- b.WriteString("[-] joined the server.")
- b.WriteByte('\n')
- case discordgo.MessageTypeCall:
- b.WriteString("[#5865F2]")
- b.WriteString(m.Author.Username)
- b.WriteString("[-] started a call.")
- b.WriteByte('\n')
- case discordgo.MessageTypeChannelPinnedMessage:
- b.WriteString("[#5865F2]")
- b.WriteString(m.Author.Username)
- b.WriteString("[-] pinned a message.")
- b.WriteByte('\n')
- }
- if str := b.String(); str != "" {
- b := make([]byte, len(str)+1)
- copy(b, str)
- return b
- }
- return nil
- }
- func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message) {
- if rm != nil {
- b.WriteString(" ╭ ")
- b.WriteString("[::d]")
- buildAuthor(b, rm.Author)
- if rm.Content != "" {
- rm.Content = buildMentions(rm.Content, rm.Mentions)
- b.WriteString(parseMarkdown(rm.Content))
- }
- b.WriteString("[::-]")
- b.WriteByte('\n')
- }
- }
- func buildContent(b *strings.Builder, m *discordgo.Message) {
- if m.Content != "" {
- m.Content = buildMentions(m.Content, m.Mentions)
- b.WriteString(parseMarkdown(m.Content))
- }
- }
- func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
- for _, e := range es {
- if e.Type != discordgo.EmbedTypeRich {
- continue
- }
- var embedBuilder strings.Builder
- var hasHeading bool
- prefix := fmt.Sprintf("[#%06X]▐[-] ", e.Color)
- b.WriteByte('\n')
- embedBuilder.WriteString(prefix)
- if e.Author != nil {
- hasHeading = true
- embedBuilder.WriteString("[::u]")
- embedBuilder.WriteString(e.Author.Name)
- embedBuilder.WriteString("[::-]")
- }
- if e.Title != "" {
- hasHeading = true
- embedBuilder.WriteString("[::b]")
- embedBuilder.WriteString(e.Title)
- embedBuilder.WriteString("[::-]")
- }
- if e.Description != "" {
- if hasHeading {
- embedBuilder.WriteString("\n\n")
- }
- embedBuilder.WriteString(parseMarkdown(e.Description))
- }
- if len(e.Fields) != 0 {
- if hasHeading || e.Description != "" {
- embedBuilder.WriteString("\n\n")
- }
- for i, ef := range e.Fields {
- embedBuilder.WriteString("[::b]")
- embedBuilder.WriteString(ef.Name)
- embedBuilder.WriteString("[::-]")
- embedBuilder.WriteByte('\n')
- embedBuilder.WriteString(parseMarkdown(ef.Value))
- if i != len(e.Fields)-1 {
- embedBuilder.WriteString("\n\n")
- }
- }
- }
- if e.Footer != nil {
- if hasHeading {
- embedBuilder.WriteString("\n\n")
- }
- embedBuilder.WriteString(e.Footer.Text)
- }
- b.WriteString(strings.Replace(embedBuilder.String(), "\n", "\n"+prefix, -1))
- }
- }
- func buildAttachments(b *strings.Builder, as []*discordgo.MessageAttachment) {
- for _, a := range as {
- b.WriteByte('\n')
- b.WriteByte('[')
- b.WriteString(a.Filename)
- b.WriteString("]: ")
- b.WriteString(a.URL)
- }
- }
- func buildMentions(content string, mentions []*discordgo.User) string {
- for _, mUser := range mentions {
- var color string
- if mUser.ID == session.State.User.ID {
- color = "[:#5865F2]"
- } else {
- color = "[#EB459E]"
- }
- content = strings.NewReplacer(
- // <@USER_ID>
- "<@"+mUser.ID+">",
- color+"@"+mUser.Username+"[-:-]",
- // <@!USER_ID>
- "<@!"+mUser.ID+">",
- color+"@"+mUser.Username+"[-:-]",
- ).Replace(content)
- }
- return content
- }
- func buildAuthor(b *strings.Builder, u *discordgo.User) {
- if u.ID == session.State.User.ID {
- b.WriteString("[#57F287]")
- } else {
- b.WriteString("[#ED4245]")
- }
- b.WriteString(u.Username)
- b.WriteString("[-] ")
- // If the message author is a bot account, render the message with bot label
- // for distinction.
- if u.Bot {
- b.WriteString("[#EB459E]BOT[-] ")
- }
- }
- func parseMarkdown(md string) string {
- var res string
- res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
- res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
- res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
- res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
- return res
- }
- // channelIsUnread returns `true` if the given channel is marked as read by the client user, otherwise `false`.
- func channelIsUnread(s *discordgo.State, c *discordgo.Channel) bool {
- if c.LastMessageID == "" {
- return false
- }
- for _, rs := range s.ReadState {
- if c.ID == rs.ID {
- return c.LastMessageID != rs.LastMessageID
- }
- }
- return false
- }
- // findMessageByID returns the index and the `*Message` struct of the current message if the given message ID *mID* is equal to the current message ID. If the given message ID *mID* is not found in the given slice *ms*, `-1` and `nil` are returned instead.
- func findMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
- for i, m := range ms {
- if m.ID == mID {
- return i, m
- }
- }
- return -1, nil
- }
|