@@ -53,7 +53,7 @@ able to message. | |||||
You may check the message channel type. Visit [Glossary] to see the | You may check the message channel type. Visit [Glossary] to see the | ||||
various types of channels. | various types of channels. | ||||
[glossary]: xref:FAQ.Glossary#message-channels | |||||
[Glossary]: xref:Guides.Entities.Glossary#channels | |||||
## How can I get the guild from a message? | ## How can I get the guild from a message? | ||||
@@ -10,7 +10,7 @@ Casting only works for types that inherit the base type that you want to unbox f | |||||
`IUser` cannot be cast to `IMessage`. | `IUser` cannot be cast to `IMessage`. | ||||
> [!NOTE] | > [!NOTE] | ||||
> Interfaces CAN be cast to other interfaces, as long as they inherit eachother. | |||||
> Interfaces **can** be cast to other interfaces, as long as they inherit eachother. | |||||
> The same goes for reverse casting. As long as some entity can be simplified into what it inherits, your cast will pass. | > The same goes for reverse casting. As long as some entity can be simplified into what it inherits, your cast will pass. | ||||
## Boxing | ## Boxing | ||||
@@ -25,17 +25,17 @@ Through casting, we can **unbox** this type, and access the properties that were | |||||
Unboxing is the most direct way to access the real definition of an object. | Unboxing is the most direct way to access the real definition of an object. | ||||
If we want to return a type from its interface, we can unbox it directly. | If we want to return a type from its interface, we can unbox it directly. | ||||
[!code-csharp[Unboxing](images/unboxing.cs)] | |||||
[!code-csharp[Unboxing](samples/unboxing.cs)] | |||||
## Regular casting | ## Regular casting | ||||
In 'regular' casting, we use the ` as ` keyword to assign the given type to the object. | |||||
In 'regular' casting, we use the `as` keyword to assign the given type to the object. | |||||
If the boxed type can indeed be cast into given type, | If the boxed type can indeed be cast into given type, | ||||
it will become said type, and its properties can be accessed. | it will become said type, and its properties can be accessed. | ||||
[!code-csharp[Casting](images/casting.cs)] | |||||
[!code-csharp[Casting](samples/casting.cs)] | |||||
> [!WARNING] | > [!WARNING] | ||||
> If the type you're casting to is null, a ` NullReferenceException ` will be thrown when its called. | |||||
> If the type you're casting to is null, a `NullReferenceException` will be thrown when its called. | |||||
> This makes safety casting much more interesting to use, as it prevents this exception from being thrown. | > This makes safety casting much more interesting to use, as it prevents this exception from being thrown. | ||||
## Safety casting | ## Safety casting | ||||
@@ -48,21 +48,21 @@ There are 3 different ways to safety cast an object: | |||||
To safety cast an object, all we need to do is check if it is of the member type in a statement. | To safety cast an object, all we need to do is check if it is of the member type in a statement. | ||||
If this check fails, it will continue below, making sure we don't try to access null. | If this check fails, it will continue below, making sure we don't try to access null. | ||||
[!code-csharp[Base](images/safety-cast.cs)] | |||||
[!code-csharp[Base](samples/safety-cast.cs)] | |||||
### Object declaration: | ### Object declaration: | ||||
Here we declare the object we are casting to, | Here we declare the object we are casting to, | ||||
making it so that you can immediately work with its properties without reassigning through regular casting. | making it so that you can immediately work with its properties without reassigning through regular casting. | ||||
[!code-csharp[Declare](images/safety-cast-var.cs)] | |||||
[!code-csharp[Declare](samples/safety-cast-var.cs)] | |||||
### Reverse passage: | ### Reverse passage: | ||||
In previous examples, we want to let code continue running after the check, or if the check fails. | In previous examples, we want to let code continue running after the check, or if the check fails. | ||||
In this example, the cast will return the entire method (ignoring the latter) upon failure, | In this example, the cast will return the entire method (ignoring the latter) upon failure, | ||||
and declare the variable for further use into the method: | and declare the variable for further use into the method: | ||||
[!code-csharp[Pass](images/safety-cast-pass.cs)] | |||||
[!code-csharp[Pass](samples/safety-cast-pass.cs)] | |||||
> [!NOTE] | > [!NOTE] | ||||
> Usage of ` is `, ` not ` and ` as ` is required in cast assignment and/or type checks. ==, != and = are invalid assignment, | |||||
> Usage of `is`, `not` and `as` is required in cast assignment and/or type checks. `==`, `!=` and `=` are invalid assignment, | |||||
> as these operators only apply to initialized objects and not their types. | > as these operators only apply to initialized objects and not their types. |
@@ -7,7 +7,7 @@ title: Glossary & Flowcharts | |||||
A list of all Discord.Net entities, what they can be cast to and what their properties are. | A list of all Discord.Net entities, what they can be cast to and what their properties are. | ||||
> [!IMPORTANT] | |||||
> [!NOTE] | |||||
> All interfaces have the same inheritance tree for both `Socket` and `Rest` entities. | > All interfaces have the same inheritance tree for both `Socket` and `Rest` entities. | ||||
> Entities with that have been marked red are exclusive to the project they source from. | > Entities with that have been marked red are exclusive to the project they source from. | ||||
@@ -53,8 +53,8 @@ exist under a category. | |||||
 |  | ||||
