This commit is contained in:
Bruce Marriner 2016-06-17 15:44:01 -05:00
parent ab465c38c4
commit fb663ac348
9 changed files with 47 additions and 32 deletions

View file

@ -64,7 +64,7 @@ func main() {
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
@ -108,7 +108,7 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
for _, channel := range event.Guild.Channels { for _, channel := range event.Guild.Channels {
if channel.ID == event.Guild.ID { if channel.ID == event.Guild.ID {
s.ChannelMessageSend(channel.ID, "Airhorn is ready! Type !airhorn while in a voice channel to play a sound.") _, _ = s.ChannelMessageSend(channel.ID, "Airhorn is ready! Type !airhorn while in a voice channel to play a sound.")
return return
} }
} }
@ -166,7 +166,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 {
@ -174,13 +174,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
} }

View file

@ -7,14 +7,14 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line options
var ( var (
Email string Email string
Password string Password string
Token string Token string
AppName string AppName string
ConvToken string DeleteID string
DeleteID string ListOnly bool
ListOnly bool
) )
func init() { func init() {
@ -30,6 +30,7 @@ func init() {
func main() { func main() {
var err error
// Create a new Discord session using the provided login information. // Create a new Discord session using the provided login information.
dg, err := discordgo.New(Email, Password, Token) dg, err := discordgo.New(Email, Password, Token)
if err != nil { if err != nil {
@ -40,8 +41,8 @@ func main() {
// If -l set, only display a list of existing applications // If -l set, only display a list of existing applications
// for the given account. // for the given account.
if ListOnly { if ListOnly {
aps, err := dg.Applications() aps, err2 := dg.Applications()
if err != nil { if err2 != nil {
fmt.Println("error fetching applications,", err) fmt.Println("error fetching applications,", err)
return return
} }
@ -58,7 +59,7 @@ func main() {
// if -d set, delete the given Application // if -d set, delete the given Application
if DeleteID != "" { if DeleteID != "" {
err := dg.ApplicationDelete(DeleteID) err = dg.ApplicationDelete(DeleteID)
if err != nil { if err != nil {
fmt.Println("error deleting application,", err) fmt.Println("error deleting application,", err)
} }

View file

@ -10,6 +10,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line parameters
var ( var (
Email string Email string
Password string Password string
@ -63,7 +64,7 @@ func changeAvatar(s *discordgo.Session) {
base64 := base64.StdEncoding.EncodeToString(img) base64 := base64.StdEncoding.EncodeToString(img)
avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), string(base64)) avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64)
_, err = s.UserUpdate("", "", BotUsername, avatar, "") _, err = s.UserUpdate("", "", BotUsername, avatar, "")
if err != nil { if err != nil {

View file

@ -10,11 +10,12 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line parameters
var ( var (
Email string Email string
Password string Password string
Token string Token string
Url string URL string
BotID string BotID string
BotUsername string BotUsername string
) )
@ -24,7 +25,7 @@ func init() {
flag.StringVar(&Email, "e", "", "Account Email") flag.StringVar(&Email, "e", "", "Account Email")
flag.StringVar(&Password, "p", "", "Account Password") flag.StringVar(&Password, "p", "", "Account Password")
flag.StringVar(&Token, "t", "", "Account Token") flag.StringVar(&Token, "t", "", "Account Token")
flag.StringVar(&Url, "l", "http://bwmarrin.github.io/discordgo/img/discordgo.png", "Link to the avatar image") flag.StringVar(&URL, "l", "http://bwmarrin.github.io/discordgo/img/discordgo.png", "Link to the avatar image")
flag.Parse() flag.Parse()
} }
@ -57,13 +58,15 @@ func main() {
// Helper function to change the avatar // Helper function to change the avatar
func changeAvatar(s *discordgo.Session) { func changeAvatar(s *discordgo.Session) {
resp, err := http.Get(Url) resp, err := http.Get(URL)
if err != nil { if err != nil {
fmt.Println("Error retrieving the file, ", err) fmt.Println("Error retrieving the file, ", err)
return return
} }
defer resp.Body.Close() defer func() {
_ = resp.Body.Close()
}()
img, err := ioutil.ReadAll(resp.Body) img, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -73,7 +76,7 @@ func changeAvatar(s *discordgo.Session) {
base64 := base64.StdEncoding.EncodeToString(img) base64 := base64.StdEncoding.EncodeToString(img)
avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), string(base64)) avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(img), base64)
_, err = s.UserUpdate("", "", BotUsername, avatar, "") _, err = s.UserUpdate("", "", BotUsername, avatar, "")
if err != nil { if err != nil {

View file

@ -7,6 +7,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line parameters
var ( var (
Email string Email string
Password string Password string

View file

@ -8,11 +8,11 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line parameters
var ( var (
Email string Email string
Password string Password string
Token string Token string
BotID string
) )
func init() { func init() {
@ -37,7 +37,11 @@ func main() {
dg.AddHandler(messageCreate) dg.AddHandler(messageCreate)
// Open the websocket and begin listening. // Open the websocket and begin listening.
dg.Open() err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
// Simple way to keep program running until CTRL-C is pressed. // Simple way to keep program running until CTRL-C is pressed.

View file

@ -7,6 +7,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line parameters
var ( var (
Email string Email string
Password string Password string
@ -44,7 +45,11 @@ func main() {
dg.AddHandler(messageCreate) dg.AddHandler(messageCreate)
// Open the websocket and begin listening. // Open the websocket and begin listening.
dg.Open() err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
// Simple way to keep program running until CTRL-C is pressed. // Simple way to keep program running until CTRL-C is pressed.
@ -63,11 +68,11 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// If the message is "ping" reply with "Pong!" // If the message is "ping" reply with "Pong!"
if m.Content == "ping" { if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!") _, _ = s.ChannelMessageSend(m.ChannelID, "Pong!")
} }
// If the message is "pong" reply with "Ping!" // If the message is "pong" reply with "Ping!"
if m.Content == "pong" { if m.Content == "pong" {
s.ChannelMessageSend(m.ChannelID, "Ping!") _, _ = s.ChannelMessageSend(m.ChannelID, "Ping!")
} }
} }

View file

@ -107,8 +107,8 @@ func (s *Session) request(method, urlStr, contentType string, b []byte) (respons
return return
} }
defer func() { defer func() {
err := resp.Body.Close() err2 := resp.Body.Close()
if err != nil { if err2 != nil {
log.Println("error closing resp body") log.Println("error closing resp body")
} }
}() }()

View file

@ -288,15 +288,15 @@ func (s *Session) onEvent(messageType int, message []byte) {
// If this is a compressed message, uncompress it. // If this is a compressed message, uncompress it.
if messageType == websocket.BinaryMessage { if messageType == websocket.BinaryMessage {
z, err := zlib.NewReader(reader) z, err2 := zlib.NewReader(reader)
if err != nil { if err2 != nil {
s.log(LogError, "error uncompressing websocket message, %s", err) s.log(LogError, "error uncompressing websocket message, %s", err)
return return
} }
defer func() { defer func() {
err := z.Close() err3 := z.Close()
if err != nil { if err3 != nil {
s.log(LogWarning, "error closing zlib, %s", err) s.log(LogWarning, "error closing zlib, %s", err)
} }
}() }()