mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 08:32:48 +00:00
feat(Bot): add restart command - update discordgo
This commit is contained in:
6
server/Gopkg.lock
generated
6
server/Gopkg.lock
generated
@@ -18,12 +18,12 @@
|
||||
version = "v1.1.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:d87c9221a974263e3b369bfd3513707b2a53e27a6cd799d472f94dc6a6157e59"
|
||||
digest = "1:99c79dc08249968d203bf4210220c01bd8250301600f5daf0a81f4c5f184f8d9"
|
||||
name = "github.com/bwmarrin/discordgo"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "ed4d6904961d1688b3f5601b3d73e95a71046734"
|
||||
version = "v0.20.3"
|
||||
revision = "befbea878e0f1d0bb21be6723b6cf6db2689f6a8"
|
||||
version = "v0.22.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:3ee1d175a75b911a659fbd860060874c4f503e793c5870d13e5a0ede529a63cf"
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/bwmarrin/discordgo"
|
||||
version = "0.20.1"
|
||||
version = "0.22.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/gin-gonic/gin"
|
||||
|
||||
@@ -1,37 +1,23 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/mgerb/go-discord-bot/server/bothandlers"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var session *discordgo.Session
|
||||
|
||||
// Start the bot
|
||||
func Start(token string) *discordgo.Session {
|
||||
// initialize connection
|
||||
session := connect(token)
|
||||
|
||||
// add bot handlers
|
||||
addHandler(session, bothandlers.SoundsHandler)
|
||||
addHandler(session, bothandlers.LoggerHandler)
|
||||
|
||||
// start listening for commands
|
||||
startListener(session)
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
// GetSession - get current discord session
|
||||
func GetSession() *discordgo.Session {
|
||||
return session
|
||||
}
|
||||
// keep reference to discord session
|
||||
var _session *discordgo.Session
|
||||
var _token string
|
||||
var _sc chan os.Signal
|
||||
|
||||
// SendEmbeddedNotification - sends notification to default room
|
||||
func SendEmbeddedNotification(title, description string) {
|
||||
if session == nil || config.Config.DefaultRoomID == "" {
|
||||
if _session == nil || config.Config.DefaultRoomID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -41,17 +27,70 @@ func SendEmbeddedNotification(title, description string) {
|
||||
Description: description,
|
||||
}
|
||||
|
||||
session.ChannelMessageSendEmbed(config.Config.DefaultRoomID, embed)
|
||||
_session.ChannelMessageSendEmbed(config.Config.DefaultRoomID, embed)
|
||||
}
|
||||
|
||||
func addHandler(session *discordgo.Session, handler interface{}) {
|
||||
session.AddHandler(handler)
|
||||
// Start bot - this is a blocking function
|
||||
func Start(token string) {
|
||||
_token = token
|
||||
|
||||
// initialize connection
|
||||
_session = connect(token)
|
||||
|
||||
// add bot handlers
|
||||
_session.AddHandler(bothandlers.SoundsHandler)
|
||||
_session.AddHandler(bothandlers.LoggerHandler)
|
||||
_session.AddHandler(func(_s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Content == config.Config.BotPrefix+"restart" {
|
||||
restart()
|
||||
}
|
||||
})
|
||||
|
||||
// start listening for commands
|
||||
// Open the websocket and begin listening.
|
||||
err := _session.Open()
|
||||
|
||||
if err != nil {
|
||||
log.Error("error opening connection,", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("Bot is now running...")
|
||||
}
|
||||
|
||||
func Stop() {
|
||||
_session.Close()
|
||||
}
|
||||
|
||||
func restart() {
|
||||
if _token == "" {
|
||||
log.Warn("Unable to restart - token nil")
|
||||
return
|
||||
}
|
||||
|
||||
for _, vc := range _session.VoiceConnections {
|
||||
vc.Disconnect()
|
||||
}
|
||||
|
||||
// https://github.com/bwmarrin/discordgo/issues/759
|
||||
// Might need to use this to reconnect
|
||||
// err := _session.CloseWithCode(1012)
|
||||
err := _session.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Second * 5)
|
||||
|
||||
bothandlers.ActiveConnections = map[string]*bothandlers.AudioConnection{}
|
||||
|
||||
Start(_token)
|
||||
}
|
||||
|
||||
func connect(token string) *discordgo.Session {
|
||||
// Create a new Discord session using the provided bot token.
|
||||
var err error
|
||||
session, err = discordgo.New("Bot " + token)
|
||||
session, err := discordgo.New("Bot " + token)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -70,21 +109,3 @@ func connect(token string) *discordgo.Session {
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
func startListener(session *discordgo.Session) {
|
||||
// start new non blocking go routine
|
||||
go func() {
|
||||
// Open the websocket and begin listening.
|
||||
err := session.Open()
|
||||
if err != nil {
|
||||
log.Error("error opening connection,", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("Bot is now running...")
|
||||
|
||||
// Simple way to keep program running until CTRL-C is pressed.
|
||||
<-make(chan struct{})
|
||||
return
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func LoggerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
Timestamp: timestamp,
|
||||
EditedTimestamp: editedTimestamp,
|
||||
MentionRoles: strings.Join(m.Message.MentionRoles, ","),
|
||||
Tts: m.Message.Tts,
|
||||
TTS: m.Message.TTS,
|
||||
MentionEveryone: m.Message.MentionEveryone,
|
||||
UserID: m.Author.ID,
|
||||
Attachments: attachments,
|
||||
|
||||
@@ -109,6 +109,10 @@ func (conn *AudioConnection) handleMessage(m *discordgo.MessageCreate) {
|
||||
case "random":
|
||||
conn.PlayRandomAudio(m, nil)
|
||||
|
||||
// restart command handled in bot file - needed here to break out
|
||||
case "restart":
|
||||
break
|
||||
|
||||
default:
|
||||
conn.PlayAudio(command, m, nil)
|
||||
}
|
||||
@@ -375,7 +379,7 @@ loop:
|
||||
for {
|
||||
|
||||
select {
|
||||
// grab incomming audio
|
||||
// grab incoming audio
|
||||
case opusChannel, ok := <-conn.VoiceConnection.OpusRecv:
|
||||
if !ok {
|
||||
continue
|
||||
|
||||
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/mgerb/go-discord-bot/server/bot"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
@@ -33,9 +35,19 @@ func init() {
|
||||
|
||||
func main() {
|
||||
|
||||
// start the bot
|
||||
bot.Start(config.Config.Token)
|
||||
|
||||
// start the web server
|
||||
webserver.Start()
|
||||
go func() {
|
||||
webserver.Start()
|
||||
}()
|
||||
|
||||
// start the bot
|
||||
go func() {
|
||||
bot.Start(config.Config.Token)
|
||||
}()
|
||||
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||
<-sc
|
||||
|
||||
bot.Stop()
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type Message struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
EditedTimestamp time.Time `json:"edited_timestamp"`
|
||||
MentionRoles string `json:"mention_roles"`
|
||||
Tts bool `json:"tts"`
|
||||
TTS bool `json:"tts"`
|
||||
MentionEveryone bool `json:"mention_everyone"`
|
||||
User User `json:"user"`
|
||||
UserID string `json:"user_id"`
|
||||
|
||||
Reference in New Issue
Block a user