Merge pull request #2 from rigormorrtiss/basic

Implement basic functionality
This commit is contained in:
rigormorrtiss 2021-07-20 12:08:27 +04:00 • committed by GitHub
commit e70b241527
15 changed files with 537 additions and 7 deletions

View file

@ -1,2 +1,32 @@
# discordo
Discord Terminal Client
Lightweight Discord terminal client
![preview](assets/preview.png)
## Features
- **Lightweight**: Designed to have a low memory footprint and low CPU usage, overall a low usage of system resources.
- **Secure**: Securely stores credentials (email, password, or authentication token) in OS-specific keyring file.
## Installation
- For Linux, run the following commands in chronological order.
```
git clone https://github.com/rigormorrtiss/discordo
cd discordo
mv build/discordo-os-arch /usr/local/bin
```
## Usage
- Run the executable by running the following command in a terminal.
```
discordo
```
- Choose the preferred login method.
- Log in using the chosen login method and click on "Login" button to continue.
Note: bot accounts must be prefixed with "Bot ".

BIN
assets/preview.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 KiB

View file

@ -1,7 +1,235 @@
package main
import "fmt"
import (
"strings"
"github.com/diamondburned/arikawa/v2/api"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/gateway"
"github.com/diamondburned/arikawa/v2/session"
"github.com/gdamore/tcell/v2"
"github.com/rigormorrtiss/discordo/ui"
"github.com/rigormorrtiss/discordo/util"
"github.com/rivo/tview"
)
var app *tview.Application
var loginModal *tview.Modal
var loginForm *tview.Form
var guildsDropDown *tview.DropDown
var channelsList *tview.List
var messagesTextView *tview.TextView
var messageInputField *tview.InputField
var mainFlex *tview.Flex
var loginVia string
var discordSession *session.Session
var guilds []gateway.GuildCreateEvent
var currentGuild gateway.GuildCreateEvent
var currentChannel discord.Channel
func main() {
fmt.Println("Discordo")
loginModal = ui.NewLoginModal(onLoginModalDone)
guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected)
channelsList = ui.NewChannelsList(onChannelsListSelected)
messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged)
mainFlex = ui.NewMainFlex(guildsDropDown, channelsList, messagesTextView)
app = ui.NewApplication(onApplicationInputCapture)
email := util.GetPassword("email")
password := util.GetPassword("password")
token := util.GetPassword("token")
if email != "" && password != "" {
app.
SetRoot(mainFlex, true).
SetFocus(guildsDropDown)
discordSession = newSession(email, password, "")
} else if token != "" {
app.
SetRoot(mainFlex, true).
SetFocus(guildsDropDown)
discordSession = newSession("", "", token)
} else {
app.SetRoot(loginModal, true)
}
if err := app.Run(); err != nil {
panic(err)
}
}
func onLoginFormQuitButtonSelected() {
app.Stop()
}
func onApplicationInputCapture(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlR {
app.Sync()
}
return event
}
func onMessagesTextViewChanged() {
app.Draw()
}
func onLoginModalDone(buttonIndex int, buttonLabel string) {
if buttonLabel == ui.LoginViaEmailPasswordLoginModalButton {
loginVia = "emailpassword"
loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
app.SetRoot(loginForm, true)
} else if buttonLabel == ui.LoginViaTokenLoginModalButton {
loginVia = "token"
loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
app.SetRoot(loginForm, true)
}
}
func newSession(email string, password string, token string) *session.Session {
var sess *session.Session
var err error
if email != "" && password != "" {
api.UserAgent = `Mozilla/5.0 (X11; Linux x86_64; rv:90.0)` +
`Gecko/20100101 Firefox/90.0`
gateway.DefaultIdentity = gateway.IdentifyProperties{
OS: "Linux",
Browser: "Firefox",
Device: "",
}
sess, err = session.Login(email, password, "")
if err != nil {
panic(err)
}
sess.AddHandler(onReady)
} else if token != "" {
sess, err = session.New(token)
if err != nil {
panic(err)
}
sess.AddHandler(onGuildCreate)
sess.Gateway.AddIntents(gateway.IntentGuilds)
sess.Gateway.AddIntents(gateway.IntentGuildMessages)
}
sess.AddHandler(onMessageCreate)
if err = sess.Open(); err != nil {
panic(err)
}
return sess
}
func onGuildCreate(guild *gateway.GuildCreateEvent) {
guildsDropDown.AddOption(guild.Name, nil)
guilds = append(guilds, *guild)
}
func onReady(ready *gateway.ReadyEvent) {
guilds = ready.Guilds
for i := 0; i < len(guilds); i++ {
guildsDropDown.AddOption(guilds[i].Name, nil)
}
}
func onMessageCreate(message *gateway.MessageCreateEvent) {
if currentChannel.ID == message.ChannelID {
util.WriteMessage(messagesTextView, message.Message)
}
}
func onGuildsDropDownSelected(text string, _ int) {
// Remove/clear all items from the channels List
channelsList.Clear()
// Remove/clear all text from the messages TextView buffer
messagesTextView.Clear()
// If the message InputField is not nil, remove the message InputField from the main Flex and set the message InputField to nil
if messageInputField != nil {
mainFlex.RemoveItem(messageInputField)
messageInputField = nil
}
for i := 0; i < len(guilds); i++ {
guild := guilds[i]
if guild.Name == text {
currentGuild = guild
break
}
}
for i := 0; i < len(currentGuild.Channels); i++ {
channel := currentGuild.Channels[i]
channelsList.AddItem(channel.Name, "", 0, nil)
}
app.SetFocus(channelsList)
}
func onChannelsListSelected(i int, mainText string, secondaryText string, _ rune) {
// Remove/clear all text from the messages TextView buffer
messagesTextView.Clear()
// If the message InputField is nil, add a new message InputField to the main Flex and assign it to message InputField in instance
if messageInputField == nil {
messageInputField = ui.NewMessageInputField(onMessageInputFieldDone)
// Add the message InputField as a new item to the main Flex
mainFlex.AddItem(messageInputField, 3, 1, false)
}
app.SetFocus(messageInputField)
currentChannel = currentGuild.Channels[i]
// Set the title of the messages TextView Box to the name of the channel
messagesTextView.SetTitle(currentChannel.Name)
messages := util.GetMessages(discordSession, currentChannel.ID, 50)
for i := len(messages) - 1; i >= 0; i-- {
util.WriteMessage(messagesTextView, messages[i])
}
}
func onMessageInputFieldDone(key tcell.Key) {
if key == tcell.KeyEnter {
currentText := messageInputField.GetText()
currentText = strings.TrimSpace(currentText)
// If the current text of the message InputField is an empty string and the enter key is pressed, do not proceed
if currentText == "" {
return
}
util.SendMessage(discordSession, currentChannel.ID, currentText)
// Set the current text of the message InputField to an empty string after the message has been sent
messageInputField.SetText("")
}
}
func onLoginFormLoginButtonSelected() {
if loginVia == "emailpassword" {
email := loginForm.GetFormItemByLabel("Email").(*tview.InputField).GetText()
password := loginForm.GetFormItemByLabel("Password").(*tview.InputField).GetText()
if email == "" || password == "" {
return
}
discordSession = newSession(email, password, "")
util.SetPassword("email", email)
util.SetPassword("password", password)
} else if loginVia == "token" {
token := loginForm.GetFormItemByLabel("Token").(*tview.InputField).GetText()
if token == "" {
return
}
discordSession = newSession("", "", token)
util.SetPassword("token", token)
}
app.
SetRoot(mainFlex, true).
SetFocus(guildsDropDown)
}

