6 Commits
Author SHA1 Message Date
Stone-Red-Code 992dcf832b Added releases for "Improved visuals and code cleanup" commit 2021-04-12 22:36:57 +02:00
Stone-Red-Code d3117795d3 Merge branch 'main' of https://github.com/Stone-Red-Code/DiscordCLI into main 2021-04-12 22:30:03 +02:00
Stone_Red 144869d5ad Create README.md 2021-04-12 22:29:28 +02:00
Stone-Red-Code bd5384f356 Improved visuals and code cleanup
-Fixed mention highlighting bug
-Improved embeds design
-Improved code structure
2021-04-12 22:28:02 +02:00
Stone_Red 9ad6a23305 Create LICENSE 2021-04-12 12:52:56 +02:00
Stone-Red-Code da2fa6f581 Remove unnecessary async keyword 2021-04-12 12:49:56 +02:00
69 changed files with 297 additions and 3932 deletions
Binary file not shown.
+11 -9
View File
@@ -1,12 +1,11 @@
using DSharpPlus; using DSharpPlus.Entities;
using System; using DSharpPlus;
using System.Collections.Generic;
using System.Linq;
using Stone_Red_Utilities.ColorConsole; using Stone_Red_Utilities.ColorConsole;
using System.Collections.Generic;
using DSharpPlus.Entities;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System;
namespace DiscordCLI namespace DiscordCLI
{ {
@@ -16,7 +15,7 @@ namespace DiscordCLI
private readonly Dictionary<string, (string, Action<string>)> CommandsList; 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; client = dicordClient;
@@ -85,9 +84,12 @@ namespace DiscordCLI
private readonly DiscordClient client; private readonly DiscordClient client;
public Commands(DiscordClient dicordClient) private readonly OutputManager outputManager;
public Commands(DiscordClient dicordClient, OutputManager outputMan)
{ {
client = dicordClient; client = dicordClient;
outputManager = outputMan;
} }
protected void ListGuilds(string args) protected void ListGuilds(string args)
@@ -187,7 +189,7 @@ namespace DiscordCLI
{ {
try try
{ {
await Program.WriteMessage(message, textChannel, GlobalInformation.currentGuild); await outputManager.WriteMessage(message, textChannel, GlobalInformation.currentGuild);
} }
catch (Exception ex) catch (Exception ex)
{ {
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<_LastSelectedProfileId>C:\Users\David\Google Drive\Programmieren\DiscordCLI\DiscordCLI\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId> <_LastSelectedProfileId>C:\Users\David\Google Drive\Programmieren\DiscordCLI\DiscordCLI\Properties\PublishProfiles\Linux-x64.pubxml</_LastSelectedProfileId>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
-6
View File
@@ -1,9 +1,4 @@
using DSharpPlus.Entities; using DSharpPlus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordCLI namespace DiscordCLI
{ {
@@ -11,6 +6,5 @@ namespace DiscordCLI
{ {
public static DiscordGuild currentGuild; public static DiscordGuild currentGuild;
public static DiscordChannel currentTextChannel; public static DiscordChannel currentTextChannel;
public static int colorMode = 1;
} }
} }
+60
View File
@@ -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);
});
}
}
}
+183
View File
@@ -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
View File
@@ -1,15 +1,8 @@
using ColorMine.ColorSpaces; using DSharpPlus;
using ColorMine.ColorSpaces.Comparisons;
using DSharpPlus;
using DSharpPlus.Entities;
using Stone_Red_Utilities.ColorConsole; using Stone_Red_Utilities.ColorConsole;
using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System;
using Color = System.Drawing.Color;
namespace DiscordCLI namespace DiscordCLI
{ {
@@ -18,10 +11,11 @@ namespace DiscordCLI
public static void Main(string[] args) public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult(); => new Program().MainAsync().GetAwaiter().GetResult();
private static DiscordClient client; private DiscordClient client;
private InputManager inputManager;
private OutputManager outputManager;
private CommandsManager commandsManager; private CommandsManager commandsManager;
private static string input = "<";
public const string tokenPath = "token.txt"; public const string tokenPath = "token.txt";
public async Task MainAsync() public async Task MainAsync()
@@ -46,7 +40,7 @@ namespace DiscordCLI
client = new DiscordClient(new DiscordConfiguration() client = new DiscordClient(new DiscordConfiguration()
{ {
Token = token, Token = token,
TokenType = TokenType.User TokenType = TokenType.User,
}); });
client.MessageCreated += Client_MessageCreated; client.MessageCreated += Client_MessageCreated;
@@ -62,186 +56,17 @@ namespace DiscordCLI
return; 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 inputManager.ReadInput();
await Task.Delay(-1);
} }
private async Task Client_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs e) private async Task Client_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs e)
{ {
await WriteMessage(e.Message, e.Channel, e.Guild); await outputManager.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 async 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;
} }
} }
} }
@@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup> <PropertyGroup>
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>Any CPU</Platform> <Platform>Any CPU</Platform>
<PublishDir>bin\Release\net5.0\publish\</PublishDir> <PublishDir>bin\Release\net5.0\publish\Linux-x64</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol> <PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier> <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
@@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup> <PropertyGroup>
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>Any CPU</Platform> <Platform>Any CPU</Platform>
<PublishDir>bin\Release\net5.0\publish\</PublishDir> <PublishDir>bin\Release\net5.0\publish\Windows</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol> <PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<SelfContained>false</SelfContained> <SelfContained>false</SelfContained>
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.
File diff suppressed because it is too large Load Diff
@@ -1,9 +0,0 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -1,9 +0,0 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
Binary file not shown.
File diff suppressed because it is too large Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,9 +0,0 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
Binary file not shown.
@@ -1 +1 @@
dfd104565a5c51e51c96fc2a0f9e9b9515e39127 c330ac1ec415f28e8dfa881f69ac743ee03b156f
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
3ab14035ed25e46cb2eb046c245e2f90325a2743 f577cd7c0c9371a4e25c13c1586e47e82f16d417
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
6843f3cabf894dfce1aaffe6c65e64e8531c52b6 c27366fe7586ce3c313a2245960fd7296c163460
Binary file not shown.
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Stone_Red
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+4
View File
@@ -0,0 +1,4 @@
# DiscordCLI
> Use Discord in the terminal
# README coming soon