Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
3bf763f196
|
|||
8f9205fbf0
|
|||
e1bae3edea
|
|||
34fdf453c1
|
|||
113c6927cb
|
@@ -58,7 +58,10 @@ import (
|
||||
func main() {
|
||||
//bolt defaults the command indicator to '.' however that can be changed with the options
|
||||
//Example: bolt.New(bolt.WithIndicator('!')) would support commands like !ping
|
||||
b := bolt.New(bolt.WithLogLevel(bolt.LogLevelCmd))
|
||||
b, err := bolt.New(bolt.WithLogLevel(bolt.LogLevelCmd))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
b.AddCommands(
|
||||
// basic ping pong command, .ping can be run at anytime by anyone and will reply "pong"
|
||||
@@ -95,11 +98,12 @@ func main() {
|
||||
)
|
||||
|
||||
//start is a blocking call that handles safe-shutdown, all configuration and setup should be done before calling Start()
|
||||
err := b.Start()
|
||||
err = b.Start()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Development
|
||||
|
47
bolt.go
47
bolt.go
@@ -20,7 +20,8 @@ const (
|
||||
dg.IntentGuildMembers |
|
||||
dg.IntentGuildPresences |
|
||||
dg.IntentMessageContent |
|
||||
dg.IntentsGuildMessages
|
||||
dg.IntentsGuildMessages |
|
||||
dg.IntentGuildMessageReactions
|
||||
)
|
||||
|
||||
// basic bot structure containing discordgo connection as well as the command map
|
||||
@@ -44,20 +45,16 @@ type Bolt interface {
|
||||
timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (bool, error)
|
||||
}
|
||||
|
||||
// setup
|
||||
func init() {
|
||||
//validate environment variables
|
||||
// create a new bolt interface
|
||||
func New(opts ...Option) (Bolt, error) {
|
||||
_, check := os.LookupEnv(TOKEN_ENV_VAR)
|
||||
if !check {
|
||||
log.Fatalf("the %s environment variable must be set", TOKEN_ENV_VAR)
|
||||
}
|
||||
return nil, fmt.Errorf("environment variable %s must be set", TOKEN_ENV_VAR)
|
||||
}
|
||||
|
||||
// create a new bolt interface
|
||||
func New(opts ...Option) Bolt {
|
||||
bot, err := dg.New(fmt.Sprintf("Bot %s", os.Getenv(TOKEN_ENV_VAR)))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, fmt.Errorf("failed to create Discord session: %e", err)
|
||||
}
|
||||
bot.Identify.Intents = BOT_INTENTS
|
||||
|
||||
@@ -74,7 +71,7 @@ func New(opts ...Option) Bolt {
|
||||
opt(b)
|
||||
}
|
||||
|
||||
return b
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING
|
||||
@@ -85,7 +82,7 @@ func (b *bolt) Start() error {
|
||||
|
||||
err := b.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to open websocket connection with Discord: %e", err)
|
||||
}
|
||||
|
||||
//safe shutdown handler
|
||||
@@ -117,12 +114,12 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
||||
//get server information
|
||||
server, err := s.Guild(msg.GuildID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Printf("failed to get guild: %e\n", err)
|
||||
return
|
||||
}
|
||||
channel, err := s.Channel(msg.ChannelID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Printf("failed to get channel from guild: %e\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -132,20 +129,20 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
||||
msg.Content = "[Embedded Content]"
|
||||
}
|
||||
|
||||
if b.logLvl == LogLevelAll {
|
||||
//log message
|
||||
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
|
||||
}
|
||||
|
||||
//the bot will ignore it's own messages to prevent command loops
|
||||
if msg.Author.ID == s.State.User.ID {
|
||||
if b.logLvl == LogLevelCmd {
|
||||
//log commands
|
||||
//log command responses
|
||||
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if b.logLvl == LogLevelAll {
|
||||
//log message
|
||||
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
|
||||
}
|
||||
|
||||
//does the message have the command indicator
|
||||
lg := len(b.indicator)
|
||||
if msg.Content[:lg] == b.indicator {
|
||||
@@ -173,7 +170,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
|
||||
//has command met its timeout requirements
|
||||
tc, err := b.timeoutCheck(msg, s, run)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to calculate timeout for %s\n%e", run.Trigger, err)
|
||||
}
|
||||
if !tc {
|
||||
return nil
|
||||
@@ -183,7 +180,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
|
||||
if run.Roles != nil {
|
||||
check, err := b.roleCheck(msg, s, run)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to perform permission checks for %s\n%e", run.Trigger, err)
|
||||
}
|
||||
if !check {
|
||||
return nil
|
||||
@@ -226,7 +223,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
|
||||
//run command payload
|
||||
err = run.Payload(plMsg)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to execute payload function: %e", err)
|
||||
}
|
||||
|
||||
//update run time
|
||||
@@ -279,7 +276,7 @@ func (b *bolt) roleCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (boo
|
||||
//get role name from ID
|
||||
n, err := s.State.Role(msg.GuildID, r)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, fmt.Errorf("failed to get role from ID %s\n%e", msg.GuildID, err)
|
||||
}
|
||||
//does this role exist in command roles
|
||||
check := slices.Contains(run.Roles, n.Name)
|
||||
@@ -294,7 +291,7 @@ func (b *bolt) roleCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (boo
|
||||
reply := b.createReply("you do not have permissions to run that command", msg.ID, msg.ChannelID, msg.GuildID)
|
||||
_, err := s.ChannelMessageSendComplex(msg.ChannelID, reply)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, fmt.Errorf("failed to send permission response: %e", err)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@@ -309,7 +306,7 @@ func (b *bolt) timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (
|
||||
reply := b.createReply(fmt.Sprintf("that command cannot be run for another %s", b.getRemainingTimeout(wait)), msg.ID, msg.ChannelID, msg.GuildID)
|
||||
_, err := s.ChannelMessageSendComplex(msg.ChannelID, reply)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, fmt.Errorf("failed to send timeout response: %e", err)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
21
message.go
21
message.go
@@ -8,12 +8,33 @@ type Reaction string
|
||||
const (
|
||||
ReactionThumbsUp Reaction = ":+1:"
|
||||
ReactionThumbsDown Reaction = ":-1:"
|
||||
ReactionHundred Reaction = ":100:"
|
||||
ReactionHeart Reaction = ":heart:"
|
||||
ReactionPinkHeart Reaction = ":pink_heart:"
|
||||
ReactionOrangeHeart Reaction = ":orange_heart:"
|
||||
ReactionYellowHeart Reaction = ":yellow_heart:"
|
||||
ReactionGreenHeart Reaction = ":green_heart:"
|
||||
ReactionBlueHeart Reaction = ":blue_heart:"
|
||||
ReactionBlackHeart Reaction = ":black_heart:"
|
||||
ReactionPointUp Reaction = ":point_up:"
|
||||
ReactionPointDown Reaction = ":point_down:"
|
||||
ReactionHotdog Reaction = ":hotdog:"
|
||||
ReactionDog Reaction = ":dog:"
|
||||
ReactionCat Reaction = ":cat:"
|
||||
ReactionMonkey Reaction = ":monkey:"
|
||||
ReactionGiraffe Reaction = ":giraffe:"
|
||||
ReactionDuck Reaction = ":duck:"
|
||||
ReactionGoose Reaction = ":goose:"
|
||||
ReactionWatermelon Reaction = ":watermelon:"
|
||||
ReactionHoney Reaction = ":honey_pot:"
|
||||
ReactionSandwich Reaction = ":sandwich:"
|
||||
ReactionPepper Reaction = ":hot_pepper:"
|
||||
ReactionNoPedestrians Reaction = ":no_pedestrian:"
|
||||
ReactionExclamation Reaction = ":exclamation:"
|
||||
ReactionDoubleExclamation Reaction = ":bangbang:"
|
||||
ReactionSkull Reaction = ":skull:"
|
||||
ReactionSpeakingHead Reaction = ":speaking_head:"
|
||||
ReactionGreenCheck Reaction = ":white_check_mark:"
|
||||
)
|
||||
|
||||
// information about attachments to messages
|
||||
|
Reference in New Issue
Block a user