Discord.net sending direct message(PM) to specified user

Diceon picture Diceon · May 12, 2017 · Viewed 11.6k times · Source

I'm new here so don't judge me, I want to create a command !poke which sends a direct message(PM) to specified user, but I can't seem to find a way to feed userid to it and documentation is pretty much non existing for discord.net.

        commands.CreateCommand("poke")
            .Parameter("target")
            .Do(async (e) =>
            {
                ulong userID = e.User.Id;
                Console.WriteLine("[" + e.Server.Name + "]" + e.User.Name + " just poked " + e.GetArg("target"));
                await e.User.SendMessage("HEY, wake up! ");
            });

Answer

Diceon picture Diceon · May 31, 2017

There's a few ways of doing this, first is creating private channel with command:

                var c = discord.CreatePrivateChannel(ulong userid);

and sending message from it like this:

                await c.SendMessage("blabla");

and another way is storing user as object and then sending message from it.

                User u = null;
                string findUser = e.GetArg("target");

                        u = e.Message.MentionedUsers.FirstOrDefault(); //checking mentioned users
                        u = e.Server.FindUsers(findUser).FirstOrDefault(); //looking for specified user in server

                await u.SendMessage("HEY, wake up! ");

My command looks like this:

        commands.CreateCommand("poke")
            .Parameter("target", ParameterType.Required)
            .Do(async (e) =>
            {
                ulong id;
                User u = null;
                string findUser = e.Args[0];

                if (!string.IsNullOrWhiteSpace(findUser))
                {
                    if (e.Message.MentionedUsers.Count() == 1)
                        u = e.Message.MentionedUsers.FirstOrDefault();
                    else if (e.Server.FindUsers(findUser).Any())
                        u = e.Server.FindUsers(findUser).FirstOrDefault();
                    else if (ulong.TryParse(findUser, out id))
                        u = e.Server.GetUser(id);
                }
                Console.WriteLine("[" + e.Server.Name + "]" + e.User.Name + " just poked " + u);
                await u.SendMessage("HEY, wake up! ");
            });