Skip to content

Commit

Permalink
Reworked extensions, Spotify
Browse files Browse the repository at this point in the history
- Reworked extension structure
- Added Spotify extension
- Added missing channel methods
  • Loading branch information
julxyz authored Apr 19, 2021
2 parents df5ec0f + e89a6f4 commit ef89584
Show file tree
Hide file tree
Showing 19 changed files with 866 additions and 160 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.5.1
golang.org/x/text v0.3.5 // indirect
github.com/stretchr/testify v1.7.0
github.com/zmb3/spotify v1.1.2
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/text v0.3.5
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand Down Expand Up @@ -324,11 +325,15 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
github.com/zmb3/spotify v1.1.2 h1:X/t7NUhhPuMqga4C2ZfoM3ZSaRanEInSroVst5Ztg2M=
github.com/zmb3/spotify v1.1.2/go.mod h1:GD7AAEMUJVYc2Z7p2a2S0E3/5f/KxM/vOnErNr4j+Tw=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
Expand Down Expand Up @@ -381,9 +386,12 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -445,6 +453,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down Expand Up @@ -478,6 +487,8 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
11 changes: 7 additions & 4 deletions launchcfg_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
"sharded": true,
"shards": 2,
"token": "",
"loglevel": "info",
"coordinator": "http://localhost:8080/api/"
"log_level": "info",
"coordinator": "http://localhost:8080/api/",
"spotify_client_id": "2ih14ihojk21hjko312kjh",
"spotify_client_secret": "3iuhj4i2huuih4oj32uhio423"
},
"clientcoordinator": {
"launch": true,
"port": "5000",
"dbc": "database, user, password, host, port",
"certtype": "letsencrypt",
"cert_type": "letsencrypt",
"domain": "example.domain"
},
"secret": ""
"secret": "somethingsomething",
"redact_sensitive": true
}
20 changes: 19 additions & 1 deletion src/peach_discord_client/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ type Message struct {
Flags int `json:"flags,omitempty"`
}

