简体   繁体   中英

DM Join message not working in Discord C# Bot

I'm getting an error where when I try to create a DM channel for a welcome message I get the error: Cannot convert type to via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

Here is my code.

        {
            var channel = _client.GetDMChannelsAsync() as SocketDMChannel;
            await channel.SendMessageAsync("Welcome to Noice Discord!");
        }

In my handlecommandsasync function I also have this at the bottom. _client.UserJoined += AnnounceJoinedUser;

Any help would be nice as I'm really stuck here.

Firstly, seems like you're using an outdated version of the library, I recommend you update.

Secondly, assuming the _client in your code is the SocketGuildUser that your UserJoined event handler gives (look into giving it a better name and in future when asking for help, provide all the code that is required), you don't need to cast it as anything and you want to await it.
And you shouldn't be using GetDMChannelsAsync .
(Note the plural, maybe you did this by accident? Remove that)

So here's what you want to end up with (on a more up-to-date version of Discord.NET)

var channel = await _client.GetDMChannelAsync();
await channel.SendMessageAsync("Welcome to Noice Discord!");

Or if you change to a more up-to-date version of the library, the method is renamed to GetOrCreateDMChannelAsync .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM