Compare commits
4 Commits
v0.4.3
...
option/cha
Author | SHA1 | Date | |
---|---|---|---|
b47c979e3b
|
|||
bfe9601cd3
|
|||
c6d877b101
|
|||
3bf763f196
|
47
bolt.go
47
bolt.go
@@ -14,21 +14,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TOKEN_ENV_VAR = "DISCORD_TOKEN" //label for token environment variable
|
//Discord auth token must be saved as an environment variable using this label/name
|
||||||
|
//in order for bolt to detect it
|
||||||
|
TOKEN_ENV_VAR = "DISCORD_TOKEN"
|
||||||
|
|
||||||
BOT_INTENTS = dg.IntentGuilds |
|
BOT_INTENTS = dg.IntentGuilds |
|
||||||
dg.IntentGuildMembers |
|
dg.IntentGuildMembers |
|
||||||
dg.IntentGuildPresences |
|
dg.IntentGuildPresences |
|
||||||
dg.IntentMessageContent |
|
dg.IntentMessageContent |
|
||||||
dg.IntentsGuildMessages
|
dg.IntentsGuildMessages |
|
||||||
|
dg.IntentGuildMessageReactions
|
||||||
)
|
)
|
||||||
|
|
||||||
// basic bot structure containing discordgo connection as well as the command map
|
|
||||||
type bolt struct {
|
type bolt struct {
|
||||||
*dg.Session //holds discordgo internals
|
*dg.Session //holds discordgo internals
|
||||||
commands map[string]Command //maps trigger phrase to command struct for fast lookup
|
commands map[string]Command //maps trigger phrase to command struct for fast lookup
|
||||||
indicator string //the indicator used to detect whether a message is a command
|
indicator string //the indicator used to detect whether a message is a command
|
||||||
logLvl LogLevel //determines how much the bot logs
|
logLvl LogLevel //determines how much the bot logs
|
||||||
|
channels []string //optional list of channels that the bot listens in, all channels are used if list is empty
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bolt interface {
|
type Bolt interface {
|
||||||
@@ -44,7 +47,7 @@ type Bolt interface {
|
|||||||
timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (bool, error)
|
timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new bolt interface
|
// creates and returns a new bolt interface after ensuring token is present and applying option functions
|
||||||
func New(opts ...Option) (Bolt, error) {
|
func New(opts ...Option) (Bolt, error) {
|
||||||
_, check := os.LookupEnv(TOKEN_ENV_VAR)
|
_, check := os.LookupEnv(TOKEN_ENV_VAR)
|
||||||
if !check {
|
if !check {
|
||||||
@@ -74,9 +77,9 @@ func New(opts ...Option) (Bolt, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING
|
// starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING
|
||||||
// call that handles safe shutdown of the bot
|
// call that handles safe shutdown
|
||||||
func (b *bolt) Start() error {
|
func (b *bolt) Start() error {
|
||||||
//register commands and open connection
|
//registers the message handler used internally to detect commands
|
||||||
b.AddHandler(b.messageHandler)
|
b.AddHandler(b.messageHandler)
|
||||||
|
|
||||||
err := b.Open()
|
err := b.Open()
|
||||||
@@ -103,25 +106,43 @@ func (b *bolt) stop() error {
|
|||||||
|
|
||||||
// adds commands to bot command map for use
|
// adds commands to bot command map for use
|
||||||
func (b *bolt) AddCommands(cmd ...Command) {
|
func (b *bolt) AddCommands(cmd ...Command) {
|
||||||
|
//command trigger words are mapped directly to payload functions to prevent
|
||||||
|
//extra loops when searching for the appropriate payload
|
||||||
for _, c := range cmd {
|
for _, c := range cmd {
|
||||||
b.commands[c.Trigger] = c
|
b.commands[c.Trigger] = c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handler function that parses message data and executes any command payloads
|
// internal message handler function. Ensure's the message is a message the bot should interpret
|
||||||
|
// and checks to see if it contains a command then executes the proper payload function
|
||||||
func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
||||||
//get server information
|
//get channel name to, idk if i like this
|
||||||
server, err := s.Guild(msg.GuildID)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to get guild: %e\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
channel, err := s.Channel(msg.ChannelID)
|
channel, err := s.Channel(msg.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to get channel from guild: %e\n", err)
|
log.Printf("failed to get channel from guild: %e\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(b.channels) > 0 {
|
||||||
|
var check bool
|
||||||
|
for _, c := range b.channels {
|
||||||
|
if channel.Name == c {
|
||||||
|
check = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//not a channel we care about
|
||||||
|
if !check {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
server, err := s.Guild(msg.GuildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to get guild: %e\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//if there is no content it is likely an image, gif, or sticker, updating message content for
|
//if there is no content it is likely an image, gif, or sticker, updating message content for
|
||||||
//better logging and to avoid confusion
|
//better logging and to avoid confusion
|
||||||
if len(msg.Content) == 0 {
|
if len(msg.Content) == 0 {
|
||||||
|
62
message.go
62
message.go
@@ -5,36 +5,40 @@ import "fmt"
|
|||||||
//built-in Discord reactions
|
//built-in Discord reactions
|
||||||
type Reaction string
|
type Reaction string
|
||||||
|
|
||||||
|
//a few easy-to-use emojis, Discordgo/Discord API requires them to be saved like this.
|
||||||
const (
|
const (
|
||||||
ReactionThumbsUp Reaction = ":+1:"
|
ReactionThumbsUp Reaction = "👍"
|
||||||
ReactionThumbsDown Reaction = ":-1:"
|
ReactionThumbsDown Reaction = "👎"
|
||||||
ReactionHundred Reaction = ":100:"
|
ReactionHundred Reaction = "💯"
|
||||||
ReactionHeart Reaction = ":heart:"
|
ReactionHeart Reaction = "❤️"
|
||||||
ReactionPinkHeart Reaction = ":pink_heart:"
|
ReactionPinkHeart Reaction = "🩷"
|
||||||
ReactionOrangeHeart Reaction = ":orange_heart:"
|
ReactionOrangeHeart Reaction = "🧡"
|
||||||
ReactionYellowHeart Reaction = ":yellow_heart:"
|
ReactionYellowHeart Reaction = "💛"
|
||||||
ReactionGreenHeart Reaction = ":green_heart:"
|
ReactionGreenHeart Reaction = "💚"
|
||||||
ReactionBlueHeart Reaction = ":blue_heart:"
|
ReactionBlueHeart Reaction = "💙"
|
||||||
ReactionBlackHeart Reaction = ":black_heart:"
|
ReactionBlackHeart Reaction = "🖤"
|
||||||
ReactionPointUp Reaction = ":point_up:"
|
ReactionPointUp Reaction = "☝️"
|
||||||
ReactionPointDown Reaction = ":point_down:"
|
ReactionPointDown Reaction = "👇"
|
||||||
ReactionHotdog Reaction = ":hotdog:"
|
ReactionHotdog Reaction = "🌭"
|
||||||
ReactionDog Reaction = ":dog:"
|
ReactionDog Reaction = "🐶"
|
||||||
ReactionCat Reaction = ":cat:"
|
ReactionCat Reaction = "🐱"
|
||||||
ReactionMonkey Reaction = ":monkey:"
|
ReactionMonkey Reaction = "🐒"
|
||||||
ReactionGiraffe Reaction = ":giraffe:"
|
ReactionGiraffe Reaction = "🦒"
|
||||||
ReactionDuck Reaction = ":duck:"
|
ReactionDuck Reaction = "🦆"
|
||||||
ReactionGoose Reaction = ":goose:"
|
ReactionGoose Reaction = "🪿"
|
||||||
ReactionWatermelon Reaction = ":watermelon:"
|
ReactionWatermelon Reaction = "🍉"
|
||||||
ReactionHoney Reaction = ":honey_pot:"
|
ReactionHoney Reaction = "🍯"
|
||||||
ReactionSandwich Reaction = ":sandwich:"
|
ReactionSandwich Reaction = "🥪"
|
||||||
ReactionPepper Reaction = ":hot_pepper:"
|
ReactionPepper Reaction = "🌶️"
|
||||||
ReactionNoPedestrians Reaction = ":no_pedestrian:"
|
ReactionNoPedestrians Reaction = "🚷"
|
||||||
ReactionExclamation Reaction = ":exclamation:"
|
ReactionExclamation Reaction = "❗"
|
||||||
ReactionDoubleExclamation Reaction = ":bangbang:"
|
ReactionDoubleExclamation Reaction = "‼️"
|
||||||
ReactionSkull Reaction = ":skull:"
|
ReactionSkull Reaction = "💀"
|
||||||
ReactionSpeakingHead Reaction = ":speaking_head:"
|
ReactionSpeakingHead Reaction = "🗣️"
|
||||||
ReactionGreenCheck Reaction = ":white_check_mark:"
|
ReactionGreenCheck Reaction = "✅"
|
||||||
|
ReactionDragon Reaction = "🐉"
|
||||||
|
ReactionLizard Reaction = "🦎"
|
||||||
|
ReactionTakeAKnee Reaction = "🧎♂️➡️"
|
||||||
)
|
)
|
||||||
|
|
||||||
// information about attachments to messages
|
// information about attachments to messages
|
||||||
|
@@ -23,3 +23,10 @@ func WithLogLevel(lvl LogLevel) Option {
|
|||||||
b.logLvl = lvl
|
b.logLvl = lvl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// restrict the channels the bot listens in and responds to, must be a list of channel names
|
||||||
|
func WithListenChannels(channels []string) Option {
|
||||||
|
return func(b *bolt) {
|
||||||
|
b.channels = channels
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user