Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jukeizu/treediagram
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.24.0
Choose a base ref
...
head repository: jukeizu/treediagram
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 6 commits
  • 5 files changed
  • 1 contributor

Commits on Nov 12, 2022

  1. Verified

    This commit was signed with the committer’s verified signature.
    shawntoffel Shawn Toffel
    Copy the full SHA
    7c365ea View commit details
  2. Merge pull request #83 from jukeizu/slashcommand

    Added default /version slash command
    shawntoffel authored Nov 12, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    413a7eb View commit details

Commits on Apr 23, 2023

  1. Verified

    This commit was signed with the committer’s verified signature.
    shawntoffel Shawn Toffel
    Copy the full SHA
    fee210a View commit details
  2. Merge pull request #84 from jukeizu/log-levels

    Bump log level to info for interaction logs
    shawntoffel authored Apr 23, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8682acc View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    shawntoffel Shawn Toffel
    Copy the full SHA
    6b34d54 View commit details
  4. Merge pull request #85 from jukeizu/log-enhancements

    Log level & property enhancements
    shawntoffel authored Apr 23, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    23ccf28 View commit details
Showing with 122 additions and 31 deletions.
  1. +110 −9 pkg/bot/discord/bot.go
  2. +1 −6 pkg/processor/command.go
  3. +1 −6 pkg/processor/interaction.go
  4. +9 −9 pkg/processor/processor.go
  5. +1 −1 pkg/user/logging.go
