Bot not responding to messages #1280
-
Hey, package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
)
func main() {
// Create a new Discord session using the provided bot token.
dg, 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)
// Just like the ping pong example, we only care about receiving message
// events in this example.
dg.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
//
// It is called whenever a message is created but only when it's sent through a
// server as we did not request IntentsDirectMessages.
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
}
// In this example, we only care about messages that are "ping".
if m.Content != "ping" {
return
}
// We create the private channel with the user who sent the message.
channel, err := s.UserChannelCreate(m.Author.ID)
if err != nil {
// If an error occurred, we failed to create the channel.
//
// Some common causes are:
// 1. We don't share a server with the user (not possible here).
// 2. We opened enough DM channels quickly enough for Discord to
// label us as abusing the endpoint, blocking us from opening
// new ones.
fmt.Println("error creating channel:", err)
s.ChannelMessageSend(
m.ChannelID,
"Something went wrong while sending the DM!",
)
return
}
// Then we send the message through the channel we created.
_, err = s.ChannelMessageSend(channel.ID, "Pong!")
if err != nil {
// If an error occurred, we failed to send the message.
//
// It may occur either when we do not share a server with the
// user (highly unlikely as we just received a message) or
// the user disabled DM in their settings (more likely).
fmt.Println("error sending DM message:", err)
s.ChannelMessageSend(
m.ChannelID,
"Failed to send you a DM. "+
"Did you disable DM in your privacy settings?",
)
}
} This is copied from the examples but when for some reason i want to get it just the token to be on the file for me it doesnt work, im running this when its built |
Beta Was this translation helpful? Give feedback.
Answered by
FedorLap2006
Nov 20, 2022
Replies: 1 comment 5 replies
-
Bummer, I forgot about that again.
Nowdays the bots require also another intent: |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
badonyt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bummer, I forgot about that again.
Nowdays the bots require also another intent:
discordgo.IntentMessageContent
.You would need to change this line to
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
.