commit 3fa19a47943252cd8b15df70eab23d4085757ee4 Author: l1ttleO Date: Tue Dec 7 17:52:37 2021 +0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2976bab --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.idea/ +*.user +token.txt +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/Boyfriend-CSharp.sln b/Boyfriend-CSharp.sln new file mode 100644 index 0000000..003c58b --- /dev/null +++ b/Boyfriend-CSharp.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Boyfriend", "Boyfriend\Boyfriend.csproj", "{21640A7A-75C2-4515-A1DF-CE8B6EEBD260}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {21640A7A-75C2-4515-A1DF-CE8B6EEBD260}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {21640A7A-75C2-4515-A1DF-CE8B6EEBD260}.Debug|Any CPU.Build.0 = Debug|Any CPU + {21640A7A-75C2-4515-A1DF-CE8B6EEBD260}.Release|Any CPU.ActiveCfg = Release|Any CPU + {21640A7A-75C2-4515-A1DF-CE8B6EEBD260}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Boyfriend/Boyfriend.cs b/Boyfriend/Boyfriend.cs new file mode 100644 index 0000000..89853d8 --- /dev/null +++ b/Boyfriend/Boyfriend.cs @@ -0,0 +1,28 @@ +using Discord; +using Discord.WebSocket; + +namespace Boyfriend; + public class Boyfriend { + + public static void Main(string[] args) + => new Boyfriend().MainAsync().GetAwaiter().GetResult(); + + public static readonly DiscordSocketClient Client = new(); + + private async Task MainAsync() { + Client.Log += Log; + var token = File.ReadAllText("token.txt").Trim(); + + await Client.LoginAsync(TokenType.Bot, token); + await Client.StartAsync(); + + await new CommandHandler().InstallCommandsAsync(); + + await Task.Delay(-1); + } + + private static Task Log(LogMessage msg) { + Console.WriteLine(msg.ToString()); + return Task.CompletedTask; + } + } \ No newline at end of file diff --git a/Boyfriend/Boyfriend.csproj b/Boyfriend/Boyfriend.csproj new file mode 100644 index 0000000..9d8eac3 --- /dev/null +++ b/Boyfriend/Boyfriend.csproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + enable + enable + default + Boyfriend + l1ttle + https://github.com/l1ttleO/Boyfriend-CSharp + https://github.com/l1ttleO/Boyfriend-CSharp + git + + + + + + + diff --git a/Boyfriend/Boyfriend.csproj.DotSettings b/Boyfriend/Boyfriend.csproj.DotSettings new file mode 100644 index 0000000..25ce924 --- /dev/null +++ b/Boyfriend/Boyfriend.csproj.DotSettings @@ -0,0 +1,3 @@ + + Yes + \ No newline at end of file diff --git a/Boyfriend/CommandHandler.cs b/Boyfriend/CommandHandler.cs new file mode 100644 index 0000000..5f000ba --- /dev/null +++ b/Boyfriend/CommandHandler.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using Discord.Commands; +using Discord.WebSocket; + +namespace Boyfriend; + +public class CommandHandler { + private readonly DiscordSocketClient _client = Boyfriend.Client; + private readonly CommandService _commands = new CommandService(); + + public async Task InstallCommandsAsync() { + _client.MessageReceived += HandleCommandAsync; + await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), null); + } + + private async Task HandleCommandAsync(SocketMessage messageParam) { + if (messageParam is not SocketUserMessage message) return; + var argPos = 0; + + if (!(message.HasCharPrefix('!', ref argPos) || + message.HasMentionPrefix(_client.CurrentUser, ref argPos)) || + message.Author.IsBot) + return; + + var context = new SocketCommandContext(_client, message); + + await _commands.ExecuteAsync(context, argPos, null); + } +} \ No newline at end of file diff --git a/Boyfriend/Commands/PingModule.cs b/Boyfriend/Commands/PingModule.cs new file mode 100644 index 0000000..c818ee2 --- /dev/null +++ b/Boyfriend/Commands/PingModule.cs @@ -0,0 +1,12 @@ +using Discord.Commands; + +namespace Boyfriend.Commands; + +public class PingModule : ModuleBase { + + [Command("ping")] + [Summary("Измеряет время обработки REST-запроса")] + [Alias("пинг")] + public async Task Run() + => await ReplyAsync(Utils.GetBeep() + Boyfriend.Client.Latency + "мс"); +} \ No newline at end of file diff --git a/Boyfriend/Utils.cs b/Boyfriend/Utils.cs new file mode 100644 index 0000000..6b6524b --- /dev/null +++ b/Boyfriend/Utils.cs @@ -0,0 +1,8 @@ +namespace Boyfriend; + +public static class Utils { + public static string GetBeep() { + var letters = new[] { "а", "о", "и"}; + return "Б" + letters[new Random().Next(3)] + "п! "; + } +} \ No newline at end of file