cooltrain
cooltrain

Reputation: 41

C# Discord.net 1.0.2 Sending message to specific channel

Looking at how to send messages to specific channels. After searching around it doesn't seem to be too complicated but for some reason channel in this case is returning null, and as such an object reference not set to an instance of an object error when its used. The ID should be correct so I'm not really sure whats wrong.

[Command("say")]
    public async Task sayAsync([Remainder] string message)
    {
        DiscordSocketClient client = new DiscordSocketClient();
        ulong channelID = 123456789123456789; //Example ID 

        var channel = client.GetChannel(channelID) as SocketTextChannel;
        await channel.SendMessageAsync(message);

    }

Upvotes: 1

Views: 3989

Answers (1)

cooltrain
cooltrain

Reputation: 41

If anyone finds this I'll put the answer I found on the discord API discord.

I made the mistake of creating a new client and trying to find the channels inside that.

 DiscordSocketClient client = new DiscordSocketClient();

I should have been using the client that was used for the connection and thus

var client = Context.Client;

Was all that was needed to fix the issue.

Upvotes: 2

Related Questions