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.

BasicOperations.md 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  4. need to cast the object as a certain type before you can perform any
  5. action. There are several ways to cast, with direct casting
  6. `(Type)type` being the the least recommended, as it *can* throw an
  7. [InvalidCastException] when the object isn't the desired type.
  8. Please refer to [this post] for more details.
  9. A good and safe casting example:
  10. [!code-csharp[Casting](samples/basics/cast.cs)]
  11. [InvalidCastException]: https://docs.microsoft.com/en-us/dotnet/api/system.invalidcastexception
  12. [this post]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-safely-cast-by-using-as-and-is-operators
  13. ## How do I send a message?
  14. Any implementation of [IMessageChannel] has a [SendMessageAsync]
  15. method. You can get the channel via [GetChannel] under the client.
  16. Remember, when using Discord.NET, polymorphism is a common recurring
  17. theme. This means an object may take in many shapes or form, which
  18. means casting is your friend. You should attempt to cast the channel
  19. as an [IMessageChannel] or any other entity that implements it to be
  20. able to message.
  21. [SendMessageAsync]: xref:Discord.IMessageChannel#Discord_IMessageChannel_SendMessageAsync_System_String_System_Boolean_Discord_Embed_Discord_RequestOptions_
  22. [GetChannel]: xref:Discord.WebSocket.DiscordSocketClient#Discord_WebSocket_DiscordSocketClient_GetChannel_System_UInt64_
  23. ## How can I tell if a message is from X, Y, Z?
  24. You may check the message channel type. Visit [Glossary] to see the
  25. various types of channels.
  26. [Glossary]: Glossary.md
  27. ## How do I add hyperlink text to an embed?
  28. Embeds can use standard [markdown] in the description field as well as
  29. in field values. With that in mind, links can be added using the
  30. following format \[text](link).
  31. [markdown]: https://support.discordapp.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-
  32. ## How do I add reactions to a message?
  33. Any entities that implement [IUserMessage] has an [AddReactionAsync]
  34. method. This method expects an [IEmote] as a parameter.
  35. In Discord.Net, an Emote represents a server custom emote, while an
  36. Emoji is a Unicode emoji (standard emoji). Both [Emoji] and [Emote]
  37. implement [IEmote] and are valid options.
  38. [!code-csharp[Emoji](samples/basics/emoji.cs)]
  39. [AddReactionAsync]: xref:Discord.IUserMessage#Discord_IUserMessage_AddReactionAsync_Discord_IEmote_Discord_RequestOptions_
  40. ## Why am I getting so many preemptive rate limits when I try to add more than one reactions?
  41. This is due to how .NET parses the HTML header, mistreating
  42. 0.25sec/action to 1sec. This casues the lib to throw preemptive rate
  43. limit more frequently than it should for methods such as adding
  44. reactions.
  45. ## Can I opt-out of preemptive rate limits?
  46. Unfortunately, not at the moment. See [#401](https://github.com/RogueException/Discord.Net/issues/401).
  47. [IMessageChannel]: xref:Discord.IMessageChannel
  48. [IUserMessage]: xref:Discord.IUserMessage
  49. [IEmote]: xref:Discord.IEmote
  50. [Emote]: xref:Discord.Emote
  51. [Emoji]: xref:Discord.Emoji