I would like to send a message on all text channels of my private discord server using a bot.
I have connected and can have a Session
object but I'm not sure how to get a list of all available channels from the Session
.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Get all channel ID's from dg here
Is this even possible with the discord API?
Don't know if this is still relevant to you, but leaving this here for anyone else wondering.
All you need is access to the discordgo.Session
object, dg
will in your case work just the same.
It is possible, but you have to loop through each Guild (server) the bot has access to. Alternatively, if you have the relevant guild ID or object you can loop through the channels of only that guild.
func spam(s *discordgo.Session) {
// Loop through each guild in the session
for _, guild := range s.State.Guilds {
// Get channels for this guild
channels, _ := s.GuildChannels(guild.ID)
for _, c := range channels {
// Check if channel is a guild text channel and not a voice or DM channel
if c.Type != discordgo.ChannelTypeGuildText {
continue
}
// Send text message
s.ChannelMessageSend(
c.ID,
fmt.Sprintf("testmsg (sorry for spam). Channel name is %q", c.Name),
)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With