fun

package
v0.0.0-...-ec10a6c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2024 License: GPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PageSize = 20
)

Variables

View Source
var EvenCommand = command.Command{
	Name: "even", Aliases: []string{"odd"},
	Parameters: []*command.Parameter{
		{
			Name:     "number",
			Type:     parameters.ParameterInt,
			Required: true,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandEven)

		n := ctx.Args[0].(int)
		if n&1 == 0 {
			return ctx.Success(t.Even.Str())
		}

		return ctx.Success(t.Odd.Str())
	},
}
View Source
var FollowCommand = command.Command{
	Name: "follow",
	Parameters: []*command.Parameter{
		{
			Name:     "game",
			Required: true,
			Type:     parameters.ParameterText,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandFollow)

		teamNameOrID := ctx.Args[0].(string)
		match, err := getMatchByTeamNameOrID(teamNameOrID)
		if err != nil {
			slog.Error("Cannot get match", tint.Err(err))
			return ctx.Error(t.SomethingWentWrong.Str())
		}

		if match == nil {
			return ctx.Error(t.MatchNotFound.Str())
		}

		liveMatch, err := livescore.GetLiveMatch(match.ID)
		if errors.Is(err, livescore.ErrMatchHasFinished) {
			return ctx.Error(t.MatchFinished.Str())
		}

		if len(followedMatchIDs[ctx.AuthorID]) >= maxFollowPerUser {
			return ctx.Error(t.FollowLimitReached.Str(maxFollowPerUser, command.Prefix))
		}

		for _, followedMatchID := range followedMatchIDs[ctx.AuthorID] {
			if followedMatchID == match.ID {
				return ctx.Error(t.AlreadyFollowing.Str())
			}
		}

		addUserFollow(ctx.AuthorID, match.ID)

		embed := buildMatchEmbed(match, t.MatchInfo)

		listenerID := getListenerID(ctx.AuthorID, match.ID)

		liveMatch.AddListener(listenerID, func(match *livescore.LiveMatch, err error) {
			if err != nil {
				ctx.Error(err.Error())
				removeUserFollow(ctx.AuthorID, match.MatchID)
				return
			}
			embed := buildMatchEmbed(match.CurrentData, t.MatchInfo)
			err = ctx.EditComplexMessage(&model.ComplexMessage{
				Embeds: []*model.Embed{embed},
			})
			if err != nil {
				_ = liveMatch.RemoveListener(listenerID)
				removeUserFollow(ctx.AuthorID, match.MatchID)
			}
		})

		return ctx.Embed(embed)
	},
}
View Source
var JokeCommand = command.Command{
	Name: "joke",
	Handler: func(ctx *command.Context) command.Result {
		joke, err := joke.GetRandomJoke()
		if err != nil {
			slog.Error("Cannot get random joke", tint.Err(err))
			return ctx.Error(ctx.Lang.SomethingWentWrong.Str())
		}
		return ctx.Successf("%s\n\n%s", joke.Setup, joke.Punchline)
	},
}
View Source
var LiveCommand = command.Command{
	Name: "live",
	Parameters: []*command.Parameter{
		{
			Name:     "page",
			Required: false,
			Type:     parameters.ParameterInt,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		return ListLiveMatches(ctx, ctx.T.(*i18n.CommandLive))
	},
}
View Source
var NewsCommand = command.Command{
	Name: "news",
	Parameters: []*command.Parameter{
		{
			Name:            "source",
			Type:            parameters.ParameterString,
			Required:        true,
			ValidValuesFunc: listSources,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandNews)

		source := ctx.Args[0].(string)

		news, err := Sources[source]()
		if err != nil {
			slog.Error("Cannot load news source", tint.Err(err))
			return ctx.Error(t.SomethingWentWrong.Str())
		}

		author := news.Author
		if author == "" {
			author = t.Unknown.Str()
		}

		embed := model.NewEmbed().
			WithTitle(t.Title.Str(news.Title, author)).
			WithDescription(news.Description).
			WithImage(news.ThumbnailURL)

		for _, entry := range news.Entries[:10] {
			shortDescription := entry.Description
			if len(shortDescription) > 100 {
				shortDescription = shortDescription[:97] + "..."
			}
			var postedAt string
			if entry.PostedAt != nil {
				postedAt = entry.PostedAt.Format("2006-01-02")
			}
			if postedAt == "" {
				embed.WithField(
					entry.Title,
					fmt.Sprintf("%s \n[%s](%s)", shortDescription, t.SeeMore.Str(), entry.URL),
				)
			} else {
				embed.WithField(
					entry.Title,
					fmt.Sprintf("%s | %s \n[%s](%s)", shortDescription, postedAt, t.SeeMore.Str(), entry.URL),
				)
			}
		}

		return ctx.SuccessEmbed(embed)
	},
}
View Source
var PickCommand = command.Command{
	Name: "pick",
	Parameters: []*command.Parameter{
		{
			Name:     "things",
			Required: true,
			Type:     parameters.ParameterText,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandPick)

		things := strings.Split(ctx.Args[0].(string), " ")
		n, err := rnd.Rnd(len(things))
		if err != nil {
			return ctx.Error(t.SomethingWentWrong.Str())
		}

		return ctx.SuccessEmbed(
			model.NewEmbed().
				WithTitle("Pick").
				WithDescription(
					t.Description.Str(ctx.Args[0], things[n]),
				),
		)
	},
}
View Source
var RollCommand = command.Command{
	Name: "roll", Aliases: []string{"dice"},
	Parameters: []*command.Parameter{
		{Name: "faces", Required: false, Type: diceNotation},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandRoll)

		var d *dice.DiceNotation

		if len(ctx.Args) == 1 {
			d = ctx.Args[0].(*dice.DiceNotation)
		} else {
			d = dice.DefaultDice
		}

		numbers := make([]int, d.Dices)
		result := 0

		for i := 0; i < d.Dices; i++ {
			luckyNumber, err := rnd.Rnd(d.Faces)
			if err != nil {
				slog.Error("Cannot get random number", tint.Err(err))
				return ctx.Error(t.SomethingWentWrong.Str())
			}

			luckyNumber++
			result += luckyNumber
			numbers[i] = luckyNumber
		}

		embed := model.NewEmbed().
			WithTitle(t.Title.Str(":game_die:", result)).
			WithDescription(
				t.Description.Str(
					d.String(),
					d.Dices, f.Pluralize(d.Dices, t.Dice.Str(), t.Dices.Str()),
					d.Faces, f.Pluralize(d.Faces, t.Face.Str(), t.Faces.Str()),
					numbers, result,
				),
			).
			WithImage(gif)

		return ctx.SuccessEmbed(embed)
	},
}
View Source
var ScoreCommand = command.Command{
	Name: "score",
	Parameters: []*command.Parameter{
		{
			Name:     "game",
			Required: true,
			Type:     parameters.ParameterText,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandScore)

		return showMatchInfo(ctx, t)

	},
}
View Source
var (
	Sources = map[string]NewsFactory{
		"thn":       news.GetTHNFeed,
		"cnn-top":   news.GetCNNTopStoriesFeed,
		"cnn-world": news.GetCNNWorldFeed,
		"cnn-tech":  news.GetCNNTechFeed,
	}
)
View Source
var UnFollowCommand = command.Command{
	Name: "unfollow",
	Parameters: []*command.Parameter{
		{
			Name:     "game",
			Required: false,
			Type:     parameters.ParameterText,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		t := ctx.T.(*i18n.CommandUnFollow)

		authorID := ctx.AuthorID

		if len(ctx.Args) == 0 {
			if len(followedMatchIDs[authorID]) == 0 {
				return ctx.Error(t.NotFollowingAny.Str())
			}
			for _, matchID := range followedMatchIDs[authorID] {
				liveMatch, err := livescore.GetLiveMatch(matchID)
				if err != nil {
					return ctx.Error(t.SomethingWentWrong.Str())
				}
				_ = liveMatch.RemoveListener(getListenerID(authorID, matchID))
				removeUserFollow(authorID, matchID)
			}
			return ctx.Success(t.UnFollowedAll.Str())
		}

		teamNameOrID := ctx.Args[0].(string)
		match, err := getMatchByTeamNameOrID(teamNameOrID)
		if err != nil {
			slog.Error("Cannot get match", tint.Err(err))
			return ctx.Error(t.SomethingWentWrong.Str())
		}
		if match == nil {
			return ctx.Error(t.MatchNotFound.Str())
		}
		liveMatch, err := livescore.GetLiveMatch(match.ID)
		if err != nil {
			slog.Error("Cannot get live match", tint.Err(err))
			return ctx.Error(t.SomethingWentWrong.Str())
		}
		err = liveMatch.RemoveListener(getListenerID(authorID, match.ID))
		if err == livescore.ErrListenerNotFound {
			return ctx.Error(t.NotFollowingMatch.Str())
		}
		removeUserFollow(authorID, match.ID)
		return ctx.Success(t.UnfollowedMatch.Str(match.T1.Name, match.T2.Name))
	},
}
View Source
var XkcdCommand = command.Command{
	Name: "xkcd",
	SubCommands: []*command.Command{
		&XkcdLatestSubCommand,
		&XkcdRandomSubCommand,
		&XkcdNumberSubCommand,
	},
}
View Source
var XkcdLatestSubCommand = command.Command{
	Name: "latest",
	Handler: func(ctx *command.Context) command.Result {
		comic, err := xkcd.GetLatest()
		return sendComic(ctx, comic, err)
	},
}
View Source
var XkcdNumberSubCommand = command.Command{
	Name:    "number",
	Aliases: []string{"num"},
	Parameters: []*command.Parameter{
		{
			Name: "number",
			Type: parameters.ParameterInt, Required: true,
		},
	},
	Handler: func(ctx *command.Context) command.Result {
		comic, err := xkcd.GetByNum(ctx.Args[0].(int))
		return sendComic(ctx, comic, err)
	},
}
View Source
var XkcdRandomSubCommand = command.Command{
	Name: "random",
	Handler: func(ctx *command.Context) command.Result {
		comic, err := xkcd.GetRandom()
		return sendComic(ctx, comic, err)
	},
}

Functions

func ListLiveMatches

func ListLiveMatches(ctx *command.Context, t *i18n.CommandLive) command.Result

Types

type NewsFactory

type NewsFactory func() (*news.NewsFeed, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL