23 Commits
Author SHA1 Message Date
Stone-Red-Code 57215db498 #2 DiscordCLI not sending capital letters. 2021-05-27 07:53:54 +02:00
Stone_Red 76845b9ae0 Update README.md 2021-05-01 20:41:58 +02:00
Stone_Red 31b4f9cc44 Update README.md 2021-04-16 12:00:23 +02:00
Stone_Red 524ed14b1f Update README.md 2021-04-14 14:36:49 +02:00
Stone_Red 8d93a9de2c Update README.md 2021-04-14 14:36:20 +02:00
Stone_Red c6ba3a434d Merge pull request #1 from Stone-Red-Code/develop
Develop
2021-04-14 12:06:08 +02:00
Stone-Red-Code fc3d170e07 Added new releases 2021-04-14 12:03:24 +02:00
Stone-Red-Code ce4fb237b3 Update Program.cs 2021-04-14 11:54:10 +02:00
Stone-Red-Code f0fcbc03c5 Made more code async and added request cooldown 2021-04-14 11:53:53 +02:00
Stone_Red f53d614fca Added Limitations sections to README 2021-04-13 21:18:09 +02:00
Stone_Red 74f40593ba Added exclamation mark 2021-04-13 11:51:41 +02:00
Stone_Red 7521d9053d Update README.md 2021-04-13 11:48:58 +02:00
Stone_Red b38433badc Updated User Token link in README 2021-04-13 11:12:55 +02:00
Stone_Red 890625e6da Added description to README 2021-04-13 11:09:58 +02:00
Stone-Red-Code 433a71e838 Merge branch 'main' of https://github.com/Stone-Red-Code/DiscordCLI into main 2021-04-13 10:18:15 +02:00
Stone-Red-Code 1ddd35e5a4 Added DM support
- Added DM support
- Improved Token input
- Put the Commands class in its own file
2021-04-13 10:18:10 +02:00
Stone_Red 12271b760b Update README.md 2021-04-12 22:50:44 +02:00
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
113 changed files with 671 additions and 4267 deletions
Binary file not shown.
+187
View File
@@ -0,0 +1,187 @@
using DSharpPlus.Entities;
using DSharpPlus;
using Stone_Red_Utilities.ColorConsole;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System;
using System.Threading.Tasks;
namespace DiscordCLI
{
internal class Commands
{
private readonly DiscordClient client;
private readonly OutputManager outputManager;
public Commands(DiscordClient dicordClient, OutputManager outputMan)
{
client = dicordClient;
outputManager = outputMan;
}
protected async Task ListGuilds(string args)
{
int index = 1;
foreach (DiscordGuild guild in client.Guilds.Values)
{
Console.WriteLine($"{index}. {guild.Name}");
index++;
}
await Task.CompletedTask;
}
protected async Task ListDms(string args)
{
int index = 1;
foreach (DiscordDmChannel dmChannel in client.PrivateChannels)
{
Console.WriteLine($"{index}. {string.Join(", ", dmChannel.Recipients.Select(x => x.Username))}");
index++;
}
await Task.CompletedTask;
}
protected async Task ListGuildChannels(string args)
{
IReadOnlyCollection<DiscordChannel> textChannels;
DiscordGuild guild;
if (args != null)
{
if (int.TryParse(args, out int ind))
{
guild = client.Guilds?.Values.ElementAtOrDefault(ind - 1);
}
else
{
guild = client.Guilds?.FirstOrDefault(x => x.Value.Name == args).Value;
}
GlobalInformation.currentTextChannel = null;
}
else
{
guild = GlobalInformation.currentGuild;
}
if (guild is null)
{
ConsoleExt.WriteLine("Guild not found!", ConsoleColor.Red);
GlobalInformation.currentTextChannel = null;
return;
}
GlobalInformation.currentGuild = guild;
textChannels = guild.Channels;
int index = 1;
foreach (DiscordChannel channel in textChannels)
{
switch (channel.Type)
{
case ChannelType.Category:
//Console.WriteLine($"[{channel.Name}]");
break;
case ChannelType.Text:
Console.WriteLine($" {index}. {channel.Name}");
index++;
break;
}
}
await Task.CompletedTask;
}
protected async Task EnterChannel(string args)
{
DiscordChannel textChannel;
if (GlobalInformation.currentGuild is null)
{
ConsoleExt.WriteLine("You are not in a guild!", ConsoleColor.Red);
return;
}
if (int.TryParse(args, out int ind))
{
textChannel = GlobalInformation.currentGuild?.Channels.Where(x => x.Type == ChannelType.Text).ElementAtOrDefault(ind - 1);
}
else
{
textChannel = GlobalInformation.currentGuild?.Channels.Where(x => x.Type == ChannelType.Text).FirstOrDefault(x => x.Name == args);
}
GlobalInformation.currentTextChannel = textChannel;
if (textChannel is null)
{
ConsoleExt.WriteLine("Channel not found!", ConsoleColor.Red);
return;
}
try
{
foreach (DiscordMessage message in (textChannel.GetMessagesAsync(10).Result).Reverse())
{
await outputManager.WriteMessage(message, textChannel, GlobalInformation.currentGuild, false);
}
}
catch (Exception ex)
{
ConsoleExt.WriteLine(ex.Message, ConsoleColor.Red);
GlobalInformation.currentTextChannel = null;
}
await Task.CompletedTask;
}
protected async Task EnterDmChannel(string args)
{
DiscordDmChannel dmChannel;
GlobalInformation.currentGuild = null;
if (int.TryParse(args, out int ind))
{
dmChannel = client.PrivateChannels.ElementAtOrDefault(ind - 1);
}
else
{
dmChannel = client.PrivateChannels.FirstOrDefault(x => x.Name == args);
}
GlobalInformation.currentTextChannel = dmChannel;
if (dmChannel is null)
{
ConsoleExt.WriteLine("Channel not found!", ConsoleColor.Red);
return;
}
try
{
foreach (DiscordMessage message in (await dmChannel.GetMessagesAsync(10)).Reverse())
{
await outputManager.WriteMessage(message, dmChannel, GlobalInformation.currentGuild, false);
}
}
catch (Exception ex)
{
ConsoleExt.WriteLine(ex, ConsoleColor.Red);
GlobalInformation.currentTextChannel = null;
}
}
protected async Task DeleteToken(string args)
{
File.Delete(Program.tokenPath);
Environment.Exit(0);
await Task.CompletedTask;
}
}
}
+49 -145
View File
@@ -1,12 +1,10 @@
using DSharpPlus;
using System;
using Stone_Red_Utilities.ColorConsole;
using System.Collections.Generic;
using System.Linq;
using Stone_Red_Utilities.ColorConsole;
using DSharpPlus.Entities;
using System.Diagnostics;
using System.IO;
using System;
using System.Timers;
using System.Threading.Tasks;
namespace DiscordCLI
{
@@ -14,9 +12,10 @@ namespace DiscordCLI
{
private readonly DiscordClient client;
private readonly Dictionary<string, (string, Action<string>)> CommandsList;
private readonly Dictionary<string, (string, Func<string, Task>)> CommandsList;
private int cooldown = 0;
public CommandsManager(DiscordClient dicordClient) : base(dicordClient)
public CommandsManager(DiscordClient dicordClient, OutputManager outputManager) : base(dicordClient, outputManager)
{
client = dicordClient;
@@ -25,42 +24,71 @@ namespace DiscordCLI
{ "exit", ("exits application", null) },
{ "logout", ("deletes auth token and exits application", DeleteToken) },
{ "guilds", ("lists all guilds you are in", ListGuilds) },
{ "dms", ("lists all private channels (Not implemented yet)", ListDms) },
{ "dms", ("lists all private channels", ListDms) },
{ "channels", ("lists all channels of guild args:<guild name/index>", ListGuildChannels) },
{ "enterg", ("enter guild args:<guild name/index>", ListGuildChannels) },
{ "enterc", ("enter chat args:<channel name/index>", EnterChannel) },
{ "enterc", ("enter channel args:<channel name/index>", EnterChannel) },
{ "enterd", ("enter DM channel args:<channel name/index>", EnterDmChannel) },
};
Timer cooldownTimer = new Timer(1000);
cooldownTimer.Elapsed += CooldownTimer_Elapsed;
cooldownTimer.Start();
}
public bool CheckCommand(string input)
private void CooldownTimer_Elapsed(object sender, ElapsedEventArgs e)
{
input = input.Trim().ToLower().Replace('\n', '\0');
if (cooldown > 0)
cooldown--;
}
/// <summary>
/// Check the command and return true if the program should exit
/// </summary>
/// <param name="input"></param>
/// <returns>exit bool and write override bool</returns>
public async Task<(bool, bool)> CheckCommand(string rawInput)
{
cooldown++;
if (cooldown > 1)
{
Console.WriteLine();
ConsoleExt.WriteLine("You are beeing rate limited!", ConsoleColor.Yellow);
ConsoleExt.WriteLine("Wait a few seconds before making another request!", ConsoleColor.Yellow);
return (false, true);
}
string input = rawInput.Trim().ToLower().Replace('\n', '\0');
if (string.IsNullOrWhiteSpace(input))
return false;
return (false, false);
if (input.StartsWith('<'))
if (input.StartsWith(InputManager.prefix))
{
input = input.Remove(0, 1);
}
else if (GlobalInformation.currentTextChannel != null)
{
Console.CursorTop--;
cooldown++;
Console.Write("\r" + new string(' ', Console.WindowWidth));
GlobalInformation.currentTextChannel.SendMessageAsync(input);
return false;
await GlobalInformation.currentTextChannel.SendMessageAsync(rawInput);
return (false, false);
}
Console.WriteLine();
if (input == "exit" || input is null)
return true;
return (true, false);
string args = input.Contains(" ") ? input[input.IndexOf(" ")..].Trim() : null;
input = input.Contains(" ") ? input.Substring(0, input.IndexOf(" ")) : input;
if (input == "help")
if (input == "help" || input == "?")
{
Console.WriteLine($"Prefix: '{InputManager.prefix}' (Only required in text channels)");
Console.WriteLine();
for (int i = 0; i < CommandsList.Count; i++)
{
Console.Write($"{i + 1}. {CommandsList.Keys.ElementAt(i)}".PadRight(15));
@@ -69,137 +97,13 @@ namespace DiscordCLI
}
else if (CommandsList.ContainsKey(input))
{
CommandsList[input].Item2(args);
await CommandsList[input].Item2(args);
}
else
{
ConsoleExt.WriteLine("Command does not exist!", ConsoleColor.Red);
}
return false;
}
}
internal class Commands
{
private IReadOnlyDictionary<ulong, DiscordGuild> socketGuildsCache;
private readonly DiscordClient client;
public Commands(DiscordClient dicordClient)
{
client = dicordClient;
}
protected void ListGuilds(string args)
{
socketGuildsCache ??= client.Guilds;
int index = 1;
foreach (DiscordGuild guild in socketGuildsCache.Values)
{
Console.WriteLine($"{index}. {guild.Name}");
index++;
}
}
protected void ListDms(string args)
{
throw new NotImplementedException();
}
protected void ListGuildChannels(string args)
{
IReadOnlyCollection<DiscordChannel> textChannels;
DiscordGuild guild;
if (args != null)
{
if (int.TryParse(args, out int ind))
{
guild = socketGuildsCache?.Values.ElementAtOrDefault(ind - 1);
}
else
{
guild = socketGuildsCache?.FirstOrDefault(x => x.Value.Name == args).Value;
}
GlobalInformation.currentTextChannel = null;
}
else
{
guild = GlobalInformation.currentGuild;
}
if (guild is null)
{
ConsoleExt.WriteLine("Guild not found!", ConsoleColor.Red);
GlobalInformation.currentTextChannel = null;
return;
}
GlobalInformation.currentGuild = guild;
textChannels = guild.Channels;
int index = 1;
foreach (DiscordChannel channel in textChannels)
{
switch (channel.Type)
{
case ChannelType.Category:
//Console.WriteLine($"[{channel.Name}]");
break;
case ChannelType.Text:
Console.WriteLine($" {index}. {channel.Name}");
index++;
break;
}
}
}
protected async void EnterChannel(string args)
{
DiscordChannel textChannel;
if (GlobalInformation.currentGuild is null)
{
ConsoleExt.WriteLine("You are not in a guild!", ConsoleColor.Red);
return;
}
if (int.TryParse(args, out int ind))
{
textChannel = GlobalInformation.currentGuild?.Channels.Where(x => x.Type == ChannelType.Text).ElementAtOrDefault(ind - 1);
}
else
{
textChannel = GlobalInformation.currentGuild?.Channels.Where(x => x.Type == ChannelType.Text).FirstOrDefault(x => x.Name == args);
}
GlobalInformation.currentTextChannel = textChannel;
if (textChannel is null)
{
ConsoleExt.WriteLine("Channel not found!", ConsoleColor.Red);
return;
}
foreach (DiscordMessage message in (await textChannel.GetMessagesAsync(10)).Reverse())
{
try
{
await Program.WriteMessage(message, textChannel, GlobalInformation.currentGuild);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
protected void DeleteToken(string args)
{
File.Delete(Program.tokenPath);
Environment.Exit(0);
return (false, false);
}
}
}
+2 -3
View File
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@@ -10,5 +10,4 @@
<PackageReference Include="DSharpPlus" Version="3.2.3" />
<PackageReference Include="Stone_Red-C-Sharp-Utilities" Version="1.0.0.2" />
</ItemGroup>
</Project>
</Project>
+1 -1
View File
@@ -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\FolderProfile.pubxml</_LastSelectedProfileId>
<_LastSelectedProfileId>C:\Users\David\Google Drive\Programmieren\DiscordCLI\DiscordCLI\Properties\PublishProfiles\Windows.pubxml</_LastSelectedProfileId>
</PropertyGroup>
</Project>
-6
View File
@@ -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;
}
}
+65
View File
@@ -0,0 +1,65 @@
using DSharpPlus;
using System.Threading.Tasks;
using System;
using DSharpPlus.Entities;
using System.Linq;
namespace DiscordCLI
{
internal class InputManager
{
private readonly CommandsManager commandsManager;
private readonly DiscordClient client;
public const string prefix = ">";
public string Input { get; private set; } = string.Empty;
public InputManager(DiscordClient dicordClient, CommandsManager commandsMan)
{
client = dicordClient;
commandsManager = commandsMan;
}
public async Task ReadInput()
{
bool exit = false;
bool printOverride = false;
string lastInput = prefix;
while (!exit)
{
DiscordDmChannel dmChannel = GlobalInformation.currentTextChannel as DiscordDmChannel;
string infoString = $"\r[{client.CurrentUser.Username}#{client.CurrentUser.Discriminator}] [{(dmChannel is null ? GlobalInformation.currentGuild?.Name : "DMs")}/{(dmChannel is null ? GlobalInformation.currentTextChannel?.Name : string.Join(", ", dmChannel.Recipients.Select(x => x.Username)))}] ==> ";
if (lastInput.StartsWith(prefix) || printOverride)
Console.Write(Environment.NewLine + infoString);
printOverride = false;
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(prefix))
Input = prefix + Input;
lastInput = new string(Input);
Input = string.Empty;
(exit, printOverride) = await commandsManager.CheckCommand(lastInput);
}
}
}
}
+199
View File
@@ -0,0 +1,199 @@
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, bool writeInfo = true)
{
try
{
if (channel.Id != GlobalInformation.currentTextChannel?.Id)
return;
DiscordUser user = message.Author;
if (GlobalInformation.currentGuild is not null)
{
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}");
}
else
{
WriteTop($"[{user.Username}]", Color.White, message, true, true, $"{user.Username}#{user.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);
}
if (embed.Fields is not null)
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);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
WriteTop(string.Empty, Color.White, message);
DiscordDmChannel dmChannel = GlobalInformation.currentTextChannel as DiscordDmChannel;
if (writeInfo)
{
string infoString = $"\r[{client.CurrentUser.Username}#{client.CurrentUser.Discriminator}] [{(dmChannel is null ? GlobalInformation.currentGuild?.Name : "DMs")}/{(dmChannel is null ? GlobalInformation.currentTextChannel?.Name : string.Join(", ", dmChannel.Recipients.Select(x => x.Username)))}] ==> ";
Console.Write(infoString);
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;
}
}
if (!discordMessage.Channel.IsPrivate)
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;
}
}
if (!discordMessage.Channel.IsPrivate)
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 closestColor = 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;
closestColor = consoleColor;
}
}
return closestColor;
}
}
}
+19 -190
View File
@@ -1,27 +1,20 @@
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
{
internal class Program
{
public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
public static void Main() => 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()
@@ -33,7 +26,8 @@ namespace DiscordCLI
if (File.Exists(tokenPath))
token = File.ReadAllText(tokenPath);
while (string.IsNullOrEmpty(token))
tokenInput:
while (string.IsNullOrWhiteSpace(token))
{
Console.Write("Enter auth token: ");
token = Console.ReadLine();
@@ -46,7 +40,7 @@ namespace DiscordCLI
client = new DiscordClient(new DiscordConfiguration()
{
Token = token,
TokenType = TokenType.User
TokenType = TokenType.User,
});
client.MessageCreated += Client_MessageCreated;
@@ -58,190 +52,25 @@ namespace DiscordCLI
ConsoleExt.WriteLine(ex, ConsoleColor.Red);
if (ex.Message.Contains("Authentication failed"))
{
File.Delete(tokenPath);
token = string.Empty;
goto tokenInput;
}
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 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;
await outputManager.WriteMessage(e.Message, e.Channel, e.Guild);
}
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net5.0\publish\Linux-arm</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
</PropertyGroup>
</Project>
@@ -3,4 +3,7 @@
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2021-05-27T05:47:11.2358523Z;</History>
</PropertyGroup>
</Project>
@@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net5.0\publish\</PublishDir>
<PublishDir>bin\Release\net5.0\publish\Linux-x64</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2021-05-27T05:47:04.1968183Z;True|2021-05-27T07:46:41.8745649+02:00;</History>
</PropertyGroup>
</Project>
@@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net5.0\publish\</PublishDir>
<PublishDir>bin\Release\net5.0\publish\Windows</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0</TargetFramework>
<SelfContained>false</SelfContained>
@@ -3,4 +3,7 @@
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2021-05-27T05:49:47.3666888Z;True|2021-05-27T07:46:50.5745814+02:00;</History>
</PropertyGroup>
</Project>
Binary file not shown.
Binary file not shown.
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": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -1,9 +0,0 @@
{
"runtimeOptions": {
"tfm": "netcoreapp3.1",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "3.1.0"
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,7 +3,8 @@
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages",
"C:\\Program Files (x86)\\Microsoft\\Xamarin\\NuGet"
]
}
}
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"
]
}
}
Binary file not shown.
@@ -3,7 +3,8 @@
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages",
"C:\\Program Files (x86)\\Microsoft\\Xamarin\\NuGet"
]
}
}
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
dfd104565a5c51e51c96fc2a0f9e9b9515e39127
d74c436d6f045a94c6aefe230b53cf4fbca29cc5
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -14,10 +14,12 @@
"outputPath": "C:\\Users\\David\\Google Drive\\Programmieren\\DiscordCLI\\DiscordCLI\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Microsoft\\Xamarin\\NuGet\\"
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages",
"C:\\Program Files (x86)\\Microsoft\\Xamarin\\NuGet\\"
],
"configFilePaths": [
"C:\\Users\\David\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Xamarin.Offline.config"
],
@@ -72,7 +74,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.104\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.203\\RuntimeIdentifierGraph.json"
}
}
}
@@ -5,12 +5,14 @@
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\David\.nuget\packages\;C:\Microsoft\Xamarin\NuGet\</NuGetPackageFolders>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\David\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages;C:\Program Files (x86)\Microsoft\Xamarin\NuGet\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">5.8.1</NuGetToolVersion>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">5.9.1</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="$([MSBuild]::EnsureTrailingSlash($(NuGetPackageFolders)))" />
<SourceRoot Include="C:\Users\David\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft\Xamarin\NuGet\" />
</ItemGroup>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
@@ -1 +1 @@
3ab14035ed25e46cb2eb046c245e2f90325a2743
6b3b4647b7644f9b16fb8ec3b15a9fe904531b35
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
6843f3cabf894dfce1aaffe6c65e64e8531c52b6
94d569a53a5ac292d10f2db736ac10f01f735125

Some files were not shown because too many files have changed in this diff Show More