mirror of
https://github.com/Stone-Red-Code/DiscordCLI.git
synced 2026-09-04 09:05:59 +02:00
Improved visuals and code cleanup
-Fixed mention highlighting bug -Improved embeds design -Improved code structure
This commit is contained in:
Binary file not shown.
@@ -1,12 +1,11 @@
|
||||
using DSharpPlus;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus;
|
||||
using Stone_Red_Utilities.ColorConsole;
|
||||
|
||||
using DSharpPlus.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace DiscordCLI
|
||||
{
|
||||
@@ -16,7 +15,7 @@ namespace DiscordCLI
|
||||
|
||||
private readonly Dictionary<string, (string, Action<string>)> CommandsList;
|
||||
|
||||
public CommandsManager(DiscordClient dicordClient) : base(dicordClient)
|
||||
public CommandsManager(DiscordClient dicordClient, OutputManager outputManager) : base(dicordClient, outputManager)
|
||||
{
|
||||
client = dicordClient;
|
||||
|
||||
@@ -85,9 +84,12 @@ namespace DiscordCLI
|
||||
|
||||
private readonly DiscordClient client;
|
||||
|
||||
public Commands(DiscordClient dicordClient)
|
||||
private readonly OutputManager outputManager;
|
||||
|
||||
public Commands(DiscordClient dicordClient, OutputManager outputMan)
|
||||
{
|
||||
client = dicordClient;
|
||||
outputManager = outputMan;
|
||||
}
|
||||
|
||||
protected void ListGuilds(string args)
|
||||
@@ -187,7 +189,7 @@ namespace DiscordCLI
|
||||
{
|
||||
try
|
||||
{
|
||||
await Program.WriteMessage(message, textChannel, GlobalInformation.currentGuild);
|
||||
await outputManager.WriteMessage(message, textChannel, GlobalInformation.currentGuild);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<_LastSelectedProfileId>C:\Users\David\Google Drive\Programmieren\DiscordCLI\DiscordCLI\Properties\PublishProfiles\Windows.pubxml</_LastSelectedProfileId>
|
||||
<_LastSelectedProfileId>C:\Users\David\Google Drive\Programmieren\DiscordCLI\DiscordCLI\Properties\PublishProfiles\Linux-x64.pubxml</_LastSelectedProfileId>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,9 +1,4 @@
|
||||
using DSharpPlus.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiscordCLI
|
||||
{
|
||||
@@ -11,6 +6,5 @@ namespace DiscordCLI
|
||||
{
|
||||
public static DiscordGuild currentGuild;
|
||||
public static DiscordChannel currentTextChannel;
|
||||
public static int colorMode = 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using DSharpPlus;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace DiscordCLI
|
||||
{
|
||||
internal class InputManager
|
||||
{
|
||||
private readonly CommandsManager commandsManager;
|
||||
private readonly DiscordClient client;
|
||||
public string Input { get; private set; } = "<";
|
||||
|
||||
public InputManager(DiscordClient dicordClient, CommandsManager commandsMan)
|
||||
{
|
||||
client = dicordClient;
|
||||
commandsManager = commandsMan;
|
||||
}
|
||||
|
||||
public async Task ReadInput()
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
bool exit = false;
|
||||
while (!exit)
|
||||
{
|
||||
string infoString = $"\r[{client.CurrentUser.Username}#{client.CurrentUser.Discriminator}] [{GlobalInformation.currentGuild?.Name}/{GlobalInformation.currentTextChannel?.Name}] ==> ";
|
||||
if (Input.StartsWith('<'))
|
||||
Console.Write(Environment.NewLine + infoString);
|
||||
|
||||
Input = string.Empty;
|
||||
|
||||
ConsoleKeyInfo keyInfo;
|
||||
do
|
||||
{
|
||||
keyInfo = Console.ReadKey(true);
|
||||
if (!char.IsControl(keyInfo.KeyChar))
|
||||
Input += keyInfo.KeyChar.ToString();
|
||||
|
||||
if (keyInfo.Key == ConsoleKey.Backspace && Input.Length > 0)
|
||||
{
|
||||
Input = Input.Remove(Input.Length - 1);
|
||||
}
|
||||
|
||||
Console.Write(keyInfo.KeyChar);
|
||||
if (keyInfo.Key == ConsoleKey.Backspace)
|
||||
Console.Write(" ");
|
||||
|
||||
Console.CursorLeft = infoString.Length + Input.Length - 1;
|
||||
} while (keyInfo.Key != ConsoleKey.Enter);
|
||||
|
||||
if (GlobalInformation.currentTextChannel == null && !Input.StartsWith('<'))
|
||||
Input = "<" + Input;
|
||||
|
||||
exit = commandsManager.CheckCommand(Input);
|
||||
}
|
||||
Environment.Exit(0);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
using ColorMine.ColorSpaces.Comparisons;
|
||||
using ColorMine.ColorSpaces;
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus;
|
||||
using Stone_Red_Utilities.ColorConsole;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace DiscordCLI
|
||||
{
|
||||
internal class OutputManager
|
||||
{
|
||||
private readonly DiscordClient client;
|
||||
public InputManager InputManager { get; set; }
|
||||
|
||||
public OutputManager(DiscordClient discordClient)
|
||||
{
|
||||
client = discordClient;
|
||||
}
|
||||
|
||||
public async Task WriteMessage(DiscordMessage message, DiscordChannel channel, DiscordGuild guild)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (channel.Id != GlobalInformation.currentTextChannel?.Id)
|
||||
return;
|
||||
|
||||
DiscordUser user = message.Author;
|
||||
DiscordMember discordMember = await guild.GetMemberAsync(user.Id);
|
||||
DiscordColor discordColor = discordMember.Color;
|
||||
|
||||
Color color = Color.FromArgb(discordColor.R, discordColor.G, discordColor.B);
|
||||
|
||||
WriteTop($"[{discordMember.DisplayName}]", color, message, true, true, $"{discordMember.Username}#{discordMember.Discriminator} {message.Timestamp.LocalDateTime}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(message.Content))
|
||||
WriteTop(message.Content, Color.White, message);
|
||||
|
||||
foreach (DiscordAttachment attachment in message.Attachments)
|
||||
{
|
||||
WriteTop($"{attachment.Url}", Color.White, message, true, true, attachment.FileName);
|
||||
}
|
||||
|
||||
foreach (DiscordEmbed embed in message.Embeds)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(embed.Title))
|
||||
{
|
||||
WriteTop(">>> ", Color.FromArgb(embed.Color.Value), message, true, false);
|
||||
WriteTop($"{{{embed.Title}}}", Color.White, message, false);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(embed.Description))
|
||||
{
|
||||
WriteTop(">>> ", Color.FromArgb(embed.Color.Value), message, true, false);
|
||||
WriteTop($"{embed.Description}", Color.White, message, false);
|
||||
}
|
||||
foreach (DiscordEmbedField field in embed.Fields)
|
||||
{
|
||||
WriteTop(">>> ", Color.FromArgb(embed.Color.Value), message, true, false);
|
||||
WriteTop($"{field.Name}{Environment.NewLine}{field.Value}", Color.White, message, false);
|
||||
}
|
||||
//WriteTop($"{string.Join(Environment.NewLine, embed.Fields.Select(x => $"> {x.Name}{Environment.NewLine}{x.Value}"))}", Color.White, message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
|
||||
WriteTop(string.Empty, Color.White, message);
|
||||
Console.Write($"[{client.CurrentUser.Username}#{client.CurrentUser.Discriminator}] [{GlobalInformation.currentGuild?.Name}/{GlobalInformation.currentTextChannel?.Name}] ==> ");
|
||||
Console.Write(InputManager.Input);
|
||||
}
|
||||
|
||||
private void WriteTop(string message, Color color, DiscordMessage discordMessage, bool removeText = true, bool newLine = true, string info = null)
|
||||
{
|
||||
ConsoleColor consoleColor = ClosestConsoleColor(color);
|
||||
|
||||
if (consoleColor == Console.BackgroundColor || consoleColor == ConsoleColor.DarkGray)
|
||||
consoleColor = ConsoleColor.White;
|
||||
|
||||
if (removeText)
|
||||
Console.Write('\r' + new string(' ', Console.WindowWidth) + '\r');
|
||||
|
||||
string[] words = message.Split(' ');
|
||||
for (int i = 0; i < words.Length; i++)
|
||||
{
|
||||
ConsoleColor mentionColor = ConsoleColor.Black;
|
||||
|
||||
if (IsUri(words[i]))
|
||||
{
|
||||
mentionColor = ConsoleColor.DarkCyan;
|
||||
}
|
||||
|
||||
foreach (DiscordUser user in discordMessage.MentionedUsers)
|
||||
{
|
||||
if (user?.Mention is not null)
|
||||
{
|
||||
if (words[i].Contains(user.Mention))
|
||||
{
|
||||
words[i] = words[i].Replace(user.Mention, $"@{user.Username}#{user.Discriminator}");
|
||||
mentionColor = ConsoleColor.Blue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (words[i].Contains("<@") && words[i].Contains('>'))
|
||||
mentionColor = ConsoleColor.Blue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (DiscordChannel channel in discordMessage.MentionedChannels)
|
||||
{
|
||||
if (channel?.Mention is not null)
|
||||
if (words[i].Contains(channel.Mention))
|
||||
{
|
||||
words[i] = words[i].Replace(channel.Mention, $"#{channel.Name}");
|
||||
mentionColor = ConsoleColor.Blue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (DiscordRole role in discordMessage.MentionedRoles)
|
||||
{
|
||||
if (role?.Mention is not null)
|
||||
if (words[i].Contains(role.Mention))
|
||||
{
|
||||
words[i] = words[i].Replace(role.Mention, $"@{role.Name}");
|
||||
DiscordColor discordColor = role.Color;
|
||||
mentionColor = ClosestConsoleColor(Color.FromArgb(discordColor.R, discordColor.G, discordColor.B));
|
||||
}
|
||||
}
|
||||
|
||||
ConsoleExt.Write(words[i] + " ", mentionColor == ConsoleColor.Black ? consoleColor : mentionColor);
|
||||
}
|
||||
|
||||
ConsoleExt.Write(info, ConsoleColor.DarkGray);
|
||||
|
||||
if (newLine)
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
private bool IsUri(string input)
|
||||
{
|
||||
return Uri.TryCreate(input, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
|
||||
}
|
||||
|
||||
private ConsoleColor ClosestConsoleColor(Color targetColor)
|
||||
{
|
||||
double minDif = double.MaxValue;
|
||||
ConsoleColor bestColor = ConsoleColor.White;
|
||||
|
||||
foreach (ConsoleColor consoleColor in Enum.GetValues(typeof(ConsoleColor)))
|
||||
{
|
||||
Color color = Color.FromName(consoleColor.ToString());
|
||||
|
||||
var colorA = new Rgb
|
||||
{
|
||||
R = targetColor.R,
|
||||
G = targetColor.G,
|
||||
B = targetColor.B
|
||||
};
|
||||
|
||||
var colorB = new Rgb
|
||||
{
|
||||
R = color.R,
|
||||
G = color.G,
|
||||
B = color.B
|
||||
};
|
||||
double diff = colorA.Compare(colorB, new Cie1976Comparison());
|
||||
|
||||
if (diff < minDif)
|
||||
{
|
||||
minDif = diff;
|
||||
bestColor = consoleColor;
|
||||
}
|
||||
}
|
||||
return bestColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-187
@@ -1,15 +1,8 @@
|
||||
using ColorMine.ColorSpaces;
|
||||
using ColorMine.ColorSpaces.Comparisons;
|
||||
using DSharpPlus;
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus;
|
||||
using Stone_Red_Utilities.ColorConsole;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Color = System.Drawing.Color;
|
||||
using System;
|
||||
|
||||
namespace DiscordCLI
|
||||
{
|
||||
@@ -18,10 +11,11 @@ namespace DiscordCLI
|
||||
public static void Main(string[] args)
|
||||
=> new Program().MainAsync().GetAwaiter().GetResult();
|
||||
|
||||
private static DiscordClient client;
|
||||
private DiscordClient client;
|
||||
private InputManager inputManager;
|
||||
private OutputManager outputManager;
|
||||
private CommandsManager commandsManager;
|
||||
|
||||
private static string input = "<";
|
||||
public const string tokenPath = "token.txt";
|
||||
|
||||
public async Task MainAsync()
|
||||
@@ -46,7 +40,7 @@ namespace DiscordCLI
|
||||
client = new DiscordClient(new DiscordConfiguration()
|
||||
{
|
||||
Token = token,
|
||||
TokenType = TokenType.User
|
||||
TokenType = TokenType.User,
|
||||
});
|
||||
|
||||
client.MessageCreated += Client_MessageCreated;
|
||||
@@ -62,186 +56,17 @@ namespace DiscordCLI
|
||||
return;
|
||||
}
|
||||
|
||||
commandsManager = new CommandsManager(client);
|
||||
outputManager = new OutputManager(client);
|
||||
commandsManager = new CommandsManager(client, outputManager);
|
||||
inputManager = new InputManager(client, commandsManager);
|
||||
outputManager.InputManager = inputManager;
|
||||
|
||||
await ReadInput();
|
||||
await Task.Delay(-1);
|
||||
await inputManager.ReadInput();
|
||||
}
|
||||
|
||||
private async Task Client_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs e)
|
||||
{
|
||||
await WriteMessage(e.Message, e.Channel, e.Guild);
|
||||
}
|
||||
|
||||
public static async Task WriteMessage(DiscordMessage message, DiscordChannel channel, DiscordGuild guild)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (channel.Id != GlobalInformation.currentTextChannel?.Id)
|
||||
return;
|
||||
DiscordUser user = message.Author;
|
||||
DiscordMember discordMember = await guild.GetMemberAsync(user.Id);
|
||||
DiscordColor discordColor = discordMember.Color;
|
||||
|
||||
Color color = Color.FromArgb(discordColor.R, discordColor.G, discordColor.B);
|
||||
|
||||
WriteTop($"[{discordMember.DisplayName}]", color, message, $"{discordMember.Username}#{discordMember.Discriminator} {message.Timestamp.LocalDateTime}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(message.Content))
|
||||
WriteTop(message.Content, Color.White, message);
|
||||
|
||||
foreach (DiscordAttachment attachment in message.Attachments)
|
||||
{
|
||||
WriteTop($"{attachment.Url}", Color.White, message, attachment.FileName);
|
||||
}
|
||||
foreach (DiscordEmbed embed in message.Embeds)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(embed.Title))
|
||||
WriteTop($">>> {{{embed.Title}}}", Color.FromArgb(embed.Color.Value), message);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(embed.Description))
|
||||
WriteTop($">> {embed.Description}", Color.White, message);
|
||||
|
||||
WriteTop($"{string.Join(Environment.NewLine, embed.Fields.Select(x => $">{x.Name}{Environment.NewLine}{x.Value}"))}", Color.White, message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
|
||||
WriteTop(string.Empty, Color.White, message);
|
||||
Console.Write($"[{client.CurrentUser.Username}#{client.CurrentUser.Discriminator}] [{GlobalInformation.currentGuild?.Name}/{GlobalInformation.currentTextChannel?.Name}] ==> ");
|
||||
Console.Write(input);
|
||||
}
|
||||
|
||||
private async Task ReadInput()
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
bool exit = false;
|
||||
while (!exit)
|
||||
{
|
||||
string infoString = $"\r[{client.CurrentUser.Username}#{client.CurrentUser.Discriminator}] [{GlobalInformation.currentGuild?.Name}/{GlobalInformation.currentTextChannel?.Name}] ==> ";
|
||||
if (input.StartsWith('<'))
|
||||
Console.Write(Environment.NewLine + infoString);
|
||||
|
||||
input = string.Empty;
|
||||
|
||||
ConsoleKeyInfo keyInfo;
|
||||
do
|
||||
{
|
||||
keyInfo = Console.ReadKey(true);
|
||||
if (!char.IsControl(keyInfo.KeyChar))
|
||||
input += keyInfo.KeyChar.ToString();
|
||||
|
||||
if (keyInfo.Key == ConsoleKey.Backspace)
|
||||
{
|
||||
if (input.Length > 0)
|
||||
{
|
||||
input = input.Remove(input.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
Console.Write(keyInfo.KeyChar);
|
||||
if (keyInfo.Key == ConsoleKey.Backspace)
|
||||
Console.Write(" ");
|
||||
|
||||
Console.CursorLeft = infoString.Length + input.Length - 1;
|
||||
} while (keyInfo.Key != ConsoleKey.Enter);
|
||||
|
||||
if (GlobalInformation.currentTextChannel == null && !input.StartsWith('<'))
|
||||
input = "<" + input;
|
||||
|
||||
exit = commandsManager.CheckCommand(input);
|
||||
}
|
||||
Environment.Exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
private static void WriteTop(string message, Color color, DiscordMessage discordMessage, string info = null)
|
||||
{
|
||||
ConsoleColor consoleColor = ClosestConsoleColor3(color);
|
||||
|
||||
if (consoleColor == Console.BackgroundColor || consoleColor == ConsoleColor.DarkGray)
|
||||
consoleColor = ConsoleColor.White;
|
||||
|
||||
//Console.SetCursorPosition(0, Console.WindowTop + Console.WindowHeight - 1);
|
||||
|
||||
Console.Write('\r' + new string(' ', Console.WindowWidth) + '\r');
|
||||
|
||||
string[] words = message.Split(' ');
|
||||
for (int i = 0; i < words.Length; i++)
|
||||
{
|
||||
ConsoleColor mentionColor = ConsoleColor.Black;
|
||||
foreach (DiscordUser user in discordMessage.MentionedUsers)
|
||||
{
|
||||
if (user?.Mention is not null)
|
||||
if (words[i].Contains(user.Mention))
|
||||
{
|
||||
words[i] = words[i].Replace(user.Mention, $"@{user.Username}#{user.Discriminator}");
|
||||
mentionColor = ConsoleColor.Blue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (DiscordChannel channel in discordMessage.MentionedChannels)
|
||||
{
|
||||
if (channel?.Mention is not null)
|
||||
if (words[i].Contains(channel.Mention))
|
||||
{
|
||||
words[i] = words[i].Replace(channel.Mention, $"#{channel.Name}");
|
||||
mentionColor = ConsoleColor.Blue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (DiscordRole role in discordMessage.MentionedRoles)
|
||||
{
|
||||
if (role?.Mention is not null)
|
||||
if (words[i].Contains(role.Mention))
|
||||
{
|
||||
words[i] = words[i].Replace(role.Mention, $"@{role.Name}");
|
||||
DiscordColor discordColor = role.Color;
|
||||
mentionColor = ClosestConsoleColor3(Color.FromArgb(discordColor.R, discordColor.G, discordColor.B));
|
||||
}
|
||||
}
|
||||
|
||||
ConsoleExt.Write(words[i] + " ", mentionColor == ConsoleColor.Black ? consoleColor : mentionColor);
|
||||
}
|
||||
|
||||
ConsoleExt.WriteLine(" " + info, ConsoleColor.DarkGray);
|
||||
}
|
||||
|
||||
public static ConsoleColor ClosestConsoleColor3(Color targetColor)
|
||||
{
|
||||
double minDif = double.MaxValue;
|
||||
ConsoleColor bestColor = ConsoleColor.White;
|
||||
|
||||
foreach (ConsoleColor consoleColor in Enum.GetValues(typeof(ConsoleColor)))
|
||||
{
|
||||
Color color = Color.FromName(consoleColor.ToString());
|
||||
|
||||
var colorA = new Rgb
|
||||
{
|
||||
R = targetColor.R,
|
||||
G = targetColor.G,
|
||||
B = targetColor.B
|
||||
};
|
||||
|
||||
var colorB = new Rgb
|
||||
{
|
||||
R = color.R,
|
||||
G = color.G,
|
||||
B = color.B
|
||||
};
|
||||
double diff = colorA.Compare(colorB, new Cie1976Comparison());
|
||||
|
||||
if (diff < minDif)
|
||||
{
|
||||
minDif = diff;
|
||||
bestColor = consoleColor;
|
||||
}
|
||||
}
|
||||
return bestColor;
|
||||
await outputManager.WriteMessage(e.Message, e.Channel, e.Guild);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
|
||||
dfd104565a5c51e51c96fc2a0f9e9b9515e39127
|
||||
c330ac1ec415f28e8dfa881f69ac743ee03b156f
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user