fromID static method

Future<Channel> fromID(
  1. String id
)

Retrieves a Channel of any type using the channel id

Returns a TextChannel if the type is not defined

Implementation

static Future<Channel> fromID(String id) async {
  http.Response response = await http
      .get(Uri.parse("${constants.apiBaseURL}/channels/$id"), headers: {
    "Authorization": "Bot ${config.botToken}",
    "Content-Type": "application/json",
  });
  if (response.statusCode != 200) {
    throw Exception("Failed to fetch the channel with the ID $id.");
  }
  Map<String, dynamic> decodedResponse = jsonDecode(response.body);
  int type = int.tryParse(decodedResponse["type"].toString()) ??
      ChannelType.guildText;

  Guild parentGuild = await Guild.fromID(decodedResponse["guild_id"] ?? "");
  switch (type) {
    case 0: // guildText
      return TextChannel(decodedResponse["id"], decodedResponse["name"],
          parentGuild, 0, decodedResponse["topic"]);
    default:
      return TextChannel(decodedResponse["id"], decodedResponse["name"],
          parentGuild, 0, decodedResponse["topic"]);
  }
}