Skip to content

Commit

Permalink
Minor code edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
h3xb0y committed Oct 14, 2021
1 parent 6f7ac06 commit ec0d142
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
18 changes: 15 additions & 3 deletions Controllers/GetRandomEmojiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ namespace SlackThrowReaction.Controllers
[Route("[controller]")]
public class GetRandomEmojiController : ControllerBase
{
public GetRandomEmojiController(ILogger<GetRandomEmojiController> _)
private readonly ILogger<GetRandomEmojiController> _logger;

public GetRandomEmojiController(ILogger<GetRandomEmojiController> logger)
{
_logger = logger;
}

[
Expand All @@ -22,18 +25,27 @@ public GetRandomEmojiController(ILogger<GetRandomEmojiController> _)
]
public async Task<JsonResult> Post([FromForm] SlashCommandPayload data)
{
var emoji = data.Text?.ToLower();
var emoji = data.Text.ToLower();

if (string.IsNullOrEmpty(emoji))
{
return new JsonResult(new
{
response_type = "ephemeral",
text = "emoji not found bruh :c"
text = "your input is incorrect"
});
}

var emojiInfo = await EmojiStorage.Get(emoji);
if (emojiInfo == null)
{

return new JsonResult(new
{
response_type = "ephemeral",
text = "emoji not found"
});
}
var imageUrl = $"https://cdn.betterttv.net/emote/{emojiInfo.Id}/3x";
var imageData = new ImageData {Emoji = emojiInfo.Code, IconUrl = imageUrl, SearchingEmoji = emoji};
var imageDataJson = JsonConvert.SerializeObject(imageData);
Expand Down
26 changes: 11 additions & 15 deletions Model/Model.cs → Model/SlackModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public sealed class SlashActionPayload
[JsonProperty("type")] public string Type { get; set; }

[JsonProperty("token")] public string Token { get; set; }

[JsonProperty("response_url")] public string ResponseUrl { get; set; }

[JsonProperty("user")] public User User { get; set; }

[JsonProperty("actions")] public Action[] Actions { get; set; }
Expand All @@ -52,25 +52,21 @@ public sealed class Action
public sealed class ImageData
{
[JsonProperty("emoji")] public string Emoji { get; set; }

[JsonProperty("searchemoji")] public string SearchingEmoji { get; set; }

[JsonProperty("url")] public string IconUrl { get; set; }
}

public sealed class User
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("username")]
public string Username { get; set; }

[JsonProperty("team_id")]
public string TeamId { get; set; }
[JsonProperty("id")] public string Id { get; set; }

[JsonProperty("name")] public string Name { get; set; }

[JsonProperty("username")] public string Username { get; set; }

[JsonProperty("team_id")] public string TeamId { get; set; }
}

public sealed class SlackReply
Expand Down
5 changes: 0 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

Expand Down
11 changes: 5 additions & 6 deletions ResponseElements/EmojiStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ public static class EmojiStorage
{
private static readonly Random Random = new Random();

private static readonly Dictionary<string, List<EmojiInfo>> EmojiesByText =
new Dictionary<string, List<EmojiInfo>>();
private static readonly Dictionary<string, List<EmojiInfo>> EmojiesByText = new();

public static async Task<EmojiInfo> Get(string emoji, bool force = false)
public static async Task<EmojiInfo?> Get(string emoji, bool force = false)
{
if (!EmojiesByText.TryGetValue(emoji, out var emojies))
{
var apiUrl = $"https://api.betterttv.net/3/emotes/shared/search?query={emoji}&offset=0&limit=30";
var result = "";
string result;

var request = (HttpWebRequest) WebRequest.Create(apiUrl);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Expand All @@ -36,10 +35,10 @@ public static async Task<EmojiInfo> Get(string emoji, bool force = false)

if (emojies == null)
return null;

EmojiesByText.Add(emoji, emojies);
}

var index = Random.Next(emojies.Count);
var emojiInfo = emojies[index];

Expand Down
3 changes: 1 addition & 2 deletions ResponseElements/SlackResponseManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using SlackThrowReaction.Model;
using SlackThrowReaction.Model;

namespace SlackThrowReaction.ResponseElements
{
Expand Down
1 change: 1 addition & 0 deletions SlackThrowReaction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit ec0d142

Please sign in to comment.