Minor refactor

This commit is contained in:
Sebastian Cabrera 2024-01-16 16:58:02 -05:00
parent ba31086469
commit 3ad90eddbf
2 changed files with 6 additions and 5 deletions

View file

@ -13,6 +13,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
// If the message is "ping" reply with "Pong!"
if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!")

10
main.go
View file

@ -25,20 +25,20 @@ func init() {
func main() {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + token)
bot, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Error creating Discord session,", err)
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreate)
bot.AddHandler(messageCreate)
// In this example, we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages
bot.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
err = bot.Open()
if err != nil {
fmt.Println("Error opening connection,", err)
return
@ -51,5 +51,5 @@ func main() {
<-sc
// Cleanly close down the Discord session.
dg.Close()
bot.Close()
}