func (m Message) delete(c *Client) error {
func (m *Message) Delete(c *Client) error {
return c.DeleteMessage(m.ChannelID, m.ID)
}

func (m *Message) Edit(c *Client, args EditMessageArgs) (*Message, error) {
return c.EditMessage(m.ChannelID, m.ID, args)
}

// Attachment represents a Discord message's attachment
Expand Down Expand Up @@ -173,3 +176,18 @@ type EmbedField struct {
Value string `json:"value"`
Inline bool `json:"inline,omitempty"`
}

type AllowedMentions struct {
MentionTypes []MentionType `json:"parse"`
RoleIDs []string `json:"roles"`
UserIDs []string `json:"users"`
MentionRepliedUser bool `json:"replied_user"`
}

type MentionType string

const (
MentionRoles MentionType = "roles"
MentionUsers = "users"
MentionEveryone = "everyone"
)
12 changes: 10 additions & 2 deletions src/peach_discord_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Client struct {
Sequence *int64

// User
User User
User *User

// Heartbeat
HeartbeatInterval time.Duration // Interval in which client should sent heartbeats
Expand All @@ -76,6 +76,9 @@ type Client struct {
ChannelCache *cache.Cache
Settings map[string]cfgSettings // Map guildIDs to settings and cache that shit

// Extensions
Extensions Extensions

// Starttime
Starttime time.Time
}
Expand Down Expand Up @@ -121,11 +124,16 @@ func CCLogin(c *Client) error {
if err != nil {
return err
}
c.TOKEN = ClientCoordinator.Token
c.ShardCount = ClientCoordinator.TotalShards
c.ShardID = ClientCoordinator.ShardID
c.GatewayURL = ClientCoordinator.GatewayURL
c.CCHeartbeatInterval = ClientCoordinator.HeartbeatInterval
c.TOKEN = ClientCoordinator.Token

if redactSensitive {
ClientCoordinator.Token = "[REDACTED]"
}

c.Log.Debugf("Websocket: Received from client coordinator: %v", ClientCoordinator)
return nil
}
Expand Down
12 changes: 8 additions & 4 deletions src/peach_discord_client/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ var (
EndpointChannelPermission = func(channelID, overwriteID string) string {
return EndpointChannels + channelID + "/permissions/" + overwriteID
}
EndpointChannelInvites = func(channelID string) string { return EndpointChannels + channelID + "/invites" }
EndpointChannelTyping = func(channelID string) string { return EndpointChannels + channelID + "/typing" }
EndpointChannelMessages = func(channelID string) string { return EndpointChannels + channelID + "/messages" }
EndpointChannelMessage = func(channelID, messageID string) string {
EndpointChannelInvites = func(channelID string) string { return EndpointChannels + channelID + "/invites" }
EndpointChannelTyping = func(channelID string) string { return EndpointChannels + channelID + "/typing" }
EndpointChannelFollowers = func(channelID string) string { return EndpointChannels + channelID + "/followers" }
EndpointChannelMessages = func(channelID string) string { return EndpointChannels + channelID + "/messages" }
EndpointChannelMessage = func(channelID, messageID string) string {
return EndpointChannels + channelID + "/messages/" + messageID
}
EndpointChannelCrosspostMessage = func(channelID, messageID string) string {
return EndpointChannels + channelID + "/messages/" + messageID + "/crosspost"
}
EndpointChannelMessageAck = func(channelID, messageID string) string {
return EndpointChannels + channelID + "/messages/" + messageID + "/ack"
}
Expand Down
30 changes: 20 additions & 10 deletions src/peach_discord_client/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,34 @@ func (c *Client) onInviteDelete(ctx *EventInviteDelete) error {

func (c *Client) onMessageCreate(ctx *EventMessageCreate) error {

if ctx.Author.ID != c.User.ID {
c.Log.WithFields(logrus.Fields{
"author": ctx.Author.Username,
"message": ctx.Content,
"serverid": ctx.GuildID,
}).Debug("Websocket: received message")
}
c.Log.WithFields(logrus.Fields{
"author": ctx.Author.Username,
"message": ctx.Content,
"serverid": ctx.GuildID,
}).Debug("Websocket: received message")

// Do nothing for messages sent by applications
if ctx.WebhookID != "" || ctx.Author.Bot {
return nil
}

// Search for commands and execute if found
prefix := c.getSetting(ctx.GuildID, "bot", "prefix")
if strings.HasPrefix(ctx.Content, prefix) && len(ctx.Content) > 1 {
noPrefix := ctx.Content[1:]
invoke := strings.Fields(noPrefix)[0]
args := strings.Fields(noPrefix)[1:]
err := c.runOnMessage(invoke, args, ctx)
err := c.Extensions.runCommand(invoke, args, ctx)
if err != nil {
return fmt.Errorf("Couldn't execute command: %s", err)
}
}

err := c.Extensions.Spotify.OnMessage(ctx.Message)
if err != nil {
c.Log.Error(err)
}

return nil
}

Expand All @@ -160,6 +165,11 @@ func (c *Client) onMessageDeleteBulk(ctx *EventMessageDeleteBulk) error {
}

func (c *Client) onMessageReactionAdd(ctx *EventMessageReactionAdd) error {
if ctx.UserID == c.User.ID {
return nil
}

c.Extensions.Spotify.OnReact(ctx)
return nil
}

Expand All @@ -186,7 +196,7 @@ func (c *Client) onPresenceUpdate(ctx *EventPresenceUpdate) error {
func (c *Client) onReady(ctx *EventReady) error {

//Cache User object
c.User = ctx.User
c.User = &ctx.User

//Store session ID
c.SessionID = ctx.SessionID
Expand Down Expand Up @@ -221,7 +231,7 @@ func (c *Client) onTypingStart(ctx *EventTypingStart) error {

func (c *Client) onUserUpdate(ctx *EventUserUpdate) error {

c.User = ctx.User
c.User = &ctx.User

return nil
}
Expand Down
70 changes: 0 additions & 70 deletions src/peach_discord_client/ext_clear.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import (
"github.com/hako/durafmt"
)

func (c *Client) extAboutOnMessage(ctx *EventMessageCreate) error {
type extInternal struct {
Bot *Client
}

func (e *extInternal) Setup(bot *Client) {
e.Bot = bot
}

func (e *extInternal) About(ctx *EventMessageCreate) error {
m := NewMessage{
Embed: Embed{
Description: "Need help with something? Join the [support server](https://discord.gg/HfrjV3ybEs)!",
Expand All @@ -18,7 +26,7 @@ func (c *Client) extAboutOnMessage(ctx *EventMessageCreate) error {
},
{
Name: "Uptime",
Value: durafmt.Parse(time.Now().Sub(c.Starttime)).LimitFirstN(2).String(),
Value: durafmt.Parse(time.Now().Sub(e.Bot.Starttime)).LimitFirstN(2).String(),
Inline: true,
},
},
Expand All @@ -29,12 +37,12 @@ func (c *Client) extAboutOnMessage(ctx *EventMessageCreate) error {
},
},
}
_, err := c.SendMessage(ctx.ChannelID, m)
_, err := e.Bot.SendMessage(ctx.ChannelID, m)
if err != nil {
return err
}

err = ctx.delete(c)
err = ctx.Delete(e.Bot)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ef89584

Please sign in to comment.