reactionroles

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: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddReactionRole = &dgc.Command{
	Name:        "add",
	Aliases:     []string{"add", "create"},
	Description: "Add a reaction role to a message. You must be in the same channel as the message.",
	Usage:       "rr add <message_id> <emoji> <role_id> <dm [false]> <remove_reaction [true]>",
	Example:     "rr add 123456789012345678 🎉 123456789012345678 true true",
	Category:    "Reaction Roles",
	Domain:      "astral.integrations.reactionroles.add",
	Handler: func(ctx *dgc.Ctx) {
		database := db.New()

		messageID := ctx.Arguments.Get(0).Raw()
		emoji := ctx.Arguments.Get(1).Raw()
		roleID := ctx.Arguments.Get(2).Raw()

		if messageID == "" || emoji == "" || roleID == "" {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "You must provide a message ID, emoji, and role ID",
				Color:       0xff0000,
			}))
			return
		}

		dm, err := ctx.Arguments.Get(3).AsBool()

		if err != nil {
			dm = false
		}

		removeReaction, err := ctx.Arguments.Get(4).AsBool()

		if err != nil {
			removeReaction = true
		}

		self := ctx.CustomObjects.MustGet("self").(types.Bot)

		workspaceData, err := database.GetIntegrationDataForWorkspace(*self.Workspace, ReactionRolesIntegrationID)

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

		roles := ReactionRolesData{
			ReactionRoles: []ReactionRole{
				{
					MessageID:      messageID,
					ChannelID:      ctx.Event.ChannelID,
					Emoji:          emoji,
					RoleID:         roleID,
					DM:             dm,
					RemoveReaction: removeReaction,
				},
			},
		}

		if len(workspaceData) == 0 {
			err = database.SetIntegrationDataForWorkspace(*self.Workspace, ReactionRolesIntegrationID, roles)

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

			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Success",
				Description: "Successfully added the reaction role.",
				Color:       0x00ff00,
			}))
			return
		} else {
			jsonStr, err := json.Marshal(workspaceData[0].Data)

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

			var data ReactionRolesData

			err = json.Unmarshal(jsonStr, &data)

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

			err = database.SetIntegrationDataForWorkspace(*self.Workspace, ReactionRolesIntegrationID, ReactionRolesData{
				ReactionRoles: append(data.ReactionRoles, roles.ReactionRoles...),
			})

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

			m, err := ctx.Session.ChannelMessage(ctx.Event.ChannelID, messageID)

			if err != nil {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "An error occurred while fetching the message. Make sure you're in the same channel as the message.",
					Color:       0xff0000,
					Fields: []*discordgo.MessageEmbedField{
						{
							Name:   "Error",
							Value:  err.Error(),
							Inline: false,
						},
					},
				}))
				return
			}

			hasReaction := false

			for _, reaction := range m.Reactions {
				if reaction.Emoji.APIName() == emoji || reaction.Emoji.Name == emoji {
					hasReaction = true
					break
				}
			}

			str := "Successfully added the reaction role."

			if !hasReaction {
				err = ctx.Session.MessageReactionAdd(ctx.Message.ChannelID, messageID, emoji)

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

				str += "\n\nThe reaction has been added to the message."
			}

			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Success",
				Description: str,
				Color:       0x00ff00,
			}))
		}
	},
}
View Source
var ListReactionRoles = &dgc.Command{
	Name:        "list",
	Aliases:     []string{"list", "ls"},
	Description: "List all reaction roles for a message",
	Usage:       "rr list <message_id>",
	Example:     "rr list 123456789012345678",
	Category:    "Reaction Roles",
	Domain:      "astral.integrations.reactionroles.list",
	Handler: func(ctx *dgc.Ctx) {
		database := db.New()

		messageID := ctx.Arguments.Get(0).Raw()

		self := ctx.CustomObjects.MustGet("self").(types.Bot)

		workspaceData, err := database.GetIntegrationDataForWorkspace(*self.Workspace, ReactionRolesIntegrationID)

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

		if len(workspaceData) == 0 {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No reaction roles have been added to this server.",
				Color:       0xff0000,
			}))
			return
		}

		jsonStr, err := json.Marshal(workspaceData[0].Data)

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

		var reactionRolesData ReactionRolesData

		err = json.Unmarshal(jsonStr, &reactionRolesData)

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

		var reactionRoles []ReactionRole

		for _, reactionRole := range reactionRolesData.ReactionRoles {
			if reactionRole.MessageID == messageID && messageID != "" {
				reactionRoles = append(reactionRoles, reactionRole)
			}
		}

		if len(reactionRoles) == 0 && messageID != "" {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No reaction roles have been added to this message.",
				Color:       0xff0000,
			}))
			return
		}

		if len(reactionRolesData.ReactionRoles) == 0 {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No reaction roles have been added to this server.",
				Color:       0xff0000,
			}))
			return
		}

		var fields []*discordgo.MessageEmbedField
		var description string

		if messageID == "" {
			description = "All reaction roles for this server."
			var fs map[string]*discordgo.MessageEmbedField
			for _, reactionRole := range reactionRolesData.ReactionRoles {
				if fs == nil {
					fs = make(map[string]*discordgo.MessageEmbedField)
				}

				if fs[reactionRole.MessageID] == nil {
					fs[reactionRole.MessageID] = &discordgo.MessageEmbedField{
						Name:   fmt.Sprintf("%s", reactionRole.MessageID),
						Value:  fmt.Sprintf("%s - <@&%s>", reactionRole.Emoji, reactionRole.RoleID),
						Inline: false,
					}
				} else {
					fs[reactionRole.MessageID].Value += fmt.Sprintf("\n%s - <@&%s>", reactionRole.Emoji, reactionRole.RoleID)
				}

				fs[reactionRole.MessageID].Value += fmt.Sprintf("\n\n[Visit Message](https://discord.com/channels/%s/%s/%s)", ctx.Event.GuildID, reactionRole.ChannelID, reactionRole.MessageID)
			}

			for _, f := range fs {
				fields = append(fields, f)
			}
		} else {
			description = fmt.Sprintf("Reaction roles for message [%s](https://discord.com/channels/%s/%s/%s)", messageID, ctx.Event.GuildID, reactionRoles[0].ChannelID, messageID)
			for _, reactionRole := range reactionRoles {
				fields = append(fields, &discordgo.MessageEmbedField{
					Name:   reactionRole.Emoji,
					Value:  fmt.Sprintf("<@&%s>", reactionRole.RoleID),
					Inline: false,
				})
			}
		}

		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
			Title:       "Reaction Roles",
			Description: description,
			Color:       0x00ff00,
			Fields:      fields,
		}))
	},
}
View Source
var ReactionRoleCommand = &dgc.Command{
	Name:        "rr",
	Aliases:     []string{"rr", "reactionrole"},
	Description: "Parent command of all reaction role commands.",
	Usage:       "rr <subcommand>",
	Example:     "rr add",
	Category:    "Reaction Roles",
	Domain:      "astral.integrations.reactionroles",
	Handler: func(ctx *dgc.Ctx) {
		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
			Title:       "Error",
			Description: "You must provide a subcommand.",
			Color:       0xff0000,
		}))
	},
	SubCommands: []*dgc.Command{
		AddReactionRole,
		RemoveReactionRole,
		ListReactionRoles,
	},
}
View Source
var ReactionRolesIntegrationID = "827bc5bb-4be4-4cc4-9ef6-db0c2546662f"
View Source
var RemoveReactionRole = &dgc.Command{
	Name:        "remove",
	Aliases:     []string{"remove", "rm"},
	Description: "Remove a reaction role from a message",
	Usage:       "rr remove <message_id> <emoji>",
	Example:     "rr remove 123456789012345678 🎉",
	Category:    "Reaction Roles",
	Domain:      "astral.integrations.reactionroles.remove",
	Handler: func(ctx *dgc.Ctx) {
		database := db.New()

		messageID := ctx.Arguments.Get(0).Raw()
		emoji := ctx.Arguments.Get(1).Raw()

		if messageID == "" || emoji == "" {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "You must provide a message ID and emoji",
				Color:       0xff0000,
			}))
			return
		}

		self := ctx.CustomObjects.MustGet("self").(types.Bot)

		workspaceData, err := database.GetIntegrationDataForWorkspace(*self.Workspace, ReactionRolesIntegrationID)

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

		if len(workspaceData) == 0 {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No reaction roles have been added to this server.",
				Color:       0xff0000,
			}))
			return
		}

		jsonStr, err := json.Marshal(workspaceData[0].Data)

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

		var reactionRolesData ReactionRolesData

		err = json.Unmarshal(jsonStr, &reactionRolesData)

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

		if len(reactionRolesData.ReactionRoles) == 0 {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No reaction roles have been added to this server.",
				Color:       0xff0000,
			}))
			return
		}

		var reactionRole ReactionRole

		for _, rr := range reactionRolesData.ReactionRoles {
			if rr.MessageID == messageID && rr.Emoji == emoji {
				reactionRole = rr
			}
		}

		if reactionRole.MessageID == "" {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "No reaction role found for the provided message ID and emoji.",
				Color:       0xff0000,
			}))
			return
		}

		for i, rr := range reactionRolesData.ReactionRoles {
			if rr.MessageID == messageID && rr.Emoji == emoji {
				reactionRolesData.ReactionRoles = append(reactionRolesData.ReactionRoles[:i], reactionRolesData.ReactionRoles[i+1:]...)
			}
		}

		err = database.SetIntegrationDataForWorkspace(*self.Workspace, ReactionRolesIntegrationID, reactionRolesData)

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

		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
			Title:       "Success",
			Description: "Successfully removed the reaction role.",
			Color:       0x00ff00,
		}))
	},
}

Functions

func HandleReactionRolesAdd

func HandleReactionRolesAdd(workspace string) func(s *discordgo.Session, m *discordgo.MessageReactionAdd)

func HandleReactionRolesRemove

func HandleReactionRolesRemove(workspace string) func(s *discordgo.Session, m *discordgo.MessageReactionRemove)

Types

type ReactionRole

type ReactionRole struct {
	MessageID      string `json:"message_id"`
	ChannelID      string `json:"channel_id"`
	Emoji          string `json:"emoji"`
	RoleID         string `json:"role_id"`
	DM             bool   `json:"dm"`
	RemoveReaction bool   `json:"remove_reaction"` // Removes if the user already has the role and
}

type ReactionRolesData

type ReactionRolesData struct {
	ReactionRoles []ReactionRole `json:"reaction_roles"`
}

Jump to

Keyboard shortcuts

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