119 changes: 110 additions & 9 deletions pkg/bot/discord/bot.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/jukeizu/contract"
"github.com/jukeizu/treediagram/api/protobuf-spec/processingpb"
"github.com/jukeizu/treediagram/internal"
nats "github.com/nats-io/nats.go"
"github.com/rs/zerolog"
)
@@ -74,7 +75,12 @@ func NewBot(token string, client processingpb.ProcessingClient, queue *nats.Enco
func (d *bot) Open() error {
d.Logger.Info().Msg("session opening")

return d.Session.Open()
err := d.Session.Open()
if err != nil {
return err
}

return d.registerApplicationCommands()
}

func (d *bot) Close() {
@@ -83,6 +89,21 @@ func (d *bot) Close() {
d.Session.Close()
}

func (d *bot) registerApplicationCommands() error {
_, err := d.Session.ApplicationCommandBulkOverwrite(d.Session.State.User.ID, "", d.defaultGlobalCommands())
return err
}

func (d *bot) defaultGlobalCommands() []*discordgo.ApplicationCommand {
version := discordgo.ApplicationCommand{
Name: "version",
Description: "Returns the current bot version",
Version: internal.Version,
}

return []*discordgo.ApplicationCommand{&version}
}

func (d *bot) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// ignore messages from bots
if m.Author.Bot {
@@ -107,14 +128,23 @@ func (d *bot) interactionCreate(s *discordgo.Session, i *discordgo.InteractionCr
return
}

if i.Type != discordgo.InteractionMessageComponent {
user := d.getInteractionUser(i)

if i.Type == discordgo.InteractionApplicationCommand {
d.handleApplicationCommand(s, i)
return
}

d.Logger.Debug().
d.Logger.Info().
Str("requestId", i.ID).
Str("type", i.Type.String()).
Interface("user", user).
Msg("received interaction create")

if i.Type != discordgo.InteractionMessageComponent {
return
}

pbInteraction := mapToPbInteraction(s.State, i)

_, err := d.Client.SendInteraction(context.Background(), pbInteraction)
@@ -126,8 +156,9 @@ func (d *bot) interactionCreate(s *discordgo.Session, i *discordgo.InteractionCr

customId := i.MessageComponentData().CustomID

d.Logger.Debug().
d.Logger.Info().
Str("customId", customId).
Interface("user", user).
Msg("interaction request sent")

err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
@@ -139,18 +170,37 @@ func (d *bot) interactionCreate(s *discordgo.Session, i *discordgo.InteractionCr
return
}

d.Logger.Debug().
d.Logger.Info().
Str("customId", customId).
Interface("user", user).
Msg("responded to discord interaction")
}

func (d *bot) handleApplicationCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
data := i.ApplicationCommandData()
user := d.getInteractionUser(i)

d.Logger.Info().
Str("requestId", i.ID).
Interface("user", user).
Interface("data", data).
Str("serverId", i.GuildID).
Str("channelId", i.ChannelID).
Msg("received application command")

switch data.Name {
case "version":
d.sendStringCommandResponse(internal.Version, s, i)
}
}

func (d *bot) messageReactionAdd(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
message, err := d.Session.State.Message(r.ChannelID, r.MessageID)
if err == discordgo.ErrStateNotFound {
d.Logger.Info().
d.Logger.Debug().
Str("messageID", r.MessageID).
Str("channelID", r.ChannelID).
Msg("lookup up message from discord api")
Msg("looking up reaction message from discord api")

message, err = d.Session.ChannelMessage(r.ChannelID, r.MessageID)
}
@@ -380,7 +430,7 @@ func (d *bot) handleRestError(messageReply *processingpb.MessageReply, responseE
return
}

d.Logger.Debug().
d.Logger.Info().
Err(responseError).
Str("processingRequestId", messageReply.ProcessingRequestId).
Str("messageReplyId", messageReply.Id).
@@ -390,7 +440,7 @@ func (d *bot) handleRestError(messageReply *processingpb.MessageReply, responseE

helpMessage := mapRestErrorToHelpMessage(restError, messageReply)
if helpMessage == "" {
d.Logger.Debug().
d.Logger.Info().
Err(responseError).
Str("processingRequestId", messageReply.ProcessingRequestId).
Str("messageReplyId", messageReply.Id).
@@ -429,3 +479,54 @@ func (d *bot) discordLogger(dgoLevel int, caller int, format string, a ...interf
Str("version", discordgo.VERSION).
Msg(message)
}

func (d *bot) sendStringCommandResponse(content string, s *discordgo.Session, i *discordgo.InteractionCreate) {
d.sendCommandResponse(&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
},
}, s, i)
}

func (d *bot) sendCommandResponse(response *discordgo.InteractionResponse, s *discordgo.Session, i *discordgo.InteractionCreate) {
data := i.ApplicationCommandData()
user := d.getInteractionUser(i)

d.Logger.Debug().
Interface("data", data).
Str("requestId", i.ID).
Interface("user", user).
Str("serverId", i.GuildID).
Str("channelId", i.ChannelID).
Msg("sending interaction response")

err := s.InteractionRespond(i.Interaction, response)
if err != nil {
d.Logger.Error().Caller().
Err(err).
Interface("data", data).
Str("requestId", i.ID).
Interface("user", user).
Str("serverId", i.GuildID).
Str("channelId", i.ChannelID).
Msg("failed to send interaction response")
return
}

d.Logger.Info().
Interface("data", data).
Str("requestId", i.ID).
Interface("user", user).
Str("serverId", i.GuildID).
Str("channelId", i.ChannelID).
Msg("interaction response sent")
}

func (d *bot) getInteractionUser(i *discordgo.InteractionCreate) *discordgo.User {
if i.Member != nil && i.Member.User != nil {
return i.Member.User
}

return i.User
}
7 changes: 1 addition & 6 deletions pkg/processor/command.go
Original file line number Diff line number Diff line change
@@ -72,12 +72,7 @@ func (c Command) MarshalZerologObject(e *zerolog.Event) {
e.Str("type", "command").
Str("intentId", c.Intent.Id).
Str("intentName", c.Intent.Name).
Str("source", c.Request.Source).
Str("channelId", c.Request.ChannelId).
Str("serverId", c.Request.ServerId).
Str("botId", c.Request.Bot.Id).
Str("userId", c.Request.Author.Id).
Bool("isDirect", c.Request.IsDirect)
Interface("request", c.Request)
}

func (c Command) isBotMentioned() bool {
7 changes: 1 addition & 6 deletions pkg/processor/interaction.go
Original file line number Diff line number Diff line change
@@ -69,10 +69,5 @@ func (c Interaction) MarshalZerologObject(e *zerolog.Event) {
e.Str("type", "interaction").
Str("intentId", c.Intent.Id).
Str("intentName", c.Intent.Name).
Str("source", c.Request.Source).
Str("channelId", c.Request.ChannelId).
Str("serverId", c.Request.ServerId).
Str("botId", c.Request.Bot.Id).
Str("userId", c.Request.User.Id).
Str("identifier", c.Request.Identifier)
Interface("request", c.Request)
}
18 changes: 9 additions & 9 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ func (p Processor) processJob(schedulingJob *schedulingpb.Job) {
}

func (p Processor) processReaction(reaction *processingpb.Reaction) {
p.logger.Info().
p.logger.Debug().
Interface("reaction", reaction).
Msg("reaction received")

@@ -208,7 +208,7 @@ func (p Processor) processReaction(reaction *processingpb.Reaction) {
}

func (p Processor) processInteraction(interaction *processingpb.Interaction) {
p.logger.Debug().
p.logger.Info().
Interface("interaction", interaction).
Msg("interaction received")

@@ -298,7 +298,7 @@ func (p Processor) process(executable Executable) {
return
}

p.logger.Info().
p.logger.Debug().
Str("processingRequestId", processingRequest.Id).
EmbedObject(executable).
Msg("saving responses from execute")
@@ -307,12 +307,12 @@ func (p Processor) process(executable Executable) {
p.saveResponseMessage(processingRequest, message)
}

p.logger.Info().
p.logger.Debug().
Str("processingRequestId", processingRequest.Id).
EmbedObject(executable).
Msg("finished saving responses from execute")

p.logger.Info().
p.logger.Debug().
Str("processingRequestId", processingRequest.Id).
EmbedObject(executable).
Msg("scheduling jobs")
@@ -321,7 +321,7 @@ func (p Processor) process(executable Executable) {
p.scheduleJobs(processingRequest, message)
}

p.logger.Info().
p.logger.Debug().
Str("processingRequestId", processingRequest.Id).
EmbedObject(executable).
Msg("finished scheduling jobs")
@@ -338,7 +338,7 @@ func (p Processor) findServerId(serverId string, userId string) (string, error)
return serverId, nil
}

p.logger.Info().
p.logger.Debug().
Str("userId", userId).
Msg("looking up server preference")

@@ -349,14 +349,14 @@ func (p Processor) findServerId(serverId string, userId string) (string, error)

preference := preferenceReply.Preference
if preference == nil {
p.logger.Info().
p.logger.Debug().
Str("userId", userId).
Msg("user does not have a preferred server")

return "", nil
}

p.logger.Info().
p.logger.Debug().
Str("userId", userId).
Str("preferredServerId", preference.ServerId).
Msg("found server preference for user")
2 changes: 1 addition & 1 deletion pkg/user/logging.go
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ func (s *loggingService) Preference(ctx context.Context, req *userpb.PreferenceR
return
}

l.Info().Msg("")
l.Debug().Msg("")
}(time.Now())

reply, err = s.Service.Preference(ctx, req)