You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

2-BasicOperations.md 3.8 KiB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Basic Operations Questions
  2. ## How should I safely check a type?
  3. In Discord.NET, the idea of polymorphism is used throughout. You may need to cast the object as a certain type before you can perform any action. There are several ways to cast, with direct casting `(Type)type` being the the least recommended, as it *can* throw an `InvalidCastException` when the object isn't the desired type. Please refer to [this post](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-safely-cast-by-using-as-and-is-operators) for more details.
  4. A good and safe casting example:
  5. ```cs
  6. public async Task MessageReceivedHandler(SocketMessage msg)
  7. {
  8. // Option 1:
  9. // Using the `as` keyword, which will return `null` if the object isn't the desired type.
  10. var usermsg = msg as SocketUserMessage;
  11. // We bail when the message isn't the desired type.
  12. if (msg == null) return;
  13. // Option 2:
  14. // Using the `is` keyword to cast (C#7 or above only)
  15. if (msg is SocketUserMessage usermsg)
  16. {
  17. // Do things
  18. }
  19. }
  20. ```
  21. ## How do I send a message?
  22. Any implementation of **IMessageChannel** has a **SendMessageAsync** method. Using the client, you can get an appropriate channel (**GetChannel(id)**) to send a message to. Remember, when using Discord.NET, polymorphism is a common recurring theme. This means an object may take in many shapes or form, which means casting is your friend. You should attempt to cast the channel as an `IMessageChannel` or any other entity that implements it to be able to message.
  23. ## How can I tell if a message is from X, Y, Z?
  24. You may check message channel type.
  25. * A **Text channel** (`ITextChannel`) is a message channel from a Guild.
  26. * A **DM channel** (`IDMChannel`) is a message channel from a DM.
  27. * A **Group channel** (`IGroupChannel`) is a message channel from a Group (this is rarely used, due to the bot's inability to join a group).
  28. * A **Private channel** (`IPrivateChannel`) is a DM or a Group.
  29. * A **Message channel** (`IMessageChannel`) is all of the above.
  30. ## How do I add hyperlink text to an embed?
  31. Embeds can use standard [markdown](https://support.discordapp.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-) in the Description as well as in field values. With that in mind, links can be added using the following format \[text](link).
  32. ## How do I add reactions to a message?
  33. Any entities that implement `IUserMessage` has an **AddReactionAsync** method. This method expects an `IEmote` as a parameter. In Discord.Net, an Emote represents a server custom emote, while an Emoji is a Unicode emoji (standard emoji). Both `Emoji` and `Emote` implement `IEmote` and are valid options.
  34. ```cs
  35. // bail if the message is not a user one (system messages cannot have reactions)
  36. var usermsg = msg as IUserMessage;
  37. if (usermsg == null) return;
  38. // standard Unicode emojis
  39. Emoji emoji = new Emoji("👍");
  40. // or
  41. // Emoji emoji = new Emoji("\u23F8");
  42. // custom guild emotes
  43. Emote emote = Emote.Parse("<:dotnet:232902710280716288>");
  44. // using Emote.TryParse may be safer in regards to errors being thrown;
  45. // please note that the method does not verify if the emote exists,
  46. // it simply creates the Emote object for you.
  47. // add the reaction to the message
  48. await usermsg.AddReactionAsync(emoji);
  49. await usermsg.AddReactionAsync(emote);
  50. ```
  51. ## Why am I getting so many preemptive rate limits when I try to add more than one reactions?
  52. This is due to how .NET parses the HTML header, mistreating 0.25sec/action to 1sec. This casues the lib to throw preemptive rate limit more frequently than it should for methods such as adding reactions.
  53. ## Can I opt-out of preemptive rate limits?
  54. Unfortunately, not at the moment. See [#401](https://github.com/RogueException/Discord.Net/issues/401).