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.8 kB

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