doukui4836 2018-04-08 07:21
浏览 1219
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • doutan5798 2018-08-08 07:53
    关注

    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),
                )
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路