Skip to content

Commit

Permalink
refactor: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ayn2op committed Dec 12, 2024
1 parent a098f47 commit 07d2df3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/guilds_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (gt *GuildsTree) createFolderNode(folder gateway.GuildFolder) {
for _, gID := range folder.GuildIDs {
g, err := discordState.Cabinet.Guild(gID)
if err != nil {
slog.Info("guild not found in state", "err", err, "guild", g)
slog.Info("failed to get guild from state", "guild_id", gID, "err", err)
continue
}

Expand Down
34 changes: 14 additions & 20 deletions cmd/message_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,24 @@ func (mi *MessageInput) send() {
return
}

data := api.SendMessageData{
Content: text,
}
if mi.replyMessageID != 0 {
data := api.SendMessageData{
Content: text,
Reference: &discord.MessageReference{MessageID: mi.replyMessageID},
AllowedMentions: &api.AllowedMentions{RepliedUser: option.False},
}
data.Reference = &discord.MessageReference{MessageID: mi.replyMessageID}
data.AllowedMentions = &api.AllowedMentions{RepliedUser: option.False}

if strings.HasPrefix(mi.GetTitle(), "[@]") {
data.AllowedMentions.RepliedUser = option.True
}

go func() {
if _, err := discordState.SendMessageComplex(layout.guildsTree.selectedChannelID, data); err != nil {
slog.Error("failed to send message", "err", err)
}
}()
} else {
go func() {
if _, err := discordState.SendMessage(layout.guildsTree.selectedChannelID, text); err != nil {
slog.Error("failed to send message", "err", err)
}
}()
}

go func() {
if _, err := discordState.SendMessageComplex(layout.guildsTree.selectedChannelID, data); err != nil {
slog.Error("failed to send message in channel", "channel_id", layout.guildsTree.selectedChannelID, "err", err)
}
}()

mi.replyMessageID = 0
mi.reset()

Expand All @@ -124,7 +118,7 @@ func (mi *MessageInput) editor() {

f, err := os.CreateTemp("", tmpFilePattern)
if err != nil {
slog.Error("failed to create temporary file", "err", err)
slog.Error("failed to create tmp file", "err", err)
return
}
_, _ = f.WriteString(mi.GetText())
Expand All @@ -140,14 +134,14 @@ func (mi *MessageInput) editor() {
mi.app.Suspend(func() {
err := cmd.Run()
if err != nil {
slog.Error("failed to run command", "err", err, "command", cmd)
slog.Error("failed to run command", "args", cmd.Args, "err", err)
return
}
})

msg, err := os.ReadFile(f.Name())
if err != nil {
slog.Error("failed to read temporary file", "err", err, "name", f.Name())
slog.Error("failed to read tmp file", "name", f.Name(), "err", err)
return
}

Expand Down
17 changes: 4 additions & 13 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,19 @@ import (
"github.com/ayn2op/discordo/internal/config"
)

// Recursively creates the log directory if it does not exist already and returns the path to the log file.
func initialize() (string, error) {
// Opens the log file and configures default logger.
func Load() error {
path, err := os.UserCacheDir()
if err != nil {
return "", err
return err
}

path = filepath.Join(path, config.Name)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return "", err
}

return filepath.Join(path, "logs.txt"), nil
}

// Opens the log file and configures standard logger.
func Load() error {
path, err := initialize()
if err != nil {
return err
}

path = filepath.Join(path, "logs.txt")
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
Expand Down

0 comments on commit 07d2df3

Please sign in to comment.