Skip to content

Commit

Permalink
Merge pull request #81 from dwarvesf/feat/cmd-ogif
Browse files Browse the repository at this point in the history
feat: cmd ogif
  • Loading branch information
baenv authored Aug 30, 2024
2 parents a59e97e + afd7cb3 commit e9692f8
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 50 deletions.
30 changes: 30 additions & 0 deletions pkg/adapter/fortress/fortress.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/gocolly/colly"

Expand Down Expand Up @@ -1044,3 +1045,32 @@ func (f *Fortress) FetchNews(platform, topic string) ([]model.News, error) {

return news.Data, nil
}

func (f *Fortress) GetOgifStats(discordID string, after time.Time) (*model.OgifStatsResponse, error) {
req, err := f.makeReq("/api/v1/ogif", http.MethodGet, nil)
if err != nil {
return nil, err
}

q := req.URL.Query()
q.Add("discordID", discordID)
q.Add("after", after.Format(time.RFC3339))
req.URL.RawQuery = q.Encode()

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
}

var ogifStats model.OgifStatsResponse
if err := json.NewDecoder(resp.Body).Decode(&ogifStats); err != nil {
return nil, fmt.Errorf("invalid decoded, error %v", err.Error())
}

return &ogifStats, nil
}
1 change: 1 addition & 0 deletions pkg/adapter/fortress/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ type FortressAdapter interface {
GetDiscordResearchTopics(timeRange string) (data *model.DiscordResearchTopicResponse, err error)

FetchNews(platform, topic string) ([]model.News, error)
GetOgifStats(discordID string, after time.Time) (*model.OgifStatsResponse, error)
}
2 changes: 2 additions & 0 deletions pkg/discord/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/discord/command/mma"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/new"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/news"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/ogif"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/profile"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/radar"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/staff"
Expand Down Expand Up @@ -87,6 +88,7 @@ func New(cfg *config.Config, l logger.Logger, svc service.Servicer, view view.Vi
withdraw.New(l, svc, view),
news.New(l, svc, view, cfg),
topic.New(l, svc, view, cfg),
ogif.New(l, svc, view, cfg),
})

return cmd
Expand Down
49 changes: 1 addition & 48 deletions pkg/discord/command/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package memo

