Initial commit

This commit is contained in:
Stone-Red-Code
2021-04-12 12:37:30 +02:00
commit ce06f5b159
174 changed files with 39729 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
+1
View File
@@ -0,0 +1 @@
*.txt
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31019.35
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordCLI", "DiscordCLI\DiscordCLI.csproj", "{24B958A5-5485-4028-AE10-253105918CBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{24B958A5-5485-4028-AE10-253105918CBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24B958A5-5485-4028-AE10-253105918CBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24B958A5-5485-4028-AE10-253105918CBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24B958A5-5485-4028-AE10-253105918CBD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E73EF9D5-29ED-4906-88C0-118F2A025B6A}
EndGlobalSection
EndGlobal
+205
View File
@@ -0,0 +1,205 @@
using DSharpPlus;
using System;
using System.Collections.Generic;
using System.Linq;
using Stone_Red_Utilities.ColorConsole;
using DSharpPlus.Entities;
using System.Diagnostics;
using System.IO;
namespace DiscordCLI
{
internal class CommandsManager : Commands
{
private readonly DiscordClient client;
private readonly Dictionary<string, (string, Action<string>)> CommandsList;
public CommandsManager(DiscordClient dicordClient) : base(dicordClient)
{
client = dicordClient;
CommandsList = new()
{
{ "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) },
{ "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) },
};
}
public bool CheckCommand(string input)
{
input = input.Trim().ToLower().Replace('\n', '\0');
if (string.IsNullOrWhiteSpace(input))
return false;
if (input.StartsWith('<'))
{
input = input.Remove(0, 1);
}
else if (GlobalInformation.currentTextChannel != null)
{
Console.CursorTop--;
Console.Write("\r" + new string(' ', Console.WindowWidth));
GlobalInformation.currentTextChannel.SendMessageAsync(input);
return false;
}
Console.WriteLine();
if (input == "exit" || input is null)
return true;
string args = input.Contains(" ") ? input[input.IndexOf(" ")..].Trim() : null;
input = input.Contains(" ") ? input.Substring(0, input.IndexOf(" ")) : input;
if (input == "help")
{
for (int i = 0; i < CommandsList.Count; i++)
{
Console.Write($"{i + 1}. {CommandsList.Keys.ElementAt(i)}".PadRight(15));
Console.WriteLine(CommandsList.Values.ElementAt(i).Item1);
}
}
else if (CommandsList.ContainsKey(input))
{
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);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ColorMineStandard" Version="1.0.0" />
<PackageReference Include="DSharpPlus" Version="3.2.3" />
<PackageReference Include="Stone_Red-C-Sharp-Utilities" Version="1.0.0.2" />
</ItemGroup>
</Project>
+6
View File
@@ -0,0 +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>
</PropertyGroup>
</Project>
+16
View File
@@ -0,0 +1,16 @@
using DSharpPlus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordCLI
{
internal static class GlobalInformation
{
public static DiscordGuild currentGuild;
public static DiscordChannel currentTextChannel;
public static int colorMode = 1;
}
}
+247
View File
@@ -0,0 +1,247 @@
using ColorMine.ColorSpaces;
using ColorMine.ColorSpaces.Comparisons;
using DSharpPlus;
using DSharpPlus.Entities;
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;
namespace DiscordCLI
{
internal class Program
{
public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private static DiscordClient client;
private CommandsManager commandsManager;
private static string input = "<";
public const string tokenPath = "token.txt";
public async Task MainAsync()
{
Console.ForegroundColor = ConsoleColor.White;
Console.OutputEncoding = System.Text.Encoding.UTF8;
string token = string.Empty;
if (File.Exists(tokenPath))
token = File.ReadAllText(tokenPath);
while (string.IsNullOrEmpty(token))
{
Console.Write("Enter auth token: ");
token = Console.ReadLine();
}
File.WriteAllText(tokenPath, token);
try
{
client = new DiscordClient(new DiscordConfiguration()
{
Token = token,
TokenType = TokenType.User
});
client.MessageCreated += Client_MessageCreated;
await client.ConnectAsync();
}
catch (Exception ex)
{
ConsoleExt.WriteLine(ex, ConsoleColor.Red);
if (ex.Message.Contains("Authentication failed"))
File.Delete(tokenPath);
return;
}
commandsManager = new CommandsManager(client);
await ReadInput();
await Task.Delay(-1);
}
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;
}
}
}
@@ -0,0 +1,16 @@
<?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\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
</PropertyGroup>
</Project>
@@ -0,0 +1,6 @@
<?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">
</Project>
@@ -0,0 +1,14 @@
<?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\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0</TargetFramework>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
@@ -0,0 +1,6 @@
<?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">
</Project>
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.
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
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.
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "netcoreapp3.1",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "3.1.0"
}
}
}
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.
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\David\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\David\\.nuget\\packages",
"C:\\Microsoft\\Xamarin\\NuGet"
]
}
}
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
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.
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net5.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "5.0.0"
}
}
}
Binary file not shown.
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v5.0", FrameworkDisplayName = "")]
@@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Dieser Code wurde von einem Tool generiert.
// Laufzeitversion:4.0.30319.42000
//
// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
// der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("DiscordCLI")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("DiscordCLI")]
[assembly: System.Reflection.AssemblyTitleAttribute("DiscordCLI")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Von der MSBuild WriteCodeFragment-Klasse generiert.
@@ -0,0 +1 @@
b19e6f37bddcbdb1df945fddaf2581ec8ab2932c
@@ -0,0 +1,8 @@
is_global = true
build_property.TargetFramework = net5.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.PublishSingleFile =
build_property.IncludeAllContentForSelfExtract =
build_property._SupportedPlatformList = Android,iOS,Linux,macOS,Windows
Binary file not shown.

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