Discord.Net C# 1.0.2 How to Send Messages to Specific Channels

Discord.NET C# 1.0.2 How to send messages to specific channels

public async Task Announce() // 1
{
DiscordSocketClient _client = new DiscordSocketClient(); // 2
ulong id = 123456789012345678; // 3
var chnl = _client.GetChannel(id) as IMessageChannel; // 4
await chnl.SendMessageAsync("Announcement!"); // 5
}

[1] A standard async task (You can choose to just include the code inside the braces inside your own method).

[2] Create an instance of the Discord Client.

[3] A random sequential channel ID (Replace with your own channel ID).

[4] Creating an instance of the Discord Channel as an IMessageChannel instead of a SocketChannel.

[5] Sending the message to the text channel.

Discord.NET sending messages to one specific channel in one specific server

Actually, I kinda feel stupid after realizing this, In my code previously, I was making the program try and find a channel, when what I actually needed to do was find a text channel, like so:

    discord.GetGuild("serverid").GetTextChannel("Channelid").SendMessageAsync(
"Message")

So sorry for asking for help when the answer was really simple.
:/

Discord.Net 2.0 send message to specific channel

Public Async Function Kick(ByVal user As IGuildUser, <Remainder> ByVal reason As String) As Task
Dim guild = Context.Guild
Dim chnl = guild.GetTextChannel(123456789)
Dim embed As New EmbedBuilder With {
.Author = New EmbedAuthorBuilder With {
.IconUrl = user.GetAvatarUrl,
.Name = user.Username
},
.Title = $"{user.toString()}'s Kick Information",
.ImageUrl = "https://i.imgur.com/vc241Ku.jpeg",
.Description = reason,
.Color = New Color(masterClass.randomEmbedColor),
.ThumbnailUrl = user.GetAvatarUrl,
.Timestamp = Context.Message.Timestamp,
.Footer = New EmbedFooterBuilder With {
.Text = "Kick Data",
.IconUrl = guild.IconUrl
}
}

Await chnl.SendMessageAsync("", False, embed.Build())
Await user.SendMessageAsync("", False, embed.Build())
Await user.KickAsync(reason)
End Function

if message found in specific channel Discord.net

As pointed out in the comment of the thread, discord refers to OP's DiscordSocketClient, which in turn would refer to the current bot client; as such, a single object of Channel cannot possibly exist, as the bot may have access to more than a handful of "channels" within the reach of the bot.

OP was likely referring to the channel of which the message arrived in was, which would be message.Channel.

Additionally, it is worth noting that when determining the channel type, there could be several variants when using Discord.Net due to its polymorphic nature (e.g. a channel can be a voice, category, text channel and more; see glossary for more details).

Although in the context of OP's code, only ISocketMessageChannel or its implementation is possible since a message could only be sent to a message channel. Despite this, the reason for bringing this up is that many more methods of obtaining a channel could sometimes return an IChannel only, leaving confusion behind as to why they could not send a message to this channel. When in fact, they only needed to (try) cast the channel as the appropriate type (see glossary above).

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

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! ");
});


Related Topics



Leave a reply



Submit