import (
"errors"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -65,7 +63,7 @@ func (e *Memo) ListMemoLogs(message *model.DiscordMessage) error {
tempFrom *time.Time
err error
)
tempFrom, timeAmount, timeUnit, err = parseAndCalculateFromDate(now, durationStr)
tempFrom, timeAmount, timeUnit, err = stringutils.ParseTimePeriod(now, durationStr)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,51 +95,6 @@ func (e *Memo) ListMemoOpenPullRequest(message *model.DiscordMessage) error {
return e.view.Memo().ListMemoOpenPullRequest(message, *data)
}

// parseAndCalculateFromDate parses the duration argument and calculates the 'from' date
func parseAndCalculateFromDate(now time.Time, arg string) (*time.Time, int, string, error) {
re := regexp.MustCompile(`(?i)^(\d+)\s*([a-z]+)$`)
matches := re.FindStringSubmatch(arg)
if len(matches) != 3 {
return nil, 0, "", errors.New("invalid duration argument format")
}

num, err := strconv.Atoi(matches[1])
if err != nil {
return nil, 0, "", errors.New("invalid number in duration argument")
}

unit := matches[2]
var from time.Time
switch strings.ToLower(unit) {
case "d", "day", "days":
from = now.AddDate(0, 0, -num)
if num > 1 {
return &from, num, "days", nil
}
return &from, num, "day", nil
case "w", "week", "weeks":
from = now.AddDate(0, 0, -7*num)
if num > 1 {
return &from, num, "weeks", nil
}
return &from, num, "week", nil
case "m", "month", "months":
from = now.AddDate(0, -num, 0)
if num > 1 {
return &from, num, "months", nil
}
return &from, num, "month", nil
case "y", "year", "years":
from = now.AddDate(-num, 0, 0)
if num > 1 {
return &from, num, "years", nil
}
return &from, num, "year", nil
default:
return nil, 0, "", errors.New("invalid time duration unit")
}
}

func (e *Memo) Sync(message *model.DiscordMessage) error {
targetChannelID := constant.DiscordReadingChannel
if e.cfg.Env == "dev" {
Expand Down
66 changes: 66 additions & 0 deletions pkg/discord/command/ogif/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ogif

import (
"strings"
"time"

"github.com/dwarvesf/fortress-discord/pkg/model"
"github.com/dwarvesf/fortress-discord/pkg/utils/stringutils"
)

func (c command) Prefix() []string {
return []string{"ogif"}
}

// Execute is where we handle logic for each command
func (c command) Execute(message *model.DiscordMessage) error {
now := time.Now()
userID := message.Author.ID
timePeriod := "30d"
isUserOmitted := false

if len(message.ContentArgs) > 1 {
if message.ContentArgs[1] == "help" {
return c.Help(message)
}

extractedID := stringutils.ExtractDiscordID(message.ContentArgs[1])
if extractedID != "" {
userID = extractedID
} else {
isUserOmitted = true
timePeriod = strings.Join(message.ContentArgs[1:], "")
}
}

if len(message.ContentArgs) > 2 && !isUserOmitted {
timePeriod = strings.Join(message.ContentArgs[2:], "")
}

after, timeAmount, timeUnit, err := stringutils.ParseTimePeriod(now, timePeriod)
if err != nil {
return err
}

return c.FetchOgifStats(message, userID, *after, timeAmount, timeUnit)
}

func (c command) Name() string {
return "OGIF command"
}

func (c command) Help(message *model.DiscordMessage) error {
return c.view.Ogif().Help(message)
}

func (c command) DefaultCommand(message *model.DiscordMessage) error {
return nil
}

func (c command) PermissionCheck(message *model.DiscordMessage) (bool, []string) {
return true, []string{}
}

func (c command) ChannelPermissionCheck(message *model.DiscordMessage) bool {
return true
}
9 changes: 9 additions & 0 deletions pkg/discord/command/ogif/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ogif

import (
"github.com/dwarvesf/fortress-discord/pkg/discord/base"
)

type Commander interface {
base.TextCommander
}
24 changes: 24 additions & 0 deletions pkg/discord/command/ogif/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ogif

import (
"github.com/dwarvesf/fortress-discord/pkg/config"
"github.com/dwarvesf/fortress-discord/pkg/discord/service"
"github.com/dwarvesf/fortress-discord/pkg/discord/view"
"github.com/dwarvesf/fortress-discord/pkg/logger"
)

type command struct {
L logger.Logger
svc service.Servicer
view view.Viewer
cfg *config.Config
}

func New(l logger.Logger, svc service.Servicer, view view.Viewer, cfg *config.Config) Commander {
return &command{
L: l,
svc: svc,
view: view,
cfg: cfg,
}
}
18 changes: 18 additions & 0 deletions pkg/discord/command/ogif/ogif.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ogif

import (
"time"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (c command) FetchOgifStats(msg *model.DiscordMessage, discordID string, after time.Time, timeAmount int, timeUnit string) error {
logger := c.L.AddField("discordID", discordID).AddField("after", after)
stats, err := c.svc.Event().GetOgifStats(discordID, after)
if err != nil {
logger.Error(err, "error when get ogif stats")
return err
}

return c.view.Ogif().RenderOgifStats(msg, discordID, stats, timeAmount, timeUnit)
}
9 changes: 9 additions & 0 deletions pkg/discord/service/event/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/bwmarrin/discordgo"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/base"
Expand Down Expand Up @@ -107,3 +108,11 @@ func extractSpeakers(args []string) map[string][]string {

return mapSpeakersByTopic
}

func (e *Event) GetOgifStats(discordID string, after time.Time) (model.OgifStats, error) {
resp, err := e.adapter.Fortress().GetOgifStats(discordID, after)
if err != nil {
return model.OgifStats{}, err
}
return resp.Data, err
}
7 changes: 6 additions & 1 deletion pkg/discord/service/event/interface.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package event

import "github.com/dwarvesf/fortress-discord/pkg/model"
import (
"time"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

type EventServicer interface {
GetUpcomingEvents() ([]*model.Event, error)
GetGuildScheduledEvents() ([]*model.DiscordEvent, error)
CreateGuildScheduledEvent(*model.DiscordEvent) error
SetSpeakers(message *model.DiscordMessage) error
GetOgifStats(discordID string, after time.Time) (model.OgifStats, error)
}
2 changes: 2 additions & 0 deletions pkg/discord/view/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/discord/view/memo"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/mma"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/news"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/ogif"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/profile"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/project"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/salary"
Expand Down Expand Up @@ -54,4 +55,5 @@ type Viewer interface {
Withdraw() withdrawal.Viewer
News() news.Viewer
Topic() topic.TopicViewer
Ogif() ogif.OgifViewer
}
34 changes: 34 additions & 0 deletions pkg/discord/view/ogif/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ogif

import (
"github.com/bwmarrin/discordgo"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/base"
"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (e *Ogif) Help(message *model.DiscordMessage) error {
embed := &discordgo.MessageEmbed{
Title: "OGIF Command Help",
Description: "The OGIF command allows you to fetch and display OGIF stats for a user.",
Color: 0x00ff00, // Green color
Fields: []*discordgo.MessageEmbedField{
{
Name: "Usage",
Value: "`ogif [user_mention] [time_period]`",
},
{
Name: "Parameters",
Value: "- `user_mention`: Optional. Mention the user to fetch stats for. If omitted, uses the command author.\n- `time_period`: Optional. Time period for stats (e.g., '7d', '30d', '3m'). Default is 30 days.",
},
{
Name: "Examples",
Value: "- `ogif`\n- `ogif @user`\n- `ogif @user 7d`\n- `ogif 14d`",
},
},
Footer: &discordgo.MessageEmbedFooter{
Text: "For more information, contact the bot administrator.",
},
}

return base.SendEmbededMessage(e.ses, message, embed)
}
10 changes: 10 additions & 0 deletions pkg/discord/view/ogif/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ogif

import (
"github.com/dwarvesf/fortress-discord/pkg/model"
)

type OgifViewer interface {
RenderOgifStats(original *model.DiscordMessage, userID string, stats model.OgifStats, timeAmount int, timeUnit string) error
Help(message *model.DiscordMessage) error
}
13 changes: 13 additions & 0 deletions pkg/discord/view/ogif/ogif.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ogif

import "github.com/bwmarrin/discordgo"

type Ogif struct {
ses *discordgo.Session
}

func New(ses *discordgo.Session) OgifViewer {
return &Ogif{
ses: ses,
}
}
Loading

0 comments on commit e9692f8

Please sign in to comment.