6
go.mod
View file

@ -3,6 +3,8 @@ module github.com/rigormorrtiss/discordo
go 1.16
require (
github.com/diamondburned/arikawa/v2 v2.1.0 // indirect
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 // indirect
github.com/diamondburned/arikawa/v2 v2.1.0
github.com/gdamore/tcell/v2 v2.3.11
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2
github.com/zalando/go-keyring v0.1.1
)

21
go.sum
View file

@ -1,9 +1,16 @@
github.com/danieljoos/wincred v1.1.0 h1:3RNcEpBg4IhIChZdFRSdlQt1QjCp1sMAPIrOnm7Yf8g=
github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7hqDjlFjiygg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/diamondburned/arikawa/v2 v2.1.0 h1:nyX5TEf7kuSdCTiZDMlURbabKrLTqPQGDBqnwX+qF9E=
github.com/diamondburned/arikawa/v2 v2.1.0/go.mod h1:e+lhS20ni2luFEU06Pc8paCxgZL99/RZb77dOC82CF0=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.3.3 h1:RKoI6OcqYrr/Do8yHZklecdGzDTJH9ACKdfECbRdw3M=
github.com/gdamore/tcell/v2 v2.3.3/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/gdamore/tcell/v2 v2.3.11 h1:ECO6WqHGbKZ3HrSL7bG/zArMCmLaNr5vcjjMVnLHpzc=
github.com/gdamore/tcell/v2 v2.3.11/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
@ -15,13 +22,20 @@ github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRR
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 h1:I5N0WNMgPSq5NKUFspB4jMJ6n2P0ipz5FlOlB4BXviQ=
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2/go.mod h1:IxQujbYMAh4trWr0Dwa8jfciForjVmxyHpskZX6aydQ=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/zalando/go-keyring v0.1.1 h1:w2V9lcx/Uj4l+dzAf1m9s+DJ1O8ROkEHnynonHjTcYE=
github.com/zalando/go-keyring v0.1.1/go.mod h1:OIC+OZ28XbmwFxU/Rp9V7eKzZjamBJwRzC8UFJH9+L8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -39,3 +53,6 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e h1:EHBhcS0mlXEAVwNyO2dLfjToGsyY4j24pTs2ScHnX7s=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

