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.0 KiB

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