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.

basic-operations.md 4.5 kB

7 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ---
  2. uid: FAQ.Basics.BasicOp
  3. title: Questions about Basic Operations
  4. ---
  5. # Basic Operations Questions
  6. In the following section, you will find commonly asked questions and
  7. answers regarding basic usage of the library, as well as
  8. language-specific tips when using this library.
  9. ## How should I safely check a type?
  10. > [!WARNING]
  11. > Direct casting (e.g., `(Type)type`) is **the least recommended**
  12. > way of casting, as it *can* throw an [InvalidCastException]
  13. > when the object isn't the desired type.
  14. >
  15. > Please refer to [this post] for more details.
  16. In Discord.Net, the idea of polymorphism is used throughout. You may
  17. need to cast the object as a certain type before you can perform any
  18. action.
  19. A good and safe casting example:
  20. [!code-csharp[Casting](samples/cast.cs)]
  21. [InvalidCastException]: https://docs.microsoft.com/en-us/dotnet/api/system.invalidcastexception
  22. [this post]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-safely-cast-by-using-as-and-is-operators
  23. ## How do I send a message?
  24. > [!TIP]
  25. > The [GetChannel] method by default returns an [IChannel], allowing
  26. > channel types such as [IVoiceChannel], [ICategoryChannel]
  27. > to be returned; consequently, you cannot send a message
  28. > to channels like those.
  29. Any implementation of [IMessageChannel] has a [SendMessageAsync]
  30. method. You can get the channel via [GetChannel] under the client.
  31. Remember, when using Discord.Net, polymorphism is a common recurring
  32. theme. This means an object may take in many shapes or form, which
  33. means casting is your friend. You should attempt to cast the channel
  34. as an [IMessageChannel] or any other entity that implements it to be
  35. able to message.
  36. [SendMessageAsync]: xref:Discord.IMessageChannel.SendMessageAsync*
  37. [GetChannel]: xref:Discord.WebSocket.DiscordSocketClient.GetChannel*
  38. ## How can I tell if a message is from X, Y, Z channel?
  39. You may check the message channel type. Visit [Glossary] to see the
  40. various types of channels.
  41. [Glossary]: xref:FAQ.Glossary#message-channels
  42. ## How can I get the guild from a message?
  43. There are 2 ways to do this. You can do either of the following,
  44. 1. Cast the user as an [IGuildUser] and use its [IGuild] property.
  45. 2. Cast the channel as an [IGuildChannel] and use its [IGuild] property.
  46. ## How do I add hyperlink text to an embed?
  47. Embeds can use standard [markdown] in the description field as well
  48. as in field values. With that in mind, links can be added with
  49. `[text](link)`.
  50. [markdown]: https://support.discordapp.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-
  51. ## How do I add reactions to a message?
  52. Any entity that implements [IUserMessage] has an [AddReactionAsync]
  53. method. This method expects an [IEmote] as a parameter.
  54. In Discord.Net, an Emote represents a custom-image emote, while an
  55. Emoji is a Unicode emoji (standard emoji). Both [Emoji] and [Emote]
  56. implement [IEmote] and are valid options.
  57. # [Adding a reaction to another message](#tab/emoji-others)
  58. [!code-csharp[Emoji](samples/emoji-others.cs)]
  59. # [Adding a reaction to a sent message](#tab/emoji-self)
  60. [!code-csharp[Emoji](samples/emoji-self.cs)]
  61. ***
  62. [AddReactionAsync]: xref:Discord.IUserMessage.AddReactionAsync*
  63. ## What is a "preemptive rate limit?"
  64. A preemptive rate limit is Discord.Net's way of telling you to slow
  65. down before you get hit by the real rate limit. Hitting a real rate
  66. limit might prevent your entire client from sending any requests for
  67. a period of time. This is calculated based on the HTTP header
  68. returned by a Discord response.
  69. ## Why am I getting so many preemptive rate limits when I try to add more than one reactions?
  70. This is due to how HTML header works, mistreating
  71. 0.25sec/action to 1sec. This causes the lib to throw preemptive rate
  72. limit more frequently than it should for methods such as adding
  73. reactions.
  74. ## Can I opt-out of preemptive rate limits?
  75. Unfortunately, not at the moment. See [#401](https://github.com/RogueException/Discord.Net/issues/401).
  76. [IChannel]: xref:Discord.IChannel
  77. [ICategoryChannel]: xref:Discord.ICategoryChannel
  78. [IGuildChannel]: xref:Discord.IGuildChannel
  79. [ITextChannel]: xref:Discord.ITextChannel
  80. [IGuild]: xref:Discord.IGuild
  81. [IVoiceChannel]: xref:Discord.IVoiceChannel
  82. [IGuildUser]: xref:Discord.IGuildUser
  83. [IMessageChannel]: xref:Discord.IMessageChannel
  84. [IUserMessage]: xref:Discord.IUserMessage
  85. [IEmote]: xref:Discord.IEmote
  86. [Emote]: xref:Discord.Emote
  87. [Emoji]: xref:Discord.Emoji