Rewrite to use Arikawa (#14)
This commit is contained in:
parent
8207701654
commit
b552619b4e
5 changed files with 86 additions and 66 deletions
117
discordo.go
117
discordo.go
|
|
@ -4,7 +4,12 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"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/diamondburned/arikawa/v2/state"
|
||||
"github.com/diamondburned/arikawa/v2/state/store/defaultstore"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rigormorrtiss/discordo/ui"
|
||||
"github.com/rigormorrtiss/discordo/util"
|
||||
|
|
@ -24,9 +29,10 @@ var (
|
|||
|
||||
loginVia string
|
||||
config *util.Config
|
||||
session *discordgo.Session
|
||||
currentGuild *discordgo.Guild
|
||||
currentChannel *discordgo.Channel
|
||||
discordSession *session.Session
|
||||
discordState *state.State
|
||||
currentGuild discord.Guild
|
||||
currentChannel discord.Channel
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -58,7 +64,7 @@ func main() {
|
|||
SetRoot(mainFlex, true).
|
||||
SetFocus(guildsDropDown)
|
||||
|
||||
session = newSession("", "", token)
|
||||
discordSession = newSession("", "", token)
|
||||
} else {
|
||||
app.SetRoot(loginModal, true)
|
||||
}
|
||||
|
|
@ -88,27 +94,27 @@ func onLoginModalDone(buttonIndex int, buttonLabel string) {
|
|||
}
|
||||
}
|
||||
|
||||
func newSession(email string, password string, token string) *discordgo.Session {
|
||||
var sess *discordgo.Session
|
||||
var err error
|
||||
if email != "" && password != "" {
|
||||
sess, err = discordgo.New(email, password)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else if token != "" {
|
||||
sess, err = discordgo.New(token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
sess.UserAgent = "" +
|
||||
func newSession(email string, password string, token string) *session.Session {
|
||||
api.UserAgent = "" +
|
||||
"Mozilla/5.0 (X11; Linux x86_64) " +
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) " +
|
||||
"Chrome/91.0.4472.164 Safari/537.36"
|
||||
sess.Identify.Properties.Browser = "Chrome"
|
||||
sess.Identify.Properties.OS = "Linux"
|
||||
gateway.DefaultIdentity.Browser = "Chrome"
|
||||
gateway.DefaultIdentity.OS = "Linux"
|
||||
|
||||
var sess *session.Session
|
||||
var err error
|
||||
if email != "" && password != "" {
|
||||
sess, err = session.Login(email, password, "")
|
||||
} else if token != "" {
|
||||
sess, err = session.New(token)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
discordState = state.NewFromSession(sess, defaultstore.New())
|
||||
|
||||
sess.AddHandler(onReady)
|
||||
sess.AddHandler(onGuildCreate)
|
||||
|
|
@ -120,23 +126,23 @@ func newSession(email string, password string, token string) *discordgo.Session
|
|||
return sess
|
||||
}
|
||||
|
||||
func onGuildCreate(_ *discordgo.Session, guild *discordgo.GuildCreate) {
|
||||
func onGuildCreate(guild *gateway.GuildCreateEvent) {
|
||||
guildsDropDown.AddOption(guild.Name, nil)
|
||||
}
|
||||
|
||||
func onReady(_ *discordgo.Session, ready *discordgo.Ready) {
|
||||
func onReady(ready *gateway.ReadyEvent) {
|
||||
for i := range ready.Guilds {
|
||||
guildsDropDown.AddOption(ready.Guilds[i].Name, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func onMessageCreate(_ *discordgo.Session, message *discordgo.MessageCreate) {
|
||||
if currentChannel != nil && currentChannel.ID == message.ChannelID {
|
||||
util.WriteMessage(messagesTextView, session, message.Message)
|
||||
func onMessageCreate(message *gateway.MessageCreateEvent) {
|
||||
if currentChannel.ID == message.ChannelID {
|
||||
util.WriteMessage(messagesTextView, discordState, message.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func onGuildsDropDownSelected(text string, _ int) {
|
||||
func onGuildsDropDownSelected(_ string, i int) {
|
||||
channelsTreeNode.ClearChildren()
|
||||
messagesTextView.Clear()
|
||||
|
||||
|
|
@ -145,19 +151,17 @@ func onGuildsDropDownSelected(text string, _ int) {
|
|||
messageInputField = nil
|
||||
}
|
||||
|
||||
guilds := session.State.Guilds
|
||||
for i := range guilds {
|
||||
guild := guilds[i]
|
||||
if guild.Name == text {
|
||||
currentGuild = guild
|
||||
break
|
||||
}
|
||||
}
|
||||
guilds := discordState.Ready().Guilds
|
||||
currentGuild = guilds[i].Guild
|
||||
|
||||
channelsTreeView.SetTitle("Channels")
|
||||
app.SetFocus(channelsTreeView)
|
||||
|
||||
channels := currentGuild.Channels
|
||||
channels, err := discordState.Cabinet.Channels(currentGuild.ID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sort.Slice(channels, func(i, j int) bool {
|
||||
return channels[i].Position < channels[j].Position
|
||||
})
|
||||
|
|
@ -166,13 +170,12 @@ func onGuildsDropDownSelected(text string, _ int) {
|
|||
channel := channels[i]
|
||||
channelNode := tview.NewTreeNode(channel.Name).
|
||||
SetReference(channel)
|
||||
|
||||
switch channel.Type {
|
||||
case discordgo.ChannelTypeGuildCategory:
|
||||
case discord.GuildCategory:
|
||||
channelNode.SetColor(tcell.GetColor(config.Theme.TreeNodeForeground))
|
||||
channelsTreeNode.AddChild(channelNode)
|
||||
case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
|
||||
if channel.ParentID == "" {
|
||||
case discord.GuildText, discord.GuildNews:
|
||||
if channel.CategoryID == 0 {
|
||||
channelNode.SetText("[::d]#" + channel.Name + "[-:-:-]")
|
||||
channelsTreeNode.AddChild(channelNode)
|
||||
}
|
||||
|
|
@ -188,13 +191,19 @@ func onChannelsTreeViewSelected(node *tview.TreeNode) {
|
|||
mainFlex.AddItem(messageInputField, 3, 1, false)
|
||||
}
|
||||
|
||||
currentChannel = node.GetReference().(*discordgo.Channel)
|
||||
currentChannel = node.GetReference().(discord.Channel)
|
||||
|
||||
switch currentChannel.Type {
|
||||
case discordgo.ChannelTypeGuildCategory:
|
||||
case discord.GuildCategory:
|
||||
if len(node.GetChildren()) == 0 {
|
||||
for i := range currentGuild.Channels {
|
||||
channel := currentGuild.Channels[i]
|
||||
if (channel.Type == discordgo.ChannelTypeGuildText || channel.Type == discordgo.ChannelTypeGuildNews) && channel.ParentID == currentChannel.ID {
|
||||
channels, err := discordState.Cabinet.Channels(currentGuild.ID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := range channels {
|
||||
channel := channels[i]
|
||||
if (channel.Type == discord.GuildText || channel.Type == discord.GuildNews) && channel.CategoryID == currentChannel.ID {
|
||||
channelNode := tview.NewTreeNode("[::d]#" + channel.Name + "[-:-:-]").
|
||||
SetReference(channel)
|
||||
node.AddChild(channelNode)
|
||||
|
|
@ -203,17 +212,17 @@ func onChannelsTreeViewSelected(node *tview.TreeNode) {
|
|||
} else {
|
||||
node.SetExpanded(!node.IsExpanded())
|
||||
}
|
||||
case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
|
||||
case discord.GuildText, discord.GuildNews:
|
||||
messagesTextView.SetTitle(currentChannel.Name)
|
||||
app.SetFocus(messageInputField)
|
||||
|
||||
messages, err := session.ChannelMessages(currentChannel.ID, config.GetMessagesLimit, "", "", "")
|
||||
messages, err := discordSession.Messages(currentChannel.ID, config.GetMessagesLimit)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
util.WriteMessage(messagesTextView, session, messages[i])
|
||||
util.WriteMessage(messagesTextView, discordState, messages[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -227,7 +236,7 @@ func onMessageInputFieldDone(key tcell.Key) {
|
|||
return
|
||||
}
|
||||
|
||||
_, err := session.ChannelMessageSend(currentChannel.ID, currentText)
|
||||
_, err := discordSession.SendText(currentChannel.ID, currentText)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
@ -244,15 +253,15 @@ func onLoginFormLoginButtonSelected() {
|
|||
return
|
||||
}
|
||||
|
||||
session = newSession(email, password, "")
|
||||
util.SetPassword("token", session.Token)
|
||||
discordSession = newSession(email, password, "")
|
||||
util.SetPassword("token", discordSession.Token)
|
||||
} else if loginVia == "token" {
|
||||
token := loginForm.GetFormItemByLabel("Token").(*tview.InputField).GetText()
|
||||
if token == "" {
|
||||
return
|
||||
}
|
||||
|
||||
session = newSession("", "", token)
|
||||
discordSession = newSession("", "", token)
|
||||
util.SetPassword("token", token)
|
||||
}
|
||||
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -3,7 +3,7 @@ module github.com/rigormorrtiss/discordo
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.23.2
|
||||
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
|
||||
|
|
|
|||
22
go.sum
22
go.sum
|
|
@ -1,9 +1,9 @@
|
|||
github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4=
|
||||
github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
|
||||
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/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
|
||||
|
|
@ -11,13 +11,17 @@ github.com/gdamore/tcell/v2 v2.3.11 h1:ECO6WqHGbKZ3HrSL7bG/zArMCmLaNr5vcjjMVnLHp
|
|||
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/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
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=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
|
||||
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=
|
||||
|
|
@ -31,8 +35,12 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
|
|||
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-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
|
||||
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
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=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
|
||||
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
|
@ -42,6 +50,8 @@ golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9sn
|
|||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
|
||||
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=
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ type Theme struct {
|
|||
}
|
||||
|
||||
type Config struct {
|
||||
GetMessagesLimit int `json:"getMessagesLimit"`
|
||||
GetMessagesLimit uint `json:"getMessagesLimit"`
|
||||
Theme *Theme `json:"theme"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,15 @@ import (
|
|||
_ "image/png"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/diamondburned/arikawa/v2/discord"
|
||||
"github.com/diamondburned/arikawa/v2/state"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func WriteMessage(messagesTextView *tview.TextView, session *discordgo.Session, message *discordgo.Message) {
|
||||
func WriteMessage(messagesTextView *tview.TextView, state *state.State, message discord.Message) {
|
||||
var content strings.Builder
|
||||
|
||||
if session.State.User.ID == message.Author.ID {
|
||||
if state.Ready().User.ID == message.Author.ID {
|
||||
content.WriteString("[#ffb86c::b]")
|
||||
content.WriteString(message.Author.Username)
|
||||
content.WriteString("[-:-:-] ")
|
||||
|
|
@ -32,7 +33,7 @@ func WriteMessage(messagesTextView *tview.TextView, session *discordgo.Session,
|
|||
content.WriteString(message.Content)
|
||||
}
|
||||
|
||||
if message.EditedTimestamp != "" {
|
||||
if message.EditedTimestamp.IsValid() {
|
||||
content.WriteString(" [::d](edited)[-:-:-]")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue