Petr Kotáb
Petr Kotáb

Reputation: 38

client.GetChannel(%channelID%) return null

Im creting discord bot via discord.Net v 1.0.2

And I want to get specific channel from my server Im trying to get that channel via Client.GetChannel(%channelID%) (Client is property of type DiscordSocketClient in my bot class) but that method return me only null and I just cant figure out why. I searched on lots of similiar post on internet but nothing work. There is start method of my bot and method where Im trying to get channel:

public async Task StartAsync()
{
    await Client.StartAsync();
    Client.MessageReceived += Client_MessageReceived;
    Client.Connected += Client_Connected;
    Logger.WriteLog("Bot started");
    await Task.Delay(-1);
}

private async Task Client_Connected()
{
    var channel = Client.GetChannel(414543303187496962);
}

Upvotes: 1

Views: 2574

Answers (1)

WQYeo
WQYeo

Reputation: 4056

Use Client.Ready event Handler instead, and make sure that the bot is indeed in that specific channel, and it should work this time.

So it would look like this:

public async Task StartAsync()
{
    await Client.StartAsync();
    Client.MessageReceived += Client_MessageReceived;
    Client.Ready += Client_Ready;
    Logger.WriteLog("Bot started");
    await Task.Delay(-1);
}

private async Task Client_Ready()
{
    var channel = Client.GetChannel(414543303187496962);
}

Upvotes: 2

Related Questions