-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Initialize-A-Baguette/Discord_Interactive
Add discord interactive
- Loading branch information
Showing
9 changed files
with
236 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
ToF_Fishing_Bot/Addon/DiscordInteractive/DiscordService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace ToF_Fishing_Bot.Addon.DiscordInteractive; | ||
|
||
|
||
/// <summary> | ||
/// Contain all interactive function with Discord | ||
/// </summary> | ||
public class DiscordService : IDiscordService | ||
{ | ||
private readonly string _hookUrl; | ||
private readonly string? _mentionId; | ||
private readonly HttpClient _httpClient = new(); | ||
private readonly DateTime EpochTime = new(1970, 1, 1); | ||
|
||
/// <summary> | ||
/// Create an instance of <see cref="DiscordService"/> | ||
/// </summary> | ||
/// <param name="hookUrl">Webhook URL. You can see how to get webhook url at: <see href="https://support.discord.com/hc/en-us/articles/228383668"/></param> | ||
/// <param name="mentionId">Discord user Id.</param> | ||
/// <exception cref="ArgumentException"></exception> | ||
public DiscordService(string hookUrl, string? mentionId = null) | ||
{ | ||
if (Uri.IsWellFormedUriString(hookUrl, UriKind.Absolute)) | ||
{ | ||
var uri = new Uri(hookUrl); | ||
if (!uri.Host.Contains("discord.com", StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
throw new ArgumentException("Discord HookUrl provided is not valid"); | ||
} | ||
if (!uri.AbsolutePath.Contains("webhooks", StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
throw new ArgumentException("Discord HookUrl provided is not valid"); | ||
} | ||
_hookUrl = hookUrl; | ||
|
||
} | ||
else | ||
{ | ||
throw new ArgumentException("Discord HookUrl provided is not valid"); | ||
} | ||
_mentionId = mentionId; | ||
} | ||
|
||
/// <summary> | ||
/// Send message to specific webhook above | ||
/// </summary> | ||
/// <param name="content">Message content</param> | ||
/// <returns></returns> | ||
public Task SendMessage(HookContent content) | ||
{ | ||
var postContent = JsonContent.Create(content, new MediaTypeWithQualityHeaderValue("application/json"), new JsonSerializerOptions() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
}); | ||
return _httpClient.PostAsync(_hookUrl, postContent); | ||
} | ||
|
||
/// <summary> | ||
/// Build a message about ran out of bait | ||
/// </summary> | ||
/// <param name="fishingStartAt">Time that user clicked to Star fishing button, provide it in UTC</param> | ||
/// <returns></returns> | ||
public Task<HookContent> BuildOutOfBaitNotification(DateTime fishingStartAt) | ||
{ | ||
var strBuild = new StringBuilder("Hello"); | ||
if (!string.IsNullOrEmpty(_mentionId)) | ||
{ | ||
strBuild.Append($"<@!{_mentionId}>"); | ||
} | ||
strBuild.AppendLine(". Thank for using fishing bot."); | ||
strBuild.AppendLine("This program is open source and free to use [here](https://github.com/mdnpascual/ToF-Fishing-Bot)."); | ||
var hookContent = new HookContent() | ||
{ | ||
Content = strBuild.ToString(), | ||
Embeds = new List<HookEmbedContent>() | ||
{ | ||
new HookEmbedContent() | ||
{ | ||
Color = 5814783, | ||
Title = "Ran out of bait", | ||
Description = "This message is sent because ran out of bait. Fishing Session detail below:", | ||
Fields = new List<HookEmbedField>() | ||
{ | ||
new HookEmbedField("Start at:", $"<t:{(int)(fishingStartAt - EpochTime).TotalSeconds}:f>", true), | ||
new HookEmbedField("End at:", $"<t:{(int)(DateTime.UtcNow - EpochTime).TotalSeconds}:f>", true), | ||
}, | ||
} | ||
}, | ||
Footer = new HookEmbedFooter() | ||
{ | ||
Text = "TOF Fishing Bot" | ||
} | ||
}; | ||
return Task.FromResult(hookContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ToF_Fishing_Bot.Addon.DiscordInteractive; | ||
|
||
public class HookContent | ||
{ | ||
public string Content { get; set; } = string.Empty; | ||
public IList<HookEmbedContent> Embeds { get; set; } = new List<HookEmbedContent>(); | ||
public HookEmbedFooter? Footer { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
ToF_Fishing_Bot/Addon/DiscordInteractive/HookEmbedContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ToF_Fishing_Bot.Addon.DiscordInteractive; | ||
|
||
public class HookEmbedContent | ||
{ | ||
public string Title { get; set; } | ||
public string Description { get; set; } | ||
public long Color { get; set; } | ||
public IList<HookEmbedField> Fields { get; set; } = new List<HookEmbedField>(); | ||
} |
15 changes: 15 additions & 0 deletions
15
ToF_Fishing_Bot/Addon/DiscordInteractive/HookEmbedField.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace ToF_Fishing_Bot.Addon.DiscordInteractive; | ||
|
||
public class HookEmbedField | ||
{ | ||
public HookEmbedField(string name, string value, bool inline = false) | ||
{ | ||
Name = name; | ||
Value = value; | ||
Inline = inline; | ||
} | ||
|
||
public string Name { get; set; } = string.Empty; | ||
public string Value { get; set; } = string.Empty; | ||
public bool Inline { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace ToF_Fishing_Bot.Addon.DiscordInteractive; | ||
|
||
public class HookEmbedFooter | ||
{ | ||
public string Text { get; set; } = string.Empty; | ||
public DateTime? Timestamp { get; set; } | ||
} |
13 changes: 13 additions & 0 deletions
13
ToF_Fishing_Bot/Addon/DiscordInteractive/IDiscordService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ToF_Fishing_Bot.Addon.DiscordInteractive; | ||
|
||
public interface IDiscordService | ||
{ | ||
public Task SendMessage(HookContent content); | ||
public Task<HookContent> BuildOutOfBaitNotification(DateTime fishingStartAt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters