integrations

package
v0.0.0-...-f00225b Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: OSL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LastfmIntegrationID = "f98d0f70-c537-4fda-ad69-50cb0f1a3013"
View Source
var ScrobblesCommand = &dgc.Command{
	Name:          "scrobbles",
	Domain:        "astral.integrations.scrobbles",
	Aliases:       []string{"scrobbles"},
	Description:   "Get your most recent scrobbles, or someone else's.",
	Slash:         true,
	SlashGuilds:   []string{os.Getenv("DEV_GUILD")},
	IntegrationID: LastfmIntegrationID,
	Arguments: []*discordgo.ApplicationCommandOption{
		{
			Name:        "user",
			Description: "The user to get scrobbles for.",
			Type:        discordgo.ApplicationCommandOptionUser,
			Required:    false,
		},
	},
	Handler: func(ctx *dgc.Ctx) {
		database := db.New()

		var user string
		var getProvider bool = true

		if ctx.Arguments.Amount() < 1 {
			user = ctx.Message.Author.ID
		} else if ctx.Arguments.Get(0).AsUserMentionID() != "" {
			user = ctx.Arguments.Get(0).AsUserMentionID()
		} else {
			u := ctx.Arguments.Get(0).Raw()

			isNotDigit := func(c rune) bool { return c < '0' || c > '9' }
			b := strings.IndexFunc(u, isNotDigit) == -1

			if b {
				getProvider = true
			} else {
				getProvider = false
			}

			user = u
		}

		var provider types.Provider

		if getProvider {
			p, err := database.GetProviderFromDiscord(user, "lastfm")

			if err != nil {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "An error occurred while fetching the Last.fm account.",
					Fields: []*discordgo.MessageEmbedField{
						{
							Name:   "Error",
							Value:  err.Error(),
							Inline: false,
						},
					},
					Color: 0xff0000,
				}))
				return
			}

			if len(p) < 1 {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "You don't have a Last.fm account linked to your account. You may link one [here](https://auth.astralapp.io).",
					Color:       0xff0000,
				}))
				return
			}

			provider = p[0]
		} else {
			provider = types.Provider{
				ProviderID: user,
			}
		}

		api := lastfm.New(os.Getenv("LASTFM_API_KEY"), os.Getenv("LASTFM_API_SECRET"))

		res, err := api.User.GetRecentTracks(map[string]interface{}{
			"user": provider.ProviderID,
		})

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the scrobbles.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		if res.Total == 0 {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No scrobbles found.",
				Color:       0xff0000,
			}))
			return
		}

		embed := discordgo.MessageEmbed{
			Title:       fmt.Sprintf("%s's Scrobbles", res.User),
			Description: "",
			Color:       0x00ff00,
		}
		for i, track := range res.Tracks {
			if i > 5 {
				break
			}
			embed.Description += fmt.Sprintf("`%d` - %s\nBy **%s**\n", i+1, track.Name, track.Artist.Name)
		}

		embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
			Name:   "Total scrobbles",
			Value:  fmt.Sprintf("%d", res.Total),
			Inline: false,
		}, &discordgo.MessageEmbedField{
			Name:   "Last.fm Profile",
			Value:  fmt.Sprintf("[%s](https://last.fm/user/%s)", res.User, res.User),
			Inline: false,
		})

		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, embed))
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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