Skip to content

Commit

Permalink
Add modern styling
Browse files Browse the repository at this point in the history
  • Loading branch information
1zc committed Feb 15, 2024
1 parent 93f1437 commit 87686f0
Showing 1 changed file with 20 additions and 46 deletions.
66 changes: 20 additions & 46 deletions src/DiscordChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ You should have received a copy of the GNU General Public License

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Collections.Concurrent;
using RestSharp;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.Json.Nodes;

namespace DiscordChat;

Expand All @@ -53,6 +51,9 @@ public partial class DiscordChat : BasePlugin, IPluginConfig<DiscordChatConfig>
public override string ModuleDescription => "Plugin to relay in-game text chat to a webhook. https://github.com/1zc/CS2-Discord-Chat";
public override string ModuleAuthor => "Liam C. (Infra)";

// Global
public Dictionary<ulong, string> steamAvatars = new Dictionary<ulong, string>();

// Configuraton
public DiscordChatConfig Config { get; set; } = new DiscordChatConfig();
public void OnConfigParsed(DiscordChatConfig config)
Expand All @@ -67,9 +68,6 @@ public void OnConfigParsed(DiscordChatConfig config)
Config = config;
}

// Global
public Dictionary<ulong, string> steamAvatars = new Dictionary<ulong, string>();

public class DiscordMessage
{
/// <summary>
Expand Down Expand Up @@ -125,6 +123,12 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
return HookResult.Continue;
}

public void OnMapEnd()
{
// Clear/reset stuff here
steamAvatars.Clear();
}

public async Task SendToDiscord(string text, CCSPlayerController player)
{
// Craft Webhook
Expand All @@ -135,13 +139,8 @@ public async Task SendToDiscord(string text, CCSPlayerController player)

DiscordMessage message = new DiscordMessage{content = $"{player.PlayerName!} (`{player.SteamID!}`): `"+text+"`"};

// string avatar = steamAvatars[player.SteamID];
// Console.WriteLine("DEBUG >> " + Config.DiscordChatStyle);
Server.NextFrame(() => player.PrintToChat($"CAN U SEE THIS {steamAvatars.ContainsKey(player.SteamID)}"));
if (Config.DiscordChatStyle == 1)
{
// Console.WriteLine("DEBUG >> " + steamAvatars[player.SteamID]);
// To-do: Implement modern styling
message = new DiscordMessage
{
content = "`"+text+"`",
Expand All @@ -165,6 +164,8 @@ public override void Load(bool hotReload)
// Hook chat listeners
AddCommandListener("say", OnCommandSay);
AddCommandListener("say_team", OnCommandSay);

RegisterListener<Listeners.OnMapEnd>(OnMapEnd);
}

private string SanitiseMessage(string message)
Expand All @@ -176,50 +177,23 @@ private string SanitiseMessage(string message)
return message;
}

public class SteamResponse
{
public SteamAPIPlayer[]? players { get; set; }
}

public class SteamAPIPlayer
{
public string? steamid { get; set; }
public int? communityvisibilitystate { get; set; }
public int? profilestate { get; set; }
public string? personaname { get; set; }
public int? commentpermission { get; set; }
public string? profileurl { get; set; }
public string? avatar { get; set; }
public string? avatarmedium { get; set; }
public string? avatarfull { get; set; }
public string? personastate { get; set; }
public string? lastlogoff { get; set; }
public string? realname { get; set; }
public string? primaryclanid { get; set; }
public string? timecreated { get; set; }
public string? personastateflags { get; set; }
public string? loccountrycode { get; set; }
public string? locstatecode { get; set; }
public string? loccityid { get; set; }
}

private async Task GetSteamAvatar(ulong steamID)
public async Task GetSteamAvatar(ulong steamID)
{
// To-do: Implement Steam API
RestClient SteamClient = new RestClient("https://api.steampowered.com");
RestRequest SteamRequest = new RestRequest($"/ISteamUser/GetPlayerSummaries/v2/?key={Config.DiscordChatSteamKey}&steamids={steamID}&format=json", Method.Get);

// Send request
RestResponse SteamResponseObj = await SteamClient.GetAsync(SteamRequest);
SteamResponse response = JsonSerializer.Deserialize<SteamResponse>(SteamResponseObj.Content!)!;
RestResponse SteamResponseRaw = await SteamClient.GetAsync(SteamRequest);
JsonObject SteamResponse = JsonSerializer.Deserialize<JsonObject>(SteamResponseRaw.Content!)!;

// Parse response
if (response != null)
if (SteamResponse != null)
{
// if (steamAvatars.ContainsKey(steamID))
// return;
steamAvatars.Add(steamID, response.players![0].avatarfull!);
if (steamAvatars.ContainsKey(steamID))
return;

Server.NextFrame(() => steamAvatars.Add(steamID, SteamResponse["response"]!["players"]![0]!["avatarfull"]!.ToString()));
}

else
Expand Down

0 comments on commit 87686f0

Please sign in to comment.