14
ui/app.go Normal file
View file

@ -0,0 +1,14 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func NewApplication(onApplicationInputCapture func(event *tcell.EventKey) *tcell.EventKey) *tview.Application {
app := tview.NewApplication().
EnableMouse(true).
SetInputCapture(onApplicationInputCapture)
return app
}

22
ui/dropdowns.go Normal file
View file

@ -0,0 +1,22 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var guildsDropDownBackgroundColor = tcell.GetColor("#1C1E26")
func NewGuildsDropDown(onGuildsDropDownSelected func(text string, index int)) *tview.DropDown {
guildsDropDown := tview.NewDropDown().
SetLabel("Guild: ").
SetSelectedFunc(onGuildsDropDownSelected)
guildsDropDown.
SetFieldBackgroundColor(guildsDropDownBackgroundColor).
SetBackgroundColor(guildsDropDownBackgroundColor).
SetBorder(true).
SetBorderPadding(0, 0, 1, 1).
SetTitle("Guilds")
return guildsDropDown
}

21
ui/flex.go Normal file
View file

@ -0,0 +1,21 @@
package ui
import (
"github.com/rivo/tview"
)
func NewMainFlex(guildsDropDown *tview.DropDown, channelsList *tview.List, messagesTextView *tview.TextView) *tview.Flex {
mainFlex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(guildsDropDown, 3, 1, false).
AddItem(
tview.NewFlex().
AddItem(channelsList, 20, 1, false).
AddItem(messagesTextView, 0, 3, false),
0,
1,
false,
)
return mainFlex
}

30
ui/forms.go Normal file
View file

@ -0,0 +1,30 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var loginFormBackgroundColor = tcell.GetColor("#1C1E26")
var loginFormButtonBackgroundColor = tcell.GetColor("#5865F2")
func NewLoginForm(via string, onLoginFormLoginButtonSelected func(), onLoginFormQuitButtonSelected func()) *tview.Form {
loginForm := tview.NewForm().
AddButton("Login", onLoginFormLoginButtonSelected).
AddButton("Quit", onLoginFormQuitButtonSelected)
loginForm.
SetButtonBackgroundColor(loginFormButtonBackgroundColor).
SetBackgroundColor(loginFormBackgroundColor).
SetBorder(true).
SetBorderPadding(15, 15, 15, 15)
if via == "token" {
loginForm.AddPasswordField("Token", "", 0, 0, nil)
} else if via == "emailpassword" {
loginForm.
AddInputField("Email", "", 0, nil, nil).
AddPasswordField("Password", "", 0, 0, nil)
}
return loginForm
}

24
ui/inputfields.go Normal file
View file

