如何使用discordgo获取所有频道的列表?

Inc*_*tic 6 go discord

我想使用机器人在我的私人discord服务器的所有文本频道上发送消息.

我已连接并且可以有一个Session对象,但我不知道如何从中获取所有可用频道的列表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
Run Code Online (Sandbox Code Playgroud)

这与discord API有关吗?

小智 4

不知道这是否仍然与您相关,但将其留在这里供其他想知道的人使用。

您所需要的只是访问该discordgo.Session对象,dg在您的情况下,它的工作原理是一样的。

这是可能的,但您必须循环访问机器人可以访问的每个公会(服务器)。或者,如果您有相关的公会 ID 或对象,您可以仅循环访问该公会的频道。

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),
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)