Esempio n. 1
0
        public async Task Invoke(SocketUserMessage msg)
        {
            try
            {
                var f = new Func <IServiceProvider, Task>(async(provider) =>
                {
                    try
                    {
                        var msgContext = provider.GetRequiredService <DiscordGuildMessageContext>();
                        var repo       = provider.GetRequiredService <IRepository <WelcomeMessage> >();

                        await repo.AddOrUpdateAsync(new WelcomeMessage()
                        {
                            GuildId = msgContext.Guild.Id,
                            Message = msgContext.Message.Content.Trim()
                        });
                        await repo.CommitAsync();
                        var obj = await repo.Get().SingleAsync(x => x.GuildId == _msgContext.Guild.Id);
                        await msgContext.Message.Channel.SendMessageAsync("New MOTD is set to:");
                        await msgContext.Message.Channel.SendMessageAsync(obj.Message);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                });

                var continuedCommand = new ContinuedCommand(f, _msgContext);
                _continuedCommands.Add(continuedCommand);

                await msg.Channel.SendMessageAsync("Type in a message to do stuff");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task Invoke(SocketUserMessage msg)
        {
            var content    = msg.Content.Trim();
            var parameters = content.Split(' ', StringSplitOptions.RemoveEmptyEntries);
            var modifier   = parameters[1].ToLower();
            var guild      = ((SocketGuildChannel)msg.Channel).Guild;

            switch (modifier)
            {
            case "create":
                var name = parameters[2].ToString();

                var command = new Func <IServiceProvider, Task>(async(services) =>
                {
                    var repo       = services.GetRequiredService <IRepository <CustomCommand> >();
                    var msgContext = services.GetRequiredService <DiscordGuildMessageContext>();

                    var cmd = new CustomCommand()
                    {
                        Response = msgContext.Message.Content,
                        GuildId  = msgContext.Guild.Id,
                        Name     = name
                    };
                    await repo.AddOrUpdateAsync(cmd);
                    await repo.CommitAsync();
                });

                var continuedCommand = new ContinuedCommand(command, _msgContext);

                _continuedCommands.Add(continuedCommand);
                await msg.Channel.SendMessageAsync("Enter command response");

                break;

            case "remove":
                var removedName = parameters[2].Trim().ToLower();
                var removedCmd  = await _customCommandRepository.Get().SingleOrDefaultAsync(x =>
                                                                                            x.GuildId == _msgContext.Guild.Id && x.Name.ToLower() == removedName);

                if (removedCmd != null)
                {
                    _customCommandRepository.Remove(removedCmd);
                    await _customCommandRepository.CommitAsync();

                    await msg.Channel.SendMessageAsync($"Deleted {removedCmd.Name}");
                }
                else
                {
                    await msg.Channel.SendMessageAsync("Custom command not found");
                }
                break;

            case "list":
                var commands = await _customCommandRepository.Get().Where(x => x.GuildId == guild.Id).ToListAsync();

                if (!commands.Any())
                {
                    await msg.Channel.SendMessageAsync("No commands registered.");

                    break;
                }

                var commandResponse = "Custom Commands:\n";
                foreach (var cmd in commands)
                {
                    commandResponse += (_prefix + cmd.Name + "\n");
                }
                commandResponse = commandResponse.Trim('\n');
                await msg.Channel.SendMessageAsync(commandResponse);

                break;

            default:
                throw new ArgumentException("Must contain valid modifier");
            }
        }