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.

BaseSocketClient.Events.Examples.cs 4.8 kB

docs: Main docs update (#1304) * Remove template in favor of official samples * Fixed a variable name copy pasta mistake line 35 was _database.GetData() instead of DBService.GetData() * Experimental theme change * Change paragraph, code, heading fonts * Widen viewport * Update DocFX.Plugins.LastModified v1.2.3 * Exclude Discord.API in docs * Add remarks for SocketReaction properties * Add examples for BaseSocketClient.Events * Add additional clarification for some methods * Move IUser and IGuildChannel examples * Clarify several guides samples with notes - Reword TypeReader comment to avoid giving the idea that the sample itself is "obsolete" - Remove CommandException logging comment regarding C#7.0 as the version is now the standard across VS2017 and up - Remove suggestion about handling result in command handler since it is now advised to use CommandExecuted instead + Add additional comment to clarify ctor for DI setup * Add/migrate code examples * Incorporate material design theme License @ https://github.com/ovasquez * Update installation and nightly guide * Fix improper indentations made obvious by the widen viewport * Fix minor grammar issues + Add installation for nightly build using dotnet CLI * Fix nav level indentation * Revise "Your First Bot" article * Merge some paragraphs to avoid clutter while keeping readability * Reword the use of command framework + Add additional warning/note about environment variable * Add additional indent level * Fix indentation text warping * Remove connections sample * Update logging sample Remove redundant part of the sample * Remove mention of RPC * Remove misleading section about commands - Remove command sample from complete snippet * Revise "Your First Bot" command paragraphs * Change wording to hint devs that additional command parser packages may be available, as more and more begin to crop up * Update themes * Add XML docs contribution guidelines Update guidelines * Update CommandExecuted remarks * Fix precondition remarks typo no one saw that ok * Fix permission sample in docfx * Fix IMessageChannel samples * Update docs/_template/light-dark-theme/styles/docfx.vendor.minify.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com>
6 years ago
docs: Main docs update (#1304) * Remove template in favor of official samples * Fixed a variable name copy pasta mistake line 35 was _database.GetData() instead of DBService.GetData() * Experimental theme change * Change paragraph, code, heading fonts * Widen viewport * Update DocFX.Plugins.LastModified v1.2.3 * Exclude Discord.API in docs * Add remarks for SocketReaction properties * Add examples for BaseSocketClient.Events * Add additional clarification for some methods * Move IUser and IGuildChannel examples * Clarify several guides samples with notes - Reword TypeReader comment to avoid giving the idea that the sample itself is "obsolete" - Remove CommandException logging comment regarding C#7.0 as the version is now the standard across VS2017 and up - Remove suggestion about handling result in command handler since it is now advised to use CommandExecuted instead + Add additional comment to clarify ctor for DI setup * Add/migrate code examples * Incorporate material design theme License @ https://github.com/ovasquez * Update installation and nightly guide * Fix improper indentations made obvious by the widen viewport * Fix minor grammar issues + Add installation for nightly build using dotnet CLI * Fix nav level indentation * Revise "Your First Bot" article * Merge some paragraphs to avoid clutter while keeping readability * Reword the use of command framework + Add additional warning/note about environment variable * Add additional indent level * Fix indentation text warping * Remove connections sample * Update logging sample Remove redundant part of the sample * Remove mention of RPC * Remove misleading section about commands - Remove command sample from complete snippet * Revise "Your First Bot" command paragraphs * Change wording to hint devs that additional command parser packages may be available, as more and more begin to crop up * Update themes * Add XML docs contribution guidelines Update guidelines * Update CommandExecuted remarks * Fix precondition remarks typo no one saw that ok * Fix permission sample in docfx * Fix IMessageChannel samples * Update docs/_template/light-dark-theme/styles/docfx.vendor.minify.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com>
6 years ago
docs: Main docs update (#1304) * Remove template in favor of official samples * Fixed a variable name copy pasta mistake line 35 was _database.GetData() instead of DBService.GetData() * Experimental theme change * Change paragraph, code, heading fonts * Widen viewport * Update DocFX.Plugins.LastModified v1.2.3 * Exclude Discord.API in docs * Add remarks for SocketReaction properties * Add examples for BaseSocketClient.Events * Add additional clarification for some methods * Move IUser and IGuildChannel examples * Clarify several guides samples with notes - Reword TypeReader comment to avoid giving the idea that the sample itself is "obsolete" - Remove CommandException logging comment regarding C#7.0 as the version is now the standard across VS2017 and up - Remove suggestion about handling result in command handler since it is now advised to use CommandExecuted instead + Add additional comment to clarify ctor for DI setup * Add/migrate code examples * Incorporate material design theme License @ https://github.com/ovasquez * Update installation and nightly guide * Fix improper indentations made obvious by the widen viewport * Fix minor grammar issues + Add installation for nightly build using dotnet CLI * Fix nav level indentation * Revise "Your First Bot" article * Merge some paragraphs to avoid clutter while keeping readability * Reword the use of command framework + Add additional warning/note about environment variable * Add additional indent level * Fix indentation text warping * Remove connections sample * Update logging sample Remove redundant part of the sample * Remove mention of RPC * Remove misleading section about commands - Remove command sample from complete snippet * Revise "Your First Bot" command paragraphs * Change wording to hint devs that additional command parser packages may be available, as more and more begin to crop up * Update themes * Add XML docs contribution guidelines Update guidelines * Update CommandExecuted remarks * Fix precondition remarks typo no one saw that ok * Fix permission sample in docfx * Fix IMessageChannel samples * Update docs/_template/light-dark-theme/styles/docfx.vendor.minify.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com>
6 years ago
docs: Main docs update (#1304) * Remove template in favor of official samples * Fixed a variable name copy pasta mistake line 35 was _database.GetData() instead of DBService.GetData() * Experimental theme change * Change paragraph, code, heading fonts * Widen viewport * Update DocFX.Plugins.LastModified v1.2.3 * Exclude Discord.API in docs * Add remarks for SocketReaction properties * Add examples for BaseSocketClient.Events * Add additional clarification for some methods * Move IUser and IGuildChannel examples * Clarify several guides samples with notes - Reword TypeReader comment to avoid giving the idea that the sample itself is "obsolete" - Remove CommandException logging comment regarding C#7.0 as the version is now the standard across VS2017 and up - Remove suggestion about handling result in command handler since it is now advised to use CommandExecuted instead + Add additional comment to clarify ctor for DI setup * Add/migrate code examples * Incorporate material design theme License @ https://github.com/ovasquez * Update installation and nightly guide * Fix improper indentations made obvious by the widen viewport * Fix minor grammar issues + Add installation for nightly build using dotnet CLI * Fix nav level indentation * Revise "Your First Bot" article * Merge some paragraphs to avoid clutter while keeping readability * Reword the use of command framework + Add additional warning/note about environment variable * Add additional indent level * Fix indentation text warping * Remove connections sample * Update logging sample Remove redundant part of the sample * Remove mention of RPC * Remove misleading section about commands - Remove command sample from complete snippet * Revise "Your First Bot" command paragraphs * Change wording to hint devs that additional command parser packages may be available, as more and more begin to crop up * Update themes * Add XML docs contribution guidelines Update guidelines * Update CommandExecuted remarks * Fix precondition remarks typo no one saw that ok * Fix permission sample in docfx * Fix IMessageChannel samples * Update docs/_template/light-dark-theme/styles/docfx.vendor.minify.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com> * Update docs/_template/light-dark-theme/styles/material.css Co-Authored-By: Still34 <341464@gmail.com>
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Discord.WebSocket;
  5. using JetBrains.Annotations;
  6. namespace Discord.Net.Examples.WebSocket
  7. {
  8. [PublicAPI]
  9. internal class BaseSocketClientExamples
  10. {
  11. #region ReactionAdded
  12. public void HookReactionAdded(BaseSocketClient client)
  13. => client.ReactionAdded += HandleReactionAddedAsync;
  14. public async Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> cachedMessage,
  15. Cacheable<IMessageChannel, ulong> originChannel, SocketReaction reaction)
  16. {
  17. var message = await cachedMessage.GetOrDownloadAsync();
  18. if (message != null && reaction.User.IsSpecified)
  19. Console.WriteLine($"{reaction.User.Value} just added a reaction '{reaction.Emote}' " +
  20. $"to {message.Author}'s message ({message.Id}).");
  21. }
  22. #endregion
  23. #region ChannelCreated
  24. public void HookChannelCreated(BaseSocketClient client)
  25. => client.ChannelCreated += HandleChannelCreated;
  26. public Task HandleChannelCreated(SocketChannel channel)
  27. {
  28. if (channel is SocketGuildChannel guildChannel)
  29. Console.WriteLine($"A new channel '{guildChannel.Name}'({guildChannel.Id}, {guildChannel.GetType()})"
  30. + $"has been created at {guildChannel.CreatedAt}.");
  31. return Task.CompletedTask;
  32. }
  33. #endregion
  34. #region ChannelDestroyed
  35. public void HookChannelDestroyed(BaseSocketClient client)
  36. => client.ChannelDestroyed += HandleChannelDestroyed;
  37. public Task HandleChannelDestroyed(SocketChannel channel)
  38. {
  39. if (channel is SocketGuildChannel guildChannel)
  40. Console.WriteLine(
  41. $"A new channel '{guildChannel.Name}'({guildChannel.Id}, {guildChannel.GetType()}) has been deleted.");
  42. return Task.CompletedTask;
  43. }
  44. #endregion
  45. #region ChannelUpdated
  46. public void HookChannelUpdated(BaseSocketClient client)
  47. => client.ChannelUpdated += HandleChannelRename;
  48. public Task HandleChannelRename(SocketChannel beforeChannel, SocketChannel afterChannel)
  49. {
  50. if (beforeChannel is SocketGuildChannel beforeGuildChannel &&
  51. afterChannel is SocketGuildChannel afterGuildChannel)
  52. if (beforeGuildChannel.Name != afterGuildChannel.Name)
  53. Console.WriteLine(
  54. $"A channel ({beforeChannel.Id}) is renamed from {beforeGuildChannel.Name} to {afterGuildChannel.Name}.");
  55. return Task.CompletedTask;
  56. }
  57. #endregion
  58. #region MessageReceived
  59. private readonly ulong[] _targetUserIds = {168693960628371456, 53905483156684800};
  60. public void HookMessageReceived(BaseSocketClient client)
  61. => client.MessageReceived += HandleMessageReceived;
  62. public Task HandleMessageReceived(SocketMessage message)
  63. {
  64. // check if the message is a user message as opposed to a system message (e.g. Clyde, pins, etc.)
  65. if (!(message is SocketUserMessage userMessage)) return Task.CompletedTask;
  66. // check if the message origin is a guild message channel
  67. if (!(userMessage.Channel is SocketTextChannel textChannel)) return Task.CompletedTask;
  68. // check if the target user was mentioned
  69. var targetUsers = userMessage.MentionedUsers.Where(x => _targetUserIds.Contains(x.Id));
  70. foreach (var targetUser in targetUsers)
  71. Console.WriteLine(
  72. $"{targetUser} was mentioned in the message '{message.Content}' by {message.Author} in {textChannel.Name}.");
  73. return Task.CompletedTask;
  74. }
  75. #endregion
  76. #region MessageDeleted
  77. public void HookMessageDeleted(BaseSocketClient client)
  78. => client.MessageDeleted += HandleMessageDelete;
  79. public async Task HandleMessageDelete(Cacheable<IMessage, ulong> cachedMessage, Cacheable<IMessageChannel, ulong> cachedChannel)
  80. {
  81. // check if the message exists in cache; if not, we cannot report what was removed
  82. if (!cachedMessage.HasValue) return;
  83. // gets or downloads the channel if it's not in the cache
  84. IMessageChannel channel = await cachedChannel.GetOrDownloadAsync();
  85. var message = cachedMessage.Value;
  86. Console.WriteLine(
  87. $"A message ({message.Id}) from {message.Author} was removed from the channel {channel.Name} ({channel.Id}):"
  88. + Environment.NewLine
  89. + message.Content);
  90. }
  91. #endregion
  92. }
  93. }