Clean up comments, better blocking code.

This commit is contained in:
Bruce 2017-04-13 15:22:58 +00:00
parent 2b282540ef
commit 1c719249a1

View file

@ -6,7 +6,9 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -21,6 +23,7 @@ var token string
var buffer = make([][]byte, 0) var buffer = make([][]byte, 0)
func main() { func main() {
if token == "" { if token == "" {
fmt.Println("No token provided. Please run: airhorn -t <bot token>") fmt.Println("No token provided. Please run: airhorn -t <bot token>")
return return
@ -56,21 +59,37 @@ func main() {
fmt.Println("Error opening Discord session: ", err) fmt.Println("Error opening Discord session: ", err)
} }
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Airhorn is now running. Press CTRL-C to exit.") fmt.Println("Airhorn is now running. Press CTRL-C to exit.")
// Simple way to keep program running until CTRL-C is pressed. sc := make(chan os.Signal, 1)
<-make(chan struct{}) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
return <-sc
// Cleanly close down the Discord session.
dg.Close()
} }
// This function will be called (due to AddHandler above) when the bot receives
// the "ready" event from Discord.
func ready(s *discordgo.Session, event *discordgo.Ready) { func ready(s *discordgo.Session, event *discordgo.Ready) {
// Set the playing status. // Set the playing status.
_ = s.UpdateStatus(0, "!airhorn") s.UpdateStatus(0, "!airhorn")
} }
// This function will be called (due to AddHandler above) every time a new // This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to. // message is created on any channel that the autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
// check if the message is "!airhorn"
if strings.HasPrefix(m.Content, "!airhorn") { if strings.HasPrefix(m.Content, "!airhorn") {
// Find the channel that the message came from. // Find the channel that the message came from.
c, err := s.State.Channel(m.ChannelID) c, err := s.State.Channel(m.ChannelID)
if err != nil { if err != nil {
@ -85,7 +104,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
// Look for the message sender in that guilds current voice states. // Look for the message sender in that guild's current voice states.
for _, vs := range g.VoiceStates { for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID { if vs.UserID == m.Author.ID {
err = playSound(s, g.ID, vs.ChannelID) err = playSound(s, g.ID, vs.ChannelID)
@ -102,6 +121,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// This function will be called (due to AddHandler above) every time a new // This function will be called (due to AddHandler above) every time a new
// guild is joined. // guild is joined.
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) { func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
if event.Guild.Unavailable { if event.Guild.Unavailable {
return return
} }
@ -116,8 +136,8 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
// loadSound attempts to load an encoded sound file from disk. // loadSound attempts to load an encoded sound file from disk.
func loadSound() error { func loadSound() error {
file, err := os.Open("airhorn.dca")
file, err := os.Open("airhorn.dca")
if err != nil { if err != nil {
fmt.Println("Error opening dca file :", err) fmt.Println("Error opening dca file :", err)
return err return err
@ -160,6 +180,7 @@ func loadSound() error {
// playSound plays the current buffer to the provided channel. // playSound plays the current buffer to the provided channel.
func playSound(s *discordgo.Session, guildID, channelID string) (err error) { func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
// Join the provided voice channel. // Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true) vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil { if err != nil {
@ -170,7 +191,7 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)
// Start speaking. // Start speaking.
_ = vc.Speaking(true) vc.Speaking(true)
// Send the buffer data. // Send the buffer data.
for _, buff := range buffer { for _, buff := range buffer {
@ -178,13 +199,13 @@ func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
} }
// Stop speaking // Stop speaking
_ = vc.Speaking(false) vc.Speaking(false)
// Sleep for a specificed amount of time before ending. // Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)
// Disconnect from the provided voice channel. // Disconnect from the provided voice channel.
_ = vc.Disconnect() vc.Disconnect()
return nil return nil
} }