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 3.6 KiB

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