|
@@ -6,7 +6,9 @@ import (
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"os"
|
|
|
+ "os/signal"
|
|
|
"strings"
|
|
|
+ "syscall"
|
|
|
"time"
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
@@ -21,6 +23,7 @@ var token string
|
|
|
var buffer = make([][]byte, 0)
|
|
|
|
|
|
func main() {
|
|
|
+
|
|
|
if token == "" {
|
|
|
fmt.Println("No token provided. Please run: airhorn -t <bot token>")
|
|
|
return
|
|
@@ -56,21 +59,37 @@ func main() {
|
|
|
fmt.Println("Error opening Discord session: ", err)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
fmt.Println("Airhorn is now running. Press CTRL-C to exit.")
|
|
|
-
|
|
|
- <-make(chan struct{})
|
|
|
- return
|
|
|
+ sc := make(chan os.Signal, 1)
|
|
|
+ signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
|
+ <-sc
|
|
|
+
|
|
|
+
|
|
|
+ dg.Close()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|
|
+
|
|
|
|
|
|
- _ = s.UpdateStatus(0, "!airhorn")
|
|
|
+ s.UpdateStatus(0, "!airhorn")
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if m.Author.ID == s.State.User.ID {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
if strings.HasPrefix(m.Content, "!airhorn") {
|
|
|
+
|
|
|
|
|
|
c, err := s.State.Channel(m.ChannelID)
|
|
|
if err != nil {
|
|
@@ -85,7 +104,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
for _, vs := range g.VoiceStates {
|
|
|
if vs.UserID == m.Author.ID {
|
|
|
err = playSound(s, g.ID, vs.ChannelID)
|
|
@@ -102,6 +121,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
|
|
|
|
|
|
|
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
|
|
|
+
|
|
|
if event.Guild.Unavailable {
|
|
|
return
|
|
|
}
|
|
@@ -116,8 +136,8 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
|
|
|
|
|
|
|
|
|
func loadSound() error {
|
|
|
- file, err := os.Open("airhorn.dca")
|
|
|
|
|
|
+ file, err := os.Open("airhorn.dca")
|
|
|
if err != nil {
|
|
|
fmt.Println("Error opening dca file :", err)
|
|
|
return err
|
|
@@ -160,6 +180,7 @@ func loadSound() error {
|
|
|
|
|
|
|
|
|
func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
|
|
|
+
|
|
|
|
|
|
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
|
|
|
if err != nil {
|
|
@@ -170,7 +191,7 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
|
|
|
|
|
- _ = vc.Speaking(true)
|
|
|
+ vc.Speaking(true)
|
|
|
|
|
|
|
|
|
for _, buff := range buffer {
|
|
@@ -178,13 +199,13 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
|
|
|
}
|
|
|
|
|
|
|
|
|
- _ = vc.Speaking(false)
|
|
|
+ vc.Speaking(false)
|
|
|
|
|
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
|
|
|
|
|
- _ = vc.Disconnect()
|
|
|
+ vc.Disconnect()
|
|
|
|
|
|
return nil
|
|
|
}
|