-
Notifications
You must be signed in to change notification settings - Fork 3
poll-feat #39
base: master
Are you sure you want to change the base?
poll-feat #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,24 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading.Tasks; | ||
| using Discord; | ||
| using Discord.Commands; | ||
| using Discord.Rest; | ||
| using Discord.WebSocket; | ||
| using DSharpPlus; | ||
| using DSharpPlus.Entities; | ||
| using FluentResults; | ||
| using Kysect.BotFramework.Core; | ||
| using Kysect.BotFramework.Core.BotMedia; | ||
| using Kysect.BotFramework.Core.BotMessages; | ||
| using Kysect.BotFramework.Core.Tools.Loggers; | ||
| using Kysect.BotFramework.DefaultCommands; | ||
| using Kysect.BotFramework.Settings; | ||
| using TokenType = Discord.TokenType; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Kysect.BotFramework.ApiProviders.Discord | ||
| { | ||
|
|
@@ -21,6 +27,12 @@ public class DiscordApiProvider : IBotApiProvider, IDisposable | |
| private readonly object _lock = new object(); | ||
| private readonly DiscordSettings _settings; | ||
| private DiscordSocketClient _client; | ||
| private int _argsCount; | ||
|
|
||
| private readonly string[] _emojis = | ||
| { | ||
| "", "1️⃣", "2️⃣", "3️⃣","4️⃣","5️⃣","6️⃣","7️⃣", "8️⃣", "9️⃣", "🔟" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Bibletoon Записывай как работать с эмодзи кекв There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (это локальный мем, если что, не обращайте внимания)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кекв |
||
| }; | ||
|
Comment on lines
+32
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не часть логики DiscordApiProvider. |
||
|
|
||
| public DiscordApiProvider(ISettingsProvider<DiscordSettings> settingsProvider) | ||
| { | ||
|
|
@@ -118,12 +130,26 @@ public Result<string> SendTextMessage(string text, SenderInfo sender) | |
| if (text.Length == 0) | ||
| { | ||
| LoggerHolder.Instance.Error("The message wasn't sent by the command " + | ||
| $"\"{PingCommand.Descriptor.CommandName}\", the length must not be zero."); | ||
| "\"{CommandName}\", the length must not be zero", | ||
| PingCommand.Descriptor.CommandName); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return Result.Ok(); | ||
| } | ||
|
|
||
| return SendText(text, sender); | ||
| } | ||
|
|
||
| public Result<string> SendPollMessage(string text, SenderInfo sender) | ||
| { | ||
| if (text.Length == 0) | ||
| { | ||
| LoggerHolder.Instance.Error("The message wasn't sent by the command " + | ||
| "\"{CommandName}\", the length must not be zero", | ||
| PollCommand.Descriptor.CommandName); | ||
| return Result.Ok(); | ||
|
Comment on lines
+145
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Залогировали ошибку и вернули ок?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему тут Result.Ok, если это возвращение при ошибке - не валидном наборе аргументов. |
||
| } | ||
|
|
||
| return SendPoll(text, sender); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
|
|
@@ -150,15 +176,22 @@ private Task ClientOnMessage(SocketMessage arg) | |
| } | ||
|
|
||
| var context = new SocketCommandContext(_client, message); | ||
| if (!context.User.IsBot && context.Message.Content.Contains("!Poll")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| _argsCount = GetPollArguments(message.Content).Count() - 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что за -1?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я думаю это связано с тем, что на 0 позиции будет название команды, а остальные - это аргументы пула. А он считает именно их кол-во. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тогда не понял, почему вообще название команды это аргумент команды...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На 0-й позиции будет заголовок опроса. -1 поправлю, глупый момент, согласен
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Команды не должны менять внутренний контекст провайдера иначе будут сайд эффекты при параллельных запросах. |
||
| } | ||
| if (context.User.IsBot && context.Message.Embeds.Any()) | ||
| { | ||
| ReactWithEmojis(context); | ||
| } | ||
|
|
||
| if (context.User.IsBot || context.Guild is null) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| LoggerHolder.Instance.Debug($"New message event: {context.Message}"); | ||
|
|
||
| IBotMessage botMessage = ParseMessage(message, context); | ||
|
|
||
| OnMessage?.Invoke(context.Client, | ||
| new BotEventArgs( | ||
| botMessage, | ||
|
|
@@ -270,18 +303,83 @@ private Result<string> SendText(string text, SenderInfo sender) | |
| return Result.Fail(new Error(message).CausedBy(e)); | ||
| } | ||
| } | ||
|
|
||
| private Result<string> SendPoll(string text, SenderInfo sender) | ||
| { | ||
| List<string> arguments = GetPollArguments(text); | ||
|
|
||
| Result<string> result = CheckText(text); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Наверное же сначала нужно делать этот чек, а потом уже GetPoolArgs. |
||
| if (result.IsFailed) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| for (int i = 1; i < arguments.Count; i++) | ||
| { | ||
| arguments[i] = _emojis[i] + "\t" + arguments[i]; | ||
| } | ||
|
|
||
| var embed = new EmbedBuilder | ||
| { | ||
| Title = arguments[0], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У тебя из метода |
||
| Color = Color.Purple, | ||
| Description = String.Join("\n", arguments.Skip(1)) | ||
| }; | ||
|
|
||
|
Comment on lines
+317
to
+328
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Опять же, это выглядит как логика создания какого-то особенного сообщения. Давайте выделим класс PoolInfoMessage, которое будет создаваться от аргументов, внутри вот эти действия делать и ему оставим метод BuildToEmbed. |
||
| Task<RestUserMessage> task = _client.GetGuild((ulong) sender.GroupId) | ||
| .GetTextChannel((ulong) sender.UserSenderId) | ||
| .SendMessageAsync(embed: embed.Build()); | ||
|
|
||
| try | ||
| { | ||
| task.Wait(); | ||
| return Result.Ok("Message send"); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| var message = "Error while sending message"; | ||
| LoggerHolder.Instance.Error(e, message); | ||
| return Result.Fail(new Error(message).CausedBy(e)); | ||
| } | ||
| } | ||
|
|
||
| private Result<string> CheckText(string text) | ||
| { | ||
| if (text.Length > 2000) | ||
| { | ||
| string subString = text.Substring(0, 99) + "..."; | ||
| string errorMessage = "The message wasn't sent by the command " + | ||
| $"\"{PingCommand.Descriptor.CommandName}\", the length is too big."; | ||
| $"\"{PingCommand.Descriptor.CommandName}\", the length is too big"; | ||
| return Result.Fail(new Error(errorMessage).CausedBy(subString)); | ||
| } | ||
|
|
||
| return Result.Ok(); | ||
| } | ||
|
|
||
| private List<string> GetPollArguments(string args) | ||
| { | ||
| var regex = new Regex(@"[^\s""']+|""([^""]*)""|'([^']*)'"); // Splits into "..." '...' a b c | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А в чем смысл создавать каждый раз одну и ту же регулярку? Ну сделай ее статичным полем приватным просто |
||
| var matches = regex.Matches(args); | ||
| List<string> options = new List<string>(); | ||
|
|
||
| if (matches.Count > 0) | ||
| { | ||
| foreach (Match match in matches) | ||
| { | ||
| options.Add(match.Value.Replace("\"", "")); | ||
| } | ||
| } | ||
|
|
||
| return options; | ||
| } | ||
|
Comment on lines
+359
to
+374
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ну вот этот метод точно можно было унести куда-то. Не относится к DiscordProvider. |
||
|
|
||
| private async void ReactWithEmojis(SocketCommandContext context) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| for (int i = 1; i < _argsCount; i++) | ||
| { | ||
| await context.Message.AddReactionAsync(new Emoji(_emojis[i])) | ||
| .ConfigureAwait(false); | ||
| } | ||
| } | ||
|
Comment on lines
+376
to
+383
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да откуда вы все берете |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.RegularExpressions; | ||
| using Kysect.BotFramework.ApiProviders; | ||
| using Telegram.Bot.Requests; | ||
|
|
||
| namespace Kysect.BotFramework.Core.BotMessages | ||
| { | ||
| public class BotPollMessage : IBotMessage | ||
| { | ||
| public BotPollMessage(string text) | ||
| { | ||
| Text = text; | ||
| } | ||
|
|
||
| public string Text { get; } | ||
|
|
||
| public void Send(IBotApiProvider apiProvider, SenderInfo sender) | ||
| { | ||
| apiProvider.SendPollMessage(Text, sender); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| using FluentResults; | ||
| using Kysect.BotFramework.Core.BotMessages; | ||
| using Kysect.BotFramework.Core.Commands; | ||
| using Kysect.BotFramework.DefaultCommands; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Kysect.BotFramework.Core.CommandInvoking | ||
|
|
@@ -24,7 +25,7 @@ public Result CheckArgsCount(CommandContainer args) | |
| return commandTask.ToResult<CommandContainer>(); | ||
| } | ||
|
|
||
| return commandTask.Value.Args.Length == args.Arguments.Count | ||
| return (commandTask.Value.Args.Count == args.Arguments.Count) || (args.CommandName == "Poll") // Better way to check for Poll? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что-то вообще ничего не понял
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если есть кастомная логика проверки валидности размера, то предлагаю сделать базовый класс для команд, где будет виртуальный метод IsValidArgumentCount, который проверяет commandTask.Value.Args.Count == args.Arguments.Count, а PollCommand будет наследоваться и оверрайдить со своей кастомной логикой (в идеале там должен быть парсинг и какая-то проверка, а не "если poll - значит точно ок". |
||
| ? Result.Ok() | ||
| : Result.Fail<CommandContainer>( | ||
| "Cannot execute command. Argument count miss matched with command signature"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Kysect.BotFramework.Core.CommandInvoking; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
|
|
@@ -8,9 +9,9 @@ public abstract class BotCommandDescriptor | |
| { | ||
| public string CommandName { get; } | ||
| public string Description { get; } | ||
| public string[] Args { get; } | ||
| public List<string> Args { get; } | ||
|
Comment on lines
-11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В чем суть данного (и пачки ниже) изменения?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хотел получать переменное кол-во аргументов. Изначально нужно было указывать определенное количество, как я понял. Так же и появилась проблема выше - проверка на полученное количество аргументов и заданное на получение в PollCommand дескрипторе, как args There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вообще ничего не понял
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ну нам нужно получать !Poll + args и непонятно как действовать с string[ ], т.к. он просит указать точное количество args, а их может быть от 1 до 10 по логике, поэтому изменил на лист. Может и правда не так понял 😕 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так тебе приходит запрос уже с каким-то конкретным количеством аргументов, зачем тут лист? |
||
|
|
||
| public BotCommandDescriptor(string commandName, string description, string[] args) | ||
| public BotCommandDescriptor(string commandName, string description, List<string> args) | ||
| { | ||
| CommandName = commandName; | ||
| Description = description; | ||
|
|
@@ -22,13 +23,13 @@ public BotCommandDescriptor(string commandName, string description, string[] arg | |
|
|
||
| public class BotCommandDescriptor<T> : BotCommandDescriptor where T : IBotCommand | ||
| { | ||
| public BotCommandDescriptor(string commandName, string description, string[] args) | ||
| public BotCommandDescriptor(string commandName, string description, List<string> args) | ||
| : base(commandName, description, args) | ||
| { | ||
| } | ||
|
|
||
| public BotCommandDescriptor(string commandName, string description) | ||
| : this(commandName, description, Array.Empty<string>()) | ||
| : this(commandName, description, new List<string>()) | ||
| { | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using FluentResults; | ||
| using Kysect.BotFramework.Core.BotMessages; | ||
| using Kysect.BotFramework.Core.Commands; | ||
|
|
||
| namespace Kysect.BotFramework.DefaultCommands | ||
| { | ||
| public class PollCommand : IBotAsyncCommand | ||
| { | ||
| public static readonly BotCommandDescriptor<PollCommand> Descriptor = new BotCommandDescriptor<PollCommand>( | ||
| "Poll", | ||
| "Create a new poll", new List<string>()); | ||
|
|
||
| public Result CanExecute(CommandContainer args) => Result.Ok(); | ||
|
|
||
| public Task<Result<IBotMessage>> Execute(CommandContainer args) | ||
| { | ||
| IBotMessage message = new BotPollMessage(String.Join(" ", args.Arguments)); | ||
| return Task.FromResult(Result.Ok(message)); | ||
|
Comment on lines
+21
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ничего не понял)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В предыдущих |
||
| } | ||
|
Comment on lines
+19
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не понял, создал ботмесседж и вернул его
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А она внутри IBotMessage и вызывается BotManager'ом |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это не часть логики DiscordApiProvider.