exts

package
v0.0.0-...-9edbf76 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandHandlers = map[string]func(c *shrouter.Context, s *discordgo.Session, i *discordgo.InteractionCreate){
	"ping": func(c *shrouter.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
		err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionApplicationCommandResponseData{
				Content: fmt.Sprintf("Pong %dms!", s.HeartbeatLatency()/time.Millisecond),
			},
		})
		if err != nil {
			log.Printf("Error sending message: %s", err)
		}
	},
	"ban": func(c *shrouter.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
		if !utils.CanBan(s, i.GuildID, i.Member) {
			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
				Type: discordgo.InteractionResponseChannelMessageWithSource,
				Data: &discordgo.InteractionApplicationCommandResponseData{
					Content: fmt.Sprintf("You lack the permissions to run this command."),
				},
			})
			if err != nil {
				log.Printf("Error sending message: %s", err)
			}
			return
		}
		var user *discordgo.User
		var reason string = ""
		var days int = 0
		if found, o := utils.FindOption("user", i.Data.Options); found {
			user = o.UserValue(s)
		}
		if found, o := utils.FindOption("reason", i.Data.Options); found {
			reason = o.StringValue()
		}
		if found, o := utils.FindOption("days", i.Data.Options); found {
			days = int(o.IntValue())
		}
		if err := s.GuildBanCreateWithReason(i.GuildID, user.ID, reason, days); err != nil {
			log.Printf("Error banning user '%s': %s", i.Data.Options[0].UserValue(s).ID, err)
		}
		if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionApplicationCommandResponseData{
				Content: fmt.Sprintf("%s was banned", user.Mention()),
			},
		}); err != nil {
			log.Printf("Error responding to interaction: %s", err)
		}
	},
	"embed": func(c *shrouter.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
		if !utils.IsAdmin(s, i.GuildID, i.Member) {
			s.ChannelMessageSend(i.ChannelID, "You lack the permissions to run this command.")
			return
		}
		switch i.Data.Options[0].Name {
		case "create":
			var title string
			var desc string
			var color int
			var author discordgo.MessageEmbedAuthor
			var thumbnail discordgo.MessageEmbedThumbnail
			var footer discordgo.MessageEmbedFooter
			var timestamp string
			if found, o := utils.FindOption("title", i.Data.Options[0].Options); found {
				title = fmt.Sprintf("%v", o.Value)
			}
			if found, o := utils.FindOption("description", i.Data.Options[0].Options); found {
				desc = fmt.Sprintf("%v", o.Value)
			}
			if found, o := utils.FindOption("color", i.Data.Options[0].Options); found {
				color = int(o.IntValue())
			}
			if found, o := utils.FindOption("author", i.Data.Options[0].Options); found {
				u := o.UserValue(s)
				author.Name = u.String()
				author.IconURL = u.AvatarURL("")
			}
			if found, o := utils.FindOption("thumbnail", i.Data.Options[0].Options); found {
				thumbnail.URL = o.StringValue()
			}
			if found, o := utils.FindOption("footertext", i.Data.Options[0].Options); found {
				footer.Text = o.StringValue()
			}
			if found, o := utils.FindOption("footericon", i.Data.Options[0].Options); found {
				if o.StringValue() == "server" {
					g, err := s.Guild(i.GuildID)
					if err != nil {
						log.Printf("Error retrieving guild: %s", err)
						return
					}
					footer.IconURL = g.IconURL()
				} else {
					footer.IconURL = o.StringValue()
				}
			}
			if found, o := utils.FindOption("timestamp", i.Data.Options[0].Options); found {
				if o.BoolValue() {
					timestamp = time.Now().Format(time.RFC3339)
				}
			}
			embed := &discordgo.MessageEmbed{
				Title:       title,
				Description: desc,
				Color:       color,
				Author:      &author,
				Thumbnail:   &thumbnail,
				Footer:      &footer,
				Timestamp:   timestamp,
			}
			found, o := utils.FindOption("channel", i.Data.Options[0].Options)
			if !found {
				log.Printf("Unable to find channel option")
				return
			}
			if _, err := s.ChannelMessageSendEmbed(o.ChannelValue(s).ID, embed); err != nil {
				log.Printf("Error sending message: %s", err)
			}
			if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
				Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
			}); err != nil {
				log.Printf("Error responding to interaction: %s", err)
			}

		case "edit":
			found, o := utils.FindOption("channel", i.Data.Options[0].Options)
			if !found {
				log.Printf("Unable to find channel option")
				return
			}
			channelID := o.ChannelValue(s).ID
			found, o = utils.FindOption("messageid", i.Data.Options[0].Options)
			if !found {
				log.Printf("Unable to find messageID option")
				return
			}
			messageID := o.StringValue()
			m, err := s.ChannelMessage(channelID, messageID)
			if err != nil {
				log.Printf("Error finding message: %s", err)
				return
			}
			title := m.Embeds[0].Title
			desc := m.Embeds[0].Description
			color := m.Embeds[0].Color
			author := m.Embeds[0].Author
			thumbnail := m.Embeds[0].Thumbnail
			footer := m.Embeds[0].Footer
			timestamp := m.Embeds[0].Timestamp
			if found, o := utils.FindOption("title", i.Data.Options[0].Options); found {
				title = fmt.Sprintf("%v", o.Value)
			}
			if found, o := utils.FindOption("description", i.Data.Options[0].Options); found {
				desc = fmt.Sprintf("%v", o.Value)
			}
			if found, o := utils.FindOption("color", i.Data.Options[0].Options); found {
				color = int(o.IntValue())
			}
			if found, o := utils.FindOption("author", i.Data.Options[0].Options); found {
				u := o.UserValue(s)
				author.Name = u.String()
				author.IconURL = u.AvatarURL("")
			}
			if found, o := utils.FindOption("thumbnail", i.Data.Options[0].Options); found {
				thumbnail.URL = o.StringValue()
			}
			if found, o := utils.FindOption("footertext", i.Data.Options[0].Options); found {
				footer.Text = o.StringValue()
			}
			if found, o := utils.FindOption("footericon", i.Data.Options[0].Options); found {
				if o.StringValue() == "server" {
					g, err := s.Guild(i.GuildID)
					if err != nil {
						log.Printf("Error retrieving guild: %s", err)
						return
					}
					footer.IconURL = g.IconURL()
				} else {
					footer.IconURL = o.StringValue()
				}
			}
			if found, o := utils.FindOption("timestamp", i.Data.Options[0].Options); found {
				if o.BoolValue() {
					timestamp = time.Now().Format(time.RFC3339)
				}
			}
			embed := &discordgo.MessageEmbed{
				Title:       title,
				Description: desc,
				Color:       color,
				Author:      author,
				Thumbnail:   thumbnail,
				Footer:      footer,
				Timestamp:   timestamp,
			}
			if _, err := s.ChannelMessageEditEmbed(channelID, messageID, embed); err != nil {
				log.Printf("Error sending message: %s", err)
			}
			err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
				Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
			})
			if err != nil {
				log.Printf("Interaction response error: %s", err)
			}
		case "append":
			found, o := utils.FindOption("channel", i.Data.Options[0].Options)
			if !found {
				log.Printf("Unable to find channel option")
				return
			}
			channelID := o.ChannelValue(s).ID
			found, o = utils.FindOption("messageid", i.Data.Options[0].Options)
			if !found {
				log.Printf("Unable to find messageID option")
				return
			}
			messageID := o.StringValue()
			m, err := s.ChannelMessage(channelID, messageID)
			if err != nil {
				log.Printf("Error finding message: %s", err)
				return
			}
			found, o = utils.FindOption("text", i.Data.Options[0].Options)
			if !found {
				log.Printf("Unable to find text option")
				return
			}
			text := o.StringValue()
			title := m.Embeds[0].Title
			desc := m.Embeds[0].Description + "\n" + text
			color := m.Embeds[0].Color
			author := m.Embeds[0].Author
			thumbnail := m.Embeds[0].Thumbnail
			footer := m.Embeds[0].Footer
			timestamp := m.Embeds[0].Timestamp
			if _, err := s.ChannelMessageEditEmbed(channelID, messageID, &discordgo.MessageEmbed{
				Title:       title,
				Description: desc,
				Color:       color,
				Author:      author,
				Thumbnail:   thumbnail,
				Footer:      footer,
				Timestamp:   timestamp,
			}); err != nil {
				log.Printf("Error sending message: %s", err)
			}
			err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
				Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
			})
			if err != nil {
				log.Printf("Interaction response error: %s", err)
			}
		}

	},
	"avatar": func(c *shrouter.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
		var u *discordgo.User
		if found, o := utils.FindOption("user", i.Data.Options); found {
			u = o.UserValue(s)
		}
		err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
		})
		if err != nil {
			log.Printf("Error sending message: %s", err)
		}
		s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{
			Author: &discordgo.MessageEmbedAuthor{
				Name:    u.String(),
				IconURL: u.AvatarURL(""),
			},
			Image: &discordgo.MessageEmbedImage{
				URL: u.AvatarURL("2048"),
			},
		})
	},
}

