Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
d1a5de82fe | |||
5b32d09a27 | |||
cc77adeadc | |||
4e44b972d8 | |||
dc3ef04778 |
7
LICENSE
Normal file
7
LICENSE
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Copyright 2025 jake
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
12
README.md
12
README.md
@@ -51,15 +51,9 @@ func main() {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
_ = b.Start()
|
//start is a blocking call that handles safe-shutdown, all configuration and setup should be done before calling Start()
|
||||||
|
err := b.Start()
|
||||||
//set up safe CTRL-C
|
if err != nil {
|
||||||
sigChannel := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigChannel, syscall.SIGINT)
|
|
||||||
log.Println("bot running")
|
|
||||||
<-sigChannel
|
|
||||||
|
|
||||||
if err := b.Stop(); err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
bolt.go
28
bolt.go
@@ -4,8 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
dg "github.com/bwmarrin/discordgo"
|
dg "github.com/bwmarrin/discordgo"
|
||||||
@@ -30,7 +32,7 @@ type bolt struct {
|
|||||||
|
|
||||||
type Bolt interface {
|
type Bolt interface {
|
||||||
Start() error
|
Start() error
|
||||||
Stop() error
|
stop() error
|
||||||
AddCommands(cmd Command)
|
AddCommands(cmd Command)
|
||||||
messageHandler(s *dg.Session, msg *dg.MessageCreate)
|
messageHandler(s *dg.Session, msg *dg.MessageCreate)
|
||||||
createReply(content, message, channel, guild string) *dg.MessageSend
|
createReply(content, message, channel, guild string) *dg.MessageSend
|
||||||
@@ -68,16 +70,32 @@ func New(opts ...Option) *bolt {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// adds command handler and starts the bot
|
// adds command handler and starts the bot, this is a BLOCKING CALL
|
||||||
|
|
||||||
|
// starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING
|
||||||
|
// call and handles safe shutdown of the bot
|
||||||
func (b *bolt) Start() error {
|
func (b *bolt) Start() error {
|
||||||
//register commands and open connection
|
//register commands and open connection
|
||||||
b.AddHandler(b.messageHandler)
|
b.AddHandler(b.messageHandler)
|
||||||
|
|
||||||
return b.Open()
|
err := b.Open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sigChannel := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChannel, syscall.SIGINT)
|
||||||
|
<-sigChannel
|
||||||
|
|
||||||
|
if err := b.stop(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// stops the bot
|
// stops the bot
|
||||||
func (b *bolt) Stop() error {
|
func (b *bolt) stop() error {
|
||||||
return b.Close()
|
return b.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +184,7 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
|||||||
res, err := run.Payload(Message{
|
res, err := run.Payload(Message{
|
||||||
Author: msg.Author.Username,
|
Author: msg.Author.Username,
|
||||||
Words: words,
|
Words: words,
|
||||||
Message: msg.Content,
|
Content: msg.Content,
|
||||||
Channel: channel.Name,
|
Channel: channel.Name,
|
||||||
Server: server.Name,
|
Server: server.Name,
|
||||||
})
|
})
|
||||||
|
@@ -12,13 +12,13 @@ type Command struct {
|
|||||||
|
|
||||||
// payload function type handling commands. The returned error is parsed and, if no error,
|
// payload function type handling commands. The returned error is parsed and, if no error,
|
||||||
// is detected then the response string (res) will be sent in response to the command message
|
// is detected then the response string (res) will be sent in response to the command message
|
||||||
type Payload func(msg Message) (res string, err error)
|
type Payload func(msg Message) (string, error)
|
||||||
|
|
||||||
// contains the basic information needed for a message command
|
// contains the basic information needed for a message command
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Author string //username of message author
|
Author string //username of message author
|
||||||
Words []string //words from message split on whitespace
|
Words []string //words from message split on whitespace
|
||||||
Message string //entire message content
|
Content string //entire message content
|
||||||
Channel string //channel message was sent in
|
Channel string //channel message was sent in
|
||||||
Server string //guild message was sent in
|
Server string //guild message was sent in
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user