@ -0,0 +1,24 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var messageInputFieldBackgroundColor = tcell.GetColor("#1C1E26")
var messageInputFieldPlaceholderTextColor = tcell.ColorWhite
func NewMessageInputField(onMessageInputFieldDone func(key tcell.Key)) *tview.InputField {
messageInputField := tview.NewInputField().
SetPlaceholder("Message...").
SetFieldWidth(0).
SetDoneFunc(onMessageInputFieldDone)
messageInputField.
SetFieldBackgroundColor(messageInputFieldBackgroundColor).
SetPlaceholderTextColor(messageInputFieldPlaceholderTextColor).
SetBackgroundColor(messageInputFieldBackgroundColor).
SetBorder(true).
SetBorderPadding(0, 0, 1, 1)
return messageInputField
}

21
ui/lists.go Normal file
View file

@ -0,0 +1,21 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var channelsListBackgroundColor = tcell.GetColor("#1C1E26")
func NewChannelsList(onChannelsListSelected func(i int, mainText string, secondaryText string, _ rune)) *tview.List {
channelsList := tview.NewList().
ShowSecondaryText(false).
SetSelectedFunc(onChannelsListSelected)
channelsList.
SetBorder(true).
SetBorderPadding(0, 0, 1, 1).
SetTitle("Channels").
SetBackgroundColor(channelsListBackgroundColor)
return channelsList
}

18
ui/modals.go Normal file
View file

@ -0,0 +1,18 @@
package ui
import "github.com/rivo/tview"
var LoginViaTokenLoginModalButton = "Login via token"
var LoginViaEmailPasswordLoginModalButton = "Login via email and password"
func NewLoginModal(onLoginModalDone func(buttonIndex int, buttonLabel string)) *tview.Modal {
loginModal := tview.NewModal().
SetText("Choose a login method:").
AddButtons([]string{LoginViaTokenLoginModalButton, LoginViaEmailPasswordLoginModalButton}).
SetDoneFunc(onLoginModalDone)
loginModal.
SetBorder(true).
SetBorderPadding(0, 0, 1, 1)
return loginModal
}

24
ui/textviews.go Normal file
View file

@ -0,0 +1,24 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var messagesTextViewBackgroundColor = tcell.GetColor("#1C1E26")
func NewMessagesTextView(onMessagesTextViewChanged func()) *tview.TextView {
messagesTextView := tview.NewTextView().
SetDynamicColors(true).
SetWrap(true).
SetWordWrap(true).
SetScrollable(true).
ScrollToEnd().
SetChangedFunc(onMessagesTextViewChanged)
messagesTextView.
SetBorder(true).
SetBorderPadding(0, 0, 1, 1).
SetBackgroundColor(messagesTextViewBackgroundColor)
return messagesTextView
}

57
util/discord.go Normal file
View file

@ -0,0 +1,57 @@
package util
import (
"fmt"
_ "image/jpeg"
_ "image/png"
"strings"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/session"
"github.com/rivo/tview"
)
func WriteMessage(messagesTextView *tview.TextView, message discord.Message) {
var content strings.Builder
content.WriteString("[red::b]" + message.Author.Username + "[-:-:-] ")
// If the author of the message is a bot account, add "BOT" beside the username of the author.
if message.Author.Bot {
content.WriteString("[blue]BOT[-:-:-] ")
}
if message.Content != "" {
content.WriteString(message.Content)
}
// TODO(rigormorrtiss): display the message embed using "special" format
if len(message.Embeds) > 0 {
content.WriteString("\n<EMBED>")
}
attachments := message.Attachments
attachmentsLen := len(attachments)
if attachmentsLen > 0 {
for i := 0; i < attachmentsLen; i++ {
content.WriteString("\n" + attachments[i].URL)
}
}
fmt.Fprintln(messagesTextView, content.String())
}
func SendMessage(session *session.Session, channelID discord.ChannelID, content string) {
_, err := session.SendText(channelID, content)
if err != nil {
panic(err)
}
}
func GetMessages(session *session.Session, channelID discord.ChannelID, limit uint) []discord.Message {
messages, err := session.Messages(channelID, limit)
if err != nil {
panic(err)
}
return messages
}

22
util/keyring.go Normal file
View file

@ -0,0 +1,22 @@
package util
import (
"github.com/zalando/go-keyring"
)
const Service string = "discordo"
func GetPassword(user string) string {
password, err := keyring.Get(Service, user)
if err == keyring.ErrNotFound {
return ""
}
return password
}
func SetPassword(user string, password string) {
if err := keyring.Set(Service, user, password); err != nil {
panic(err)
}
}