Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of all channels using discordgo?

Tags:

go

discord

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?

like image 863
Increasingly Idiotic Avatar asked Apr 08 '18 07:04

Increasingly Idiotic


1 Answers

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),
            )
        }
    }
}
like image 86
haraldfw Avatar answered Nov 30 '22 17:11

haraldfw