discordo.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/atotto/clipboard"
  7. "github.com/gdamore/tcell/v2"
  8. "github.com/gen2brain/beeep"
  9. "github.com/rigormorrtiss/discordgo"
  10. "github.com/rigormorrtiss/discordo/ui"
  11. "github.com/rigormorrtiss/discordo/util"
  12. "github.com/rivo/tview"
  13. "github.com/zalando/go-keyring"
  14. )
  15. var (
  16. app *tview.Application
  17. loginForm *tview.Form
  18. guildsView *tview.TreeView
  19. messagesView *tview.TextView
  20. messageInputField *tview.InputField
  21. mainFlex *tview.Flex
  22. config *util.Config
  23. session *discordgo.Session
  24. selectedChannel *discordgo.Channel
  25. selectedMessage *discordgo.Message
  26. )
  27. func main() {
  28. config = util.NewConfig()
  29. tview.Styles = config.Theme
  30. app = tview.NewApplication()
  31. app.EnableMouse(config.Mouse)
  32. app.SetInputCapture(onAppInputCapture)
  33. guildsView = ui.NewGuildsView()
  34. guildsView.SetSelectedFunc(onGuildsTreeViewSelected)
  35. messagesView = ui.NewMessagesView(app)
  36. messagesView.SetInputCapture(onMessagesViewInputCapture)
  37. messageInputField = ui.NewMessageInputField()
  38. messageInputField.SetInputCapture(onMessageInputFieldInputCapture)
  39. mainFlex = ui.NewMainFlex(
  40. guildsView,
  41. messagesView,
  42. messageInputField,
  43. )
  44. token := config.Token
  45. if t, _ := keyring.Get("discordo", "token"); t != "" {
  46. token = t
  47. }
  48. if token != "" {
  49. app.
  50. SetRoot(mainFlex, true).
  51. SetFocus(guildsView)
  52. session = newSession()
  53. session.Token = token
  54. session.Identify.Token = token
  55. if err := session.Open(); err != nil {
  56. panic(err)
  57. }
  58. } else {
  59. loginForm = ui.NewLoginForm(onLoginFormLoginButtonSelected, false)
  60. app.SetRoot(loginForm, true)
  61. }
  62. if err := app.Run(); err != nil {
  63. panic(err)
  64. }
  65. }
  66. func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey {
  67. if e.Modifiers() == tcell.ModAlt {
  68. switch e.Rune() {
  69. case '1':
  70. app.SetFocus(guildsView)
  71. case '2':
  72. app.SetFocus(messagesView)
  73. case '3':
  74. app.SetFocus(messageInputField)
  75. }
  76. }
  77. return e
  78. }
  79. func findByMessageID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
  80. for i, m := range ms {
  81. if mID == m.ID {
  82. return i, m
  83. }
  84. }
  85. return -1, nil
  86. }
  87. func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
  88. if selectedChannel == nil {
  89. return nil
  90. }
  91. switch {
  92. case e.Key() == tcell.KeyUp || e.Rune() == 'k': // Up
  93. ms := selectedChannel.Messages
  94. hs := messagesView.GetHighlights()
  95. // If there are no currently highlighted message, highlight the last
  96. // message in the TextView.
  97. if len(hs) == 0 {
  98. messagesView.
  99. Highlight(ms[len(ms)-1].ID).
  100. ScrollToHighlight()
  101. } else {
  102. // Find the index of the currently highlighted message in the
  103. // *discordgo.Channel.Messages slice.
  104. idx, _ := findByMessageID(ms, hs[0])
  105. // If the index of the currently highlighted message is equal to
  106. // zero
  107. // (first message in the TextView), do not handle the event.
  108. if idx == -1 || idx == 0 {
  109. return nil
  110. }
  111. // Highlight the message just before the currently highlighted
  112. // message.
  113. messagesView.
  114. Highlight(ms[idx-1].ID).
  115. ScrollToHighlight()
  116. }
  117. return nil
  118. case e.Key() == tcell.KeyDown || e.Rune() == 'j': // Down
  119. ms := selectedChannel.Messages
  120. hs := messagesView.GetHighlights()
  121. // If there are no currently highlighted message, highlight the last
  122. // message in the TextView.
  123. if len(hs) == 0 {
  124. messagesView.
  125. Highlight(ms[len(ms)-1].ID).
  126. ScrollToHighlight()
  127. } else {
  128. // Find the index of the highlighted message in the
  129. // *discordgo.Channel.Messages slice.
  130. idx, _ := findByMessageID(ms, hs[0])
  131. // If the index of the currently highlighted message is equal to the
  132. // total number of elements in the *discordgo.Channel.Messages
  133. // slice, do not handle the event.
  134. if idx == -1 || idx == len(ms)-1 {
  135. return nil
  136. }
  137. // Highlight the message just after the currently highlighted
  138. // message.
  139. messagesView.
  140. Highlight(ms[idx+1].ID).
  141. ScrollToHighlight()
  142. }
  143. return nil
  144. case e.Key() == tcell.KeyHome || e.Rune() == 'g': // Top
  145. ms := selectedChannel.Messages
  146. // Highlight the last message in the selectedChannel.Messages slice
  147. // (the first message rendered in the TextView).
  148. messagesView.
  149. Highlight(ms[0].ID).
  150. ScrollToHighlight()
  151. case e.Key() == tcell.KeyEnd || e.Rune() == 'G': // Bottom
  152. ms := selectedChannel.Messages
  153. // Highlight the first message in the selectedChannel.Messages slice
  154. // (the last message rendered in the TextView).
  155. messagesView.
  156. Highlight(ms[len(ms)-1].ID).
  157. ScrollToHighlight()
  158. case e.Rune() == 'r': // Reply
  159. ms := selectedChannel.Messages
  160. hs := messagesView.GetHighlights()
  161. if len(hs) == 0 {
  162. return nil
  163. }
  164. _, selectedMessage = findByMessageID(ms, hs[0])
  165. messageInputField.SetTitle(
  166. "Replying to " + selectedMessage.Author.Username,
  167. )
  168. app.SetFocus(messageInputField)
  169. }
  170. return e
  171. }
  172. func onMessageInputFieldInputCapture(e *tcell.EventKey) *tcell.EventKey {
  173. // If the "Alt" modifier key is pressed, do not handle the event.
  174. if e.Modifiers() == tcell.ModAlt {
  175. return nil
  176. }
  177. switch e.Key() {
  178. case tcell.KeyEnter:
  179. if selectedChannel == nil {
  180. return nil
  181. }
  182. t := strings.TrimSpace(messageInputField.GetText())
  183. if t == "" {
  184. return nil
  185. }
  186. if selectedMessage != nil {
  187. messageInputField.SetTitle("")
  188. go session.ChannelMessageSendReply(
  189. selectedMessage.ChannelID,
  190. t,
  191. selectedMessage.Reference(),
  192. )
  193. selectedMessage = nil
  194. } else {
  195. go session.ChannelMessageSend(selectedChannel.ID, t)
  196. }
  197. messageInputField.SetText("")
  198. case tcell.KeyCtrlV:
  199. text, _ := clipboard.ReadAll()
  200. text = messageInputField.GetText() + text
  201. messageInputField.SetText(text)
  202. case tcell.KeyEscape: // Cancel
  203. messageInputField.SetTitle("")
  204. selectedMessage = nil
  205. }
  206. return e
  207. }
  208. func newSession() *discordgo.Session {
  209. s, err := discordgo.New()
  210. if err != nil {
  211. panic(err)
  212. }
  213. s.UserAgent = config.UserAgent
  214. s.Identify.Compress = false
  215. s.Identify.Intents = 0
  216. s.Identify.LargeThreshold = 0
  217. s.Identify.Properties.Device = ""
  218. s.Identify.Properties.Browser = "Chrome"
  219. s.Identify.Properties.OS = "Linux"
  220. s.AddHandlerOnce(onSessionReady)
  221. s.AddHandler(onSessionMessageCreate)
  222. return s
  223. }
  224. func onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
  225. sort.Slice(r.Guilds, func(a, b int) bool {
  226. found := false
  227. for _, gID := range r.Settings.GuildPositions {
  228. if found {
  229. if gID == r.Guilds[b].ID {
  230. return true
  231. }
  232. } else {
  233. if gID == r.Guilds[a].ID {
  234. found = true
  235. }
  236. }
  237. }
  238. return false
  239. })
  240. n := guildsView.GetRoot()
  241. for _, g := range r.Guilds {
  242. gn := tview.NewTreeNode(g.Name).
  243. SetReference(g.ID)
  244. n.AddChild(gn)
  245. }
  246. guildsView.SetCurrentNode(n)
  247. }
  248. func onSessionMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  249. if selectedChannel == nil {
  250. selectedChannel = &discordgo.Channel{ID: ""}
  251. }
  252. if selectedChannel.ID != m.ChannelID {
  253. if config.Notifications {
  254. for _, u := range m.Mentions {
  255. // If the client user account is mentioned in the message content, send a desktop notification with details.
  256. if u.ID == s.State.User.ID {
  257. g, err := s.State.Guild(m.GuildID)
  258. if err != nil {
  259. return
  260. }
  261. c, err := s.State.Channel(m.ChannelID)
  262. if err != nil {
  263. return
  264. }
  265. go beeep.Alert(fmt.Sprintf("%s (#%s)", g.Name, c.Name), m.ContentWithMentionsReplaced(), "")
  266. return
  267. }
  268. }
  269. }
  270. return
  271. }
  272. selectedChannel.Messages = append(selectedChannel.Messages, m.Message)
  273. util.WriteMessage(
  274. messagesView,
  275. m.Message,
  276. session.State.Ready.User.ID,
  277. )
  278. }
  279. func onGuildsTreeViewSelected(n *tview.TreeNode) {
  280. selectedChannel = nil
  281. selectedMessage = nil
  282. messagesView.
  283. Clear().
  284. SetTitle("")
  285. switch n.GetLevel() {
  286. case 1:
  287. if len(n.GetChildren()) != 0 {
  288. n.SetExpanded(!n.IsExpanded())
  289. return
  290. }
  291. n.ClearChildren()
  292. gID := n.GetReference().(string)
  293. g, _ := session.State.Guild(gID)
  294. cs := g.Channels
  295. sort.Slice(cs, func(i, j int) bool {
  296. return cs[i].Position < cs[j].Position
  297. })
  298. // Top-level channels
  299. ui.CreateTopLevelChannelsTreeNodes(session.State, n, cs)
  300. // Category channels
  301. ui.CreateCategoryChannelsTreeNodes(session.State, n, cs)
  302. // Second-level channels
  303. ui.CreateSecondLevelChannelsTreeNodes(session.State, guildsView, cs)
  304. default:
  305. cID := n.GetReference().(string)
  306. c, _ := session.State.Channel(cID)
  307. if c.Type == discordgo.ChannelTypeGuildCategory {
  308. n.SetExpanded(!n.IsExpanded())
  309. } else if c.Type == discordgo.ChannelTypeGuildNews || c.Type == discordgo.ChannelTypeGuildText {
  310. selectedChannel = c
  311. app.SetFocus(messageInputField)
  312. title := "#" + c.Name
  313. if c.Topic != "" {
  314. title += " - " + c.Topic
  315. }
  316. messagesView.
  317. Clear().
  318. SetTitle(title)
  319. go writeMessages(c.ID)
  320. }
  321. }
  322. }
  323. func writeMessages(cID string) {
  324. msgs, _ := session.ChannelMessages(cID, config.GetMessagesLimit, "", "", "")
  325. for i := len(msgs) - 1; i >= 0; i-- {
  326. selectedChannel.Messages = append(selectedChannel.Messages, msgs[i])
  327. util.WriteMessage(
  328. messagesView,
  329. msgs[i],
  330. session.State.Ready.User.ID,
  331. )
  332. }
  333. }
  334. func onLoginFormLoginButtonSelected() {
  335. email := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  336. password := loginForm.GetFormItem(1).(*tview.InputField).GetText()
  337. if email == "" || password == "" {
  338. return
  339. }
  340. session = newSession()
  341. // Try to login without TOTP
  342. lr, err := util.Login(session, email, password)
  343. if err != nil {
  344. panic(err)
  345. }
  346. if lr.Token != "" && !lr.MFA {
  347. app.
  348. SetRoot(mainFlex, true).
  349. SetFocus(guildsView)
  350. session.Token = lr.Token
  351. session.Identify.Token = lr.Token
  352. if err = session.Open(); err != nil {
  353. panic(err)
  354. }
  355. go keyring.Set("discordo", "token", lr.Token)
  356. } else if lr.MFA {
  357. loginForm = ui.NewLoginForm(func() {
  358. code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  359. if code == "" {
  360. return
  361. }
  362. lr, err = util.TOTP(session, code, lr.Ticket)
  363. if err != nil {
  364. panic(err)
  365. }
  366. app.
  367. SetRoot(mainFlex, true).
  368. SetFocus(guildsView)
  369. session.Token = lr.Token
  370. session.Identify.Token = lr.Token
  371. if err = session.Open(); err != nil {
  372. panic(err)
  373. }
  374. go keyring.Set("discordo", "token", lr.Token)
  375. }, true)
  376. app.SetRoot(loginForm, true)
  377. }
  378. }