CommandHandlers is a slice of all my command handlers

View Source
var Commands = []*discordgo.ApplicationCommand{
	{
		Name:        "ping",
		Description: "Pings the bot. Returns Pong with latency.",
	},
	{
		Name:        "ban",
		Description: "Bans a user",
		Options: []*discordgo.ApplicationCommandOption{
			{
				Type:        discordgo.ApplicationCommandOptionUser,
				Name:        "user",
				Description: "The user you want to ban",
				Required:    true,
			},
			{
				Type:        discordgo.ApplicationCommandOptionString,
				Name:        "reason",
				Description: "The ban reason",
				Required:    false,
			},
			{
				Type:        discordgo.ApplicationCommandOptionInteger,
				Name:        "days",
				Description: "The number of days of messages to delete",
				Required:    false,
			},
		},
	},
	{
		Name:        "avatar",
		Description: "Returns the avatar of the user",
		Options: []*discordgo.ApplicationCommandOption{
			{
				Type:        discordgo.ApplicationCommandOptionUser,
				Name:        "user",
				Description: "The user whose avatar you want",
				Required:    false,
			},
		},
	},
	{
		Name:        "embed",
		Description: "Creates, edits, or appends to an embed.",
		Options: []*discordgo.ApplicationCommandOption{
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "create",
				Required:    false,
				Description: "Creates an embed",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:        discordgo.ApplicationCommandOptionChannel,
						Name:        "channel",
						Description: "The channel the embed will be posted in",
						Required:    true,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "title",
						Description: "The title of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "description",
						Description: "The description of your embed (One line only, use append subcommand to add more.)",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionInteger,
						Name:        "color",
						Description: "The color of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionUser,
						Name:        "author",
						Description: "The author of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "footertext",
						Description: "The footer text of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "footericon",
						Description: "The icon URL of the footer",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "thumbnail",
						Description: "The thumbnail of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionBoolean,
						Name:        "timestamp",
						Description: "The timestamp of your embed (if true, it will set the current time)",
						Required:    false,
					},
				},
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "edit",
				Required:    false,
				Description: "Edits an embed",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:        discordgo.ApplicationCommandOptionChannel,
						Name:        "channel",
						Description: "The channel the embed is in",
						Required:    true,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "messageid",
						Description: "The message ID of the embed",
						Required:    true,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "title",
						Description: "The new title of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "description",
						Description: "The new description of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionInteger,
						Name:        "color",
						Description: "The new color of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionUser,
						Name:        "author",
						Description: "The new author of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "footertext",
						Description: "The new footer text of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "footericon",
						Description: "The new icon URL of the footer",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "thumbnail",
						Description: "The new thumbnail of your embed",
						Required:    false,
					},
					{
						Type:        discordgo.ApplicationCommandOptionBoolean,
						Name:        "timestamp",
						Description: "Change the timestamp of your embed (if true, it will set the current time)",
						Required:    false,
					},
				},
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "append",
				Required:    false,
				Description: "Appends a line to the embed description.",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:        discordgo.ApplicationCommandOptionChannel,
						Name:        "channel",
						Description: "The channel the embed is in",
						Required:    true,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "messageid",
						Description: "The message ID of the embed",
						Required:    true,
					},
					{
						Type:        discordgo.ApplicationCommandOptionString,
						Name:        "text",
						Description: "The text to be appended",
						Required:    true,
					},
				},
			},
		},
	},
}

