Reputation: 23
I am coding a discord bot, and I want it to send a message to a server default channel whenever the bot joins a new server.
Here is my code
client.JoinedGuild += async guild =>
{
var channel = guild.DefaultChannel;
await channel.SendMessageAsync("test");
};
The error occurs on channel.SendMessageAsync
, and when I debugged the program, it keeps showing that channel
is null even though I do have a default channel in my server.
Upvotes: 2
Views: 1418
Reputation: 4056
A discord update months ago removed defaultChannel
property. Which means that servers now do not need to have a default channel at all.
(You can even delete all channel in your server!)
Which also means Guild.DefaultChannel
property will not work as intended anymore.
(Correct me if I am wrong, but if a server still has a #general
channel, the property will work as intended.)
Discord.NET v2+ have a working DefaultChannel
property. (It uses its own set of algorithm to determine which would be the default channel for the guild.)
The source code for the algorithm is here. (Line 66-69)
Looking at the algorithm, you may want to be slightly cautious about using it, if your bot have permission to send messages everywhere, the DefaultChannel
property would simply be the first channel in the server's channel list.
(And it would be bad news if that was a readme channel)
Upvotes: 3