* A **Rest Followup Message ([RestFollowupMessage]) is a message returned by followup on on an interaction. | |||||
* A **Rest Interaction Message ([RestInteractionMessage]) is a message returned by the interactions' original response. | |||||
* A **Rest Followup Message** ([RestFollowupMessage]) is a message returned by followup on on an interaction. | |||||
* A **Rest Interaction Message** ([RestInteractionMessage]) is a message returned by the interactions' original response. | |||||
* A **Rest User Message** ([RestUserMessage]) is a message sent over rest, can be any of the above. | * A **Rest User Message** ([RestUserMessage]) is a message sent over rest, can be any of the above. | ||||
* An **User Message** ([IUserMessage]) is a message sent by a user. | * An **User Message** ([IUserMessage]) is a message sent by a user. | ||||
* A **System Message** ([ISystemMessage]) is a message sent by Discord itself. | * A **System Message** ([ISystemMessage]) is a message sent by Discord itself. | ||||
@@ -94,13 +94,13 @@ exist under a category. | |||||
* An **Autocomplete Interaction** ([IAutocompleteinteraction]) is an interaction that has been automatically completed. | * An **Autocomplete Interaction** ([IAutocompleteinteraction]) is an interaction that has been automatically completed. | ||||
* An **Interaction** ([IDiscordInteraction]) is any of the above. | * An **Interaction** ([IDiscordInteraction]) is any of the above. | ||||
[ISlashCommandInteraction] xref: Discord.ISlashCommandInteraction | |||||
[IMessageCommandInteraction] xref: Discord.IMessageCommandInteraction | |||||
[IUserCommandInteraction] xref: Discord.IUserCommandInteraction | |||||
[IApplicationCommandInteraction] xref: Discord.IApplicationCommandInteraction | |||||
[IMessageComponent] xref: Discord.IMessageComponent | |||||
[IAutocompleteinteraction] xref: Discord.IAutocompleteInteraction | |||||
[IDiscordInteraction] xref: Discord.IDiscordInteraction | |||||
[ISlashCommandInteraction]: xref: Discord.ISlashCommandInteraction | |||||
[IMessageCommandInteraction]: xref: Discord.IMessageCommandInteraction | |||||
[IUserCommandInteraction]: xref: Discord.IUserCommandInteraction | |||||
[IApplicationCommandInteraction]: xref: Discord.IApplicationCommandInteraction | |||||
[IMessageComponent]: xref: Discord.IMessageComponent | |||||
[IAutocompleteinteraction]: xref: Discord.IAutocompleteInteraction | |||||
[IDiscordInteraction]: xref: Discord.IDiscordInteraction | |||||
## Other types: | ## Other types: | ||||
@@ -1,7 +1,7 @@ | |||||
// Say we have an entity, for the simplicity of this example, it will appear from thin air. | // Say we have an entity, for the simplicity of this example, it will appear from thin air. | ||||
IChannel channel; | IChannel channel; | ||||
// If I want this to be an ITextChannel so I can access the properties of a text channel inside of a guild, an approach would be: | |||||
// If we want this to be an ITextChannel so we can access the properties of a text channel inside of a guild, an approach would be: | |||||
ITextChannel textChannel = channel as ITextChannel; | ITextChannel textChannel = channel as ITextChannel; | ||||
await textChannel.DoSomethingICantWithIChannelAsync(); | await textChannel.DoSomethingICantWithIChannelAsync(); |
@@ -6,9 +6,3 @@ if (user is IGuildUser) | |||||
Console.WriteLine("This user is in a guild!"); | Console.WriteLine("This user is in a guild!"); | ||||
} | } | ||||
// Check failed. | // Check failed. | ||||
---------------------------- | |||||
// Another situation, where we want to get the actual data of said IGuildUser. | |||||
---------------------------- | |||||
// A final situation, where we dont actually need to do anything code-wise when the check does not pass, so we want to simplify it. |
@@ -21,8 +21,6 @@ | |||||
topicUid: Guides.Concepts.Events | topicUid: Guides.Concepts.Events | ||||
- name: Managing Connections | - name: Managing Connections | ||||
topicUid: Guides.Concepts.ManageConnections | topicUid: Guides.Concepts.ManageConnections | ||||
- name: Entities | |||||
topicUid: Guides.Concepts.Entities | |||||
- name: Entities | - name: Entities | ||||
items: | items: | ||||
- name: Introduction | - name: Introduction | ||||
@@ -1,6 +1,5 @@ | |||||
using Discord; | using Discord; | ||||
using Discord.Interactions; | using Discord.Interactions; | ||||
using InteractionFramework.Attributes; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace InteractionFramework.Modules | namespace InteractionFramework.Modules | ||||