Esempio n. 1
0
        public bool IsCommand(DogeyCommandContext context, string prefix, out int argPos)
        {
            argPos = 0;
            if (context.User.Id == _discord.CurrentUser.Id)
            {
                return(false);
            }
            bool hasStringPrefix = prefix == null ? false : context.Message.HasStringPrefix(prefix, ref argPos);

            return(hasStringPrefix || context.Message.HasMentionPrefix(_discord.CurrentUser, ref argPos));
        }
Esempio n. 2
0
        private Task OnMessageReceivedAsync(SocketMessage s)
        {
            _ = Task.Run(async() =>
            {
                if (!(s is SocketUserMessage msg))
                {
                    return;
                }

                var context   = new DogeyCommandContext(_discord, msg);
                string prefix = await _root.GetPrefixAsync(context.Guild);

                if (IsCommand(context, prefix, out int argPos))
                {
                    using (context.Channel.EnterTypingState())
                        await ExecuteAsync(context, _provider, context.Message.Content.Substring(argPos));
                }
            }, _cancellationToken);
            return(Task.CompletedTask);
        }
Esempio n. 3
0
        public async Task <Embed> EvaluateAsync(DogeyCommandContext context, string script)
        {
            var timer = Stopwatch.StartNew();

            var    options   = GetOptions();
            string formatted = GetFormattedCode("cs", script);

            object result;

            try
            {
                result = await CSharpScript.EvaluateAsync(formatted, options, new RoslynGlobals(context, _services));
            } catch (Exception ex)
            {
                result = ex;
            }

            timer.Stop();
            return(GetEmbed(formatted, result, timer.ElapsedMilliseconds));
        }
Esempio n. 4
0
        public async Task ExecuteAsync(DogeyCommandContext context, IServiceProvider provider, string input)
        {
            var result = await _commands.ExecuteAsync(context, input, provider);

            if (result.IsSuccess || result.Error == CommandError.UnknownCommand)
            {
                return;
            }
            if (result is ExecuteResult execute)
            {
                _logger.LogError(execute.Exception?.ToString());
            }
            if (result is ParseResult parse && parse.Error == CommandError.BadArgCount)
            {
                var command = _commands.Search(context, input).Commands
                              .OrderByDescending(x => x.Command.Parameters.Count())
                              .FirstOrDefault().Command;

                var builder = new StringBuilder("!" + command.Name);
                if (command.Parameters.Count > 0)
                {
                    // !name <required> [optional=1]
                    foreach (var arg in command.Parameters)
                    {
                        string argText = arg.Name;

                        if (arg.IsRemainder)
                        {
                            argText += "...";
                        }
                        if (arg.IsMultiple)
                        {
                            argText += "+";
                        }

                        if (arg.IsOptional)
                        {
                            argText = '[' + argText;
                            if (arg.DefaultValue != null)
                            {
                                argText += ($"={arg.DefaultValue}");
                            }
                            argText += ']';
                        }
                        else
                        {
                            argText  = '<' + argText;
                            argText += '>';
                        }

                        builder.Append($" {argText}");
                    }
                }

                await context.Channel.SendMessageAsync($"{parse.ErrorReason} {builder}");

                return;
            }

            if (!string.IsNullOrWhiteSpace(result.ErrorReason))
            {
                await context.Channel.SendMessageAsync(result.ErrorReason);
            }
        }
Esempio n. 5
0
        private Task OnMessageReceivedAsync(SocketMessage s)
        {
            _ = Task.Run(async() =>
            {
                //bool plonked = await _root.IsBannedAsync(msg.Author);
                //if (plonked || msg.Author.IsBot) return;

                if (!(s is SocketUserMessage msg))
                {
                    return;
                }
                if (msg.Author.IsBot)
                {
                    return;
                }

                var context   = new DogeyCommandContext(_discord, msg);
                string prefix = await _root.GetPrefixAsync(context.Guild);

                if (_commands.IsCommand(context, prefix, out int argPos))
                {
                    return;
                }

                var wallet = await _points.GetOrCreateWalletAsync(msg.Author);

                try
                {
                    int earning = 0;

                    // Bonus points for a prime id
                    if (MathHelper.IsPrime(msg.Id))
                    {
                        earning += 25;
                    }

                    // Add 3-5x points for concecutive numbers
                    for (int mult = 3; mult <= 5; mult++)
                    {
                        int repeats = StringHelper.RepeatingChars(msg.Id.ToString(), mult);
                        if (repeats > 0)
                        {
                            earning += repeats * (mult - 1);
                        }
                    }

                    if (wallet.Multiplier != null)
                    {
                        var multiplied = earning * wallet.Multiplier.Value;
                        earning       += (int)Math.Round(multiplied, MidpointRounding.AwayFromZero);
                    }

                    if (earning > 0)
                    {
                        var log = await _points.CreateAsync(new PointLog
                        {
                            Timestamp   = msg.Timestamp.DateTime,
                            UserId      = msg.Author.Id,
                            SenderId    = msg.Id,
                            EarningType = EarningType.Message,
                            Amount      = earning
                        });

                        wallet.Balance += log.Amount;
                        await _points.ModifyAsync(wallet);

                        foreach (var action in Actions.Values)
                        {
                            await action(log);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Unable to add points: {ex}", ex);
                }
            }, _cancellationToken);
            return(Task.CompletedTask);
        }
Esempio n. 6
0
 public RoslynGlobals(DogeyCommandContext context, IServiceProvider services)
 {
     Context  = context;
     Services = services;
 }