This repository was archived by the owner on Sep 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGuildedWebhookClient.cs
More file actions
158 lines (141 loc) · 10.4 KB
/
Copy pathGuildedWebhookClient.cs
File metadata and controls
158 lines (141 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Guilded.Base;
using Guilded.Base.Embeds;
using RestSharp;
namespace Guilded.Webhook;
/// <summary>
/// Represents the <see cref="GuildedWebhookClient">client</see> for <see cref="T:Guilded.Servers.Webhook">webhook</see> execution.
/// </summary>
/// <remarks>
/// <para>This does not require to be connected. You can use it on a go.</para>
/// </remarks>
/// <example>
/// <para>Here's an example of a <see cref="GuildedWebhookClient">webhook client</see> in action:</para>
/// <code language="csharp">
/// await using var webhookClient = new GuildedWebhookClient("...url here...", "... another webhook's url here...", ...);
///
/// await webhookClient.CreateMessageAsync("Everyone, we have an announcement to make: Stop bullying!");
/// </code>
/// </example>
/// <seealso cref="T:Guilded.GuildedBotClient" />
/// <seealso cref="T:Guilded.AbstractGuildedClient" />
/// <seealso cref="BaseGuildedService" />
public class GuildedWebhookClient : BaseGuildedService
{
/// <summary>
/// Gets the list of all <see cref="T:Guilded.Servers.Webhook">webhooks</see> this <see cref="GuildedWebhookClient">client</see> will execute.
/// </summary>
/// <value>List of <see cref="T:Guilded.Servers.Webhook">webhooks</see></value>
public IList<IWebhook> Webhooks { get; }
#region Constructors
/// <summary>
/// Creates a new <see cref="GuildedWebhookClient" /> instance from given <paramref name="webhooks" />.
/// </summary>
/// <remarks>
/// <para>To execute the webhooks, you can use <see cref="CreateMessageAsync(MessageContent)" /> method.</para>
/// </remarks>
/// <param name="webhooks">The list of <see cref="T:Guilded.Servers.Webhook">webhooks</see> that will be executed</param>
/// <returns>New <see cref="GuildedWebhookClient" /> instance</returns>
/// <seealso cref="GuildedWebhookClient" />
/// <seealso cref="GuildedWebhookClient(IList{IWebhook})" />
/// <seealso cref="GuildedWebhookClient(IWebhook[])" />
/// <seealso cref="GuildedWebhookClient(Uri[])" />
/// <seealso cref="GuildedWebhookClient(string[])" />
/// <seealso cref="GuildedWebhookClient(Guid, string)" />
public GuildedWebhookClient(IList<IWebhook> webhooks) : base(new Uri(GuildedUrl.Media, "webhooks")) =>
Webhooks = webhooks;
/// <inheritdoc cref="GuildedWebhookClient(IList{IWebhook})" />
public GuildedWebhookClient(params IWebhook[] webhooks) : this((IList<IWebhook>)webhooks) { }
/// <inheritdoc cref="GuildedWebhookClient(IList{IWebhook})" />
public GuildedWebhookClient(params Uri[] webhookUrls) : this(webhookUrls.Select(x => new WebhookSkeleton(x)).ToList<IWebhook>()) { }
/// <inheritdoc cref="GuildedWebhookClient(IList{IWebhook})" />
public GuildedWebhookClient(params string[] webhookUrls) : this(webhookUrls.Select(x => new WebhookSkeleton(x)).ToList<IWebhook>()) { }
/// <inheritdoc cref="GuildedWebhookClient(IList{IWebhook})" />
public GuildedWebhookClient(Guid webhook, string token) : this(new WebhookSkeleton(webhook, token)) { }
#endregion
#region Methods
/// <summary>
/// Creates a <see cref="MessageContent">message</see> for the specified <see cref="T:Guilded.Servers.Webhook">webhook</see>.
/// </summary>
/// <param name="webhook">The <see cref="T:Guilded.Servers.Webhook.Url">url</see> of the <see cref="T:Guilded.Servers.Webhook">webhook</see> to execute</param>
/// <param name="message">The <see cref="MessageContent">message</see> to send</param>
/// <exception cref="GuildedException" />
/// <exception cref="GuildedRequestException" />
/// <exception cref="GuildedResourceException" />
public Task CreateMessageAsync(Uri webhook, MessageContent message) =>
ExecuteRequestAsync(new RestRequest(webhook, Method.Post).AddBody(message));
/// <inheritdoc cref="CreateMessageAsync(Uri, MessageContent)" />
/// <param name="webhook">The <see cref="T:Guilded.Servers.Webhook.Url">url</see> of the <see cref="T:Guilded.Servers.Webhook">webhook</see> to execute</param>
/// <param name="content">The <see cref="MessageContent.Content">text contents</see> of the <see cref="MessageContent">message</see> in Markdown</param>
/// <param name="embeds">The list of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
/// <param name="username">The displayed name of the webhook</param>
/// <param name="avatar">The displayed profile picture of the webhook</param>
public Task CreateMessageAsync(Uri webhook, string? content = null, IList<Embed>? embeds = null, string? username = null, Uri? avatar = null) =>
CreateMessageAsync(webhook, new MessageContent(content, embeds, username: username, avatar: avatar));
/// <inheritdoc cref="CreateMessageAsync(Uri, MessageContent)" />
/// <param name="webhook">The <see cref="T:Guilded.Servers.Webhook.Url">url</see> of the <see cref="T:Guilded.Servers.Webhook">webhook</see> to execute</param>
/// <param name="content">The <see cref="MessageContent.Content">text contents</see> of the <see cref="MessageContent">message</see> in Markdown</param>
/// <param name="username">The displayed name of the webhook</param>
/// <param name="avatar">The displayed profile picture of the webhook</param>
/// <param name="embeds">The array of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
public Task CreateMessageAsync(Uri webhook, string? content = null, string? username = null, Uri? avatar = null, params Embed[] embeds) =>
CreateMessageAsync(webhook, new MessageContent(content, embeds, username: username, avatar: avatar));
/// <inheritdoc cref="CreateMessageAsync(Uri, MessageContent)" />
/// <param name="webhook">The <see cref="T:Guilded.Servers.Webhook.Url">url</see> of the <see cref="T:Guilded.Servers.Webhook">webhook</see> to execute</param>
/// <param name="content">The <see cref="MessageContent.Content">text contents</see> of the <see cref="MessageContent">message</see> in Markdown</param>
/// <param name="embeds">The array of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
public Task CreateMessageAsync(Uri webhook, string content, params Embed[] embeds) =>
CreateMessageAsync(webhook, new MessageContent(content, embeds));
/// <inheritdoc cref="CreateMessageAsync(Uri, MessageContent)" />
/// <param name="webhook">The <see cref="T:Guilded.Servers.Webhook.Url">url</see> of the <see cref="T:Guilded.Servers.Webhook">webhook</see> to execute</param>
/// <param name="embeds">The array of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
public Task CreateMessageAsync(Uri webhook, params Embed[] embeds) =>
CreateMessageAsync(webhook, new MessageContent(embeds));
/// <summary>
/// Creates a <see cref="MessageContent">message</see> for every <see cref="Webhooks">webhook</see> in the <see cref="GuildedWebhookClient">client</see>.
/// </summary>
/// <param name="message">The <see cref="MessageContent">message</see> to send</param>
/// <exception cref="GuildedException" />
/// <exception cref="GuildedRequestException" />
/// <exception cref="GuildedResourceException" />
public async Task CreateMessageAsync(MessageContent message)
{
foreach (IWebhook webhook in Webhooks)
await ExecuteRequestAsync(new RestRequest(webhook.Url, Method.Post).AddBody(message)).ConfigureAwait(false);
}
/// <summary>
/// Creates a <see cref="MessageContent">message</see> for every <see cref="Webhooks">webhook</see> in the <see cref="GuildedWebhookClient">client</see>.
/// </summary>
/// <remarks>
/// <para>The text <paramref name="content" /> will be formatted in Markdown.</para>
/// </remarks>
/// <param name="content">The <see cref="MessageContent.Content">text contents</see> of the <see cref="MessageContent">message</see> in Markdown</param>
/// <param name="embeds">The list of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
/// <param name="username">The displayed name of the webhook</param>
/// <param name="avatar">The displayed profile picture of the webhook</param>
/// <exception cref="GuildedException" />
/// <exception cref="GuildedRequestException" />
/// <exception cref="GuildedResourceException" />
public Task CreateMessageAsync(string? content = null, IList<Embed>? embeds = null, string? username = null, Uri? avatar = null) =>
CreateMessageAsync(new MessageContent(content, embeds, null, username: username, avatar: avatar));
/// <inheritdoc cref="CreateMessageAsync(string, IList{Embed}, string, Uri)" />
/// <param name="content">The <see cref="MessageContent.Content">text contents</see> of the <see cref="MessageContent">message</see> in Markdown</param>
/// <param name="username">The displayed name of the webhook</param>
/// <param name="avatar">The displayed profile picture of the webhook</param>
/// <param name="embeds">The array of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
public Task CreateMessageAsync(string? content = null, string? username = null, Uri? avatar = null, params Embed[] embeds) =>
CreateMessageAsync(new MessageContent(content, embeds, null, username: username, avatar: avatar));
/// <inheritdoc cref="CreateMessageAsync(string, IList{Embed}, string, Uri)" />
/// <param name="content">The <see cref="MessageContent.Content">text contents</see> of the <see cref="MessageContent">message</see> in Markdown</param>
/// <param name="embeds">The array of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
public Task CreateMessageAsync(string content, params Embed[] embeds) =>
CreateMessageAsync(new MessageContent(content, embeds));
/// <inheritdoc cref="CreateMessageAsync(string, IList{Embed}, string, Uri)" />
/// <param name="embeds">The array of all <see cref="Embed">custom embeds</see> in the <see cref="MessageContent">message</see> (max — <c>1</c>)</param>
public Task CreateMessageAsync(params Embed[] embeds) =>
CreateMessageAsync(new MessageContent(embeds));
#endregion
}