'How do I create a DiscordChannel entity in DSharp+ with no context
I want to make my Discord bot send a message on startup using the Discord Message Builder, but .sendAsync() requires a DiscordChannel entity. All the guides I've read use context, but I can't use context unless I'm replying to a message, which I can't do.
static async Task Send(string text)
{
var msg = await new DiscordMessageBuilder()
.WithContent(text)
.SendAsync();
}
Solution 1:[1]
I'm not sure what you mean by startup but I wrote a solution that works when the bot should send a message when it joins a server.
At first your Bot needs a special intent which you can add in your DiscordConfiguration
Intents = DiscordIntents.Guilds
After this step you can create a eventhandler which fires when your bot joins a guild. The eventarguments provides the guild and you can get the default channel of the server.
EventHandler:
Client.GuildCreated += OnGuildCreated;
EventFunction:
private void OnGuildCreated(object sender, GuildCreateEventArgs e)
{
DiscordChannel defaultChannel = e.Guild.GetDefaultChannel();
defaultChannel.SendMessageAsync("Your text / message");
}
I hope I have understood your question correctly.
Edit: The solution only shows how to get channels. If the default channel can't/shouldn't be taken, you can also use Guild.Channels to get all channels of a server.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