Commands is a slice of all my commands

View Source
var Handlers = []interface{}{
	func(s *discordgo.Session, r *discordgo.Ready) {
		log.Printf("Logged in as %v\n", r.User)
		fmt.Println("-------------------------------------------------")
	},
	func(s *discordgo.Session, g *discordgo.GuildCreate) {
		ms := postgres.NewMemberService()
		for _, m := range g.Members {
			if err := ms.AddMember(bot.NewMember(m)); err != nil {
				log.Printf("Error adding member '%s': %s", m.User.ID, err)
			}
		}
		log.Print("Finished adding members to database.")
	},
	func(s *discordgo.Session, m *discordgo.MessageCreate) {
		dmChannel, err := s.Channel(m.ChannelID)
		if err != nil {
			log.Printf("Error getting dm channel: %s", err)
			return
		}
		if dmChannel.Type != discordgo.ChannelTypeDM || m.Author.ID == s.State.User.ID {
			return
		}
		chName := strings.NewReplacer("#", " ").Replace(m.Author.String())
		guild, err := s.Guild(utils.GuildID)
		if err != nil {
			log.Printf("Error finding guild: %s", err)
			return
		}
		logFooter := &discordgo.MessageEmbedFooter{
			Text:    m.Author.String() + " | " + m.Author.ID,
			IconURL: guild.IconURL(),
		}
		logNewTicket := &discordgo.MessageEmbed{
			Title:     "New Ticket",
			Color:     0x2ecc71,
			Footer:    logFooter,
			Timestamp: time.Now().Format(time.RFC3339),
		}

		author := &discordgo.MessageEmbedAuthor{
			Name:    m.Author.String(),
			IconURL: m.Author.AvatarURL(""),
		}
		replyFooter := &discordgo.MessageEmbedFooter{
			Text:    "ID: " + m.Author.ID,
			IconURL: s.State.User.AvatarURL(""),
		}
		reply := &discordgo.MessageEmbed{
			Title:       "Message Sent",
			Description: m.Content,
			Color:       0xff4949,
			Author:      author,
			Footer:      replyFooter,
			Timestamp:   time.Now().Format(time.RFC3339),
		}
		mmFooter := &discordgo.MessageEmbedFooter{
			Text:    guild.Name + " | " + guild.ID,
			IconURL: guild.IconURL(),
		}
		mm := &discordgo.MessageEmbed{
			Title:       "Message Received",
			Description: m.Content,
			Color:       0x2ecc71,
			Author:      author,
			Footer:      mmFooter,
			Timestamp:   time.Now().Format(time.RFC3339),
		}
		s.ChannelMessageSendEmbed(utils.ModmailLogID, logNewTicket)
		s.ChannelMessageSendEmbed(m.ChannelID, reply)
		data := discordgo.GuildChannelCreateData{
			Name:     chName,
			ParentID: utils.ModmailCatID,
		}
		ch, err := s.GuildChannelCreateComplex(utils.GuildID, data)
		if err != nil {
			log.Print(err)
			return
		}
		_, err = s.ChannelMessageSendEmbed(ch.ID, mm)
		if err != nil {
			log.Print(err)
			return
		}
	},
	func(s *discordgo.Session, m *discordgo.MessageCreate) {
		_ = `^\[(?P<book>(?:\d\s*)?[A-Z]?[a-z]+)\s*(?P<chapter>\d+):(?P<verses>(?P<start>\d+)(?:-(?P<end>\d+))?)(?:\s(?P<version>[A-Z]?[a-z]+))?\]$`
	},
}

Handlers is a a slice